• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 14.  Command Objects > Using Command Objects

Using Command Objects

The following example represents the use of a Command object for the execution of SQL queries that can be entered by the user:

Sub ExecuteSQL ()
  Dim cmd As ADODB.Command
  Dim strQry As String
  Dim lngRecordsAffected As Long
   
  On Error GoTo Errorhandler
 
  Set cmd = New ADODB.Command  
  cmd.ActiveConnection = CurrentProject.Connection
  Do
    strQry = InputBox("Name of Query or SQL-text" , Default:=strQry)
    If strQry <> "" Then
      cmd.CommandText = strQry
      cmd.Execute lngRecordsAffected
      MsgBox lngRecordsAffected & " records modified."
    End If
  Loop Until strQry = ""
exitsub:
  Set cmd = Nothing
  Exit Sub
Errorhandler:
  Dim strTmp As String
  strTmp = "VBA:" & vbTab & Err.Number & " - " & Err.Description & _
    vbNewLine
  Dim e As ADODB.Error
  For Each e In CurrentProject.Connection.Errors
    strTmp = strTmp & "ADO:" & vbTab & e.Number & " - " & e.Description & _
          "(" & e.Source & ")" & vbNewLine
  Next
  strTmp = strTmp & vbNewLine & "<OK> to continue, <Cancel> to stop"
  If MsgBox(strTmp, vbOKCancel, "Error") = vbOK Then
    Resume Next
  Else
    Resume exitsub
  End If
End Sub


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint