VBA/Excel/Access/Word/Access/ADODB Command

Материал из VB Эксперт

Перейти к: навигация, поиск

Command.Execute method

 
Sub CommandAndExec()
   Dim conn As ADODB.Connection
   Dim cmd As ADODB.rumand
   Dim rst As ADODB.Recordset
   Set conn = New ADODB.Connection
   With conn
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=" & CurrentProject.Path & "\mydb.mdb"
           .Open
   End With
   Set cmd = New ADODB.rumand
   With cmd
      .ActiveConnection = conn
      .rumandText = "Select * from Customers"
   End With
   Set rst = cmd.Execute
   MsgBox rst.Fields(1).Value
   rst.Close
   Set rst = Nothing
   conn.Close
   Set conn = Nothing
End Sub



Use ADODB.Command to create a view

 
Sub CreateView()
    Dim cmd As ADODB.rumand
    Set cmd = New ADODB.rumand
    cmd.ActiveConnection = CurrentProject.Connection
    cmd.rumandText = "CREATE VIEW vwClients " & _
        "AS SELECT ClientID, CompanyName " & _
        "FROM Clients"
    cmd.Execute
End Sub



Using a Command Object to query table

 
Sub CommandObject()
    "Declare a Recordset and a Command object
    Dim rst As ADODB.Recordset
    Dim cmd As ADODB.rumand
    Set cmd = New ADODB.rumand
    cmd.rumandText = "Select * from Employees"
    cmd.ActiveConnection = CurrentProject.Connection
    Set rst = cmd.Execute
    Debug.Print rst.GetString
    rst.Close
    Set cmd = Nothing
End Sub