簡単なコマンドの作成と実行
適用先: Access 2013、Office 2013
Command オブジェクトの一般的な使用方法ではありませんが、次のコードに、 Command オブジェクトを使用してデータ ソースに対するコマンドを実行する基本的な方法を示します。 この場合、行を返すコマンドなので、コマンドの実行結果を Recordset オブジェクトに返します。
'BeginBasicCmd
On Error GoTo ErrHandler:
Dim objConn As New ADODB.Connection
Dim objCmd As New ADODB.Command
Dim objRs As New ADODB.Recordset
objCmd.CommandText = "SELECT OrderID, OrderDate, " & _
"RequiredDate, ShippedDate " & _
"FROM Orders " & _
"WHERE CustomerID = 'ALFKI' " & _
"ORDER BY OrderID"
objCmd.CommandType = adCmdText
' Connect to the data source.
Set objConn = GetNewConnection
objCmd.ActiveConnection = objConn
' Execute once and display...
Set objRs = objCmd.Execute
Debug.Print "ALFKI"
Do While Not objRs.EOF
Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _
objRs(2) & vbTab & objRs(3)
objRs.MoveNext
Loop
'clean up
objRs.Close
objConn.Close
Set objRs = Nothing
Set objConn = Nothing
Set objCmd = Nothing
Exit Sub
ErrHandler:
'clean up
If objRs.State = adStateOpen Then
objRs.Close
End If
If objConn.State = adStateOpen Then
objConn.Close
End If
Set objRs = Nothing
Set objConn = Nothing
Set objCmd = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
'EndBasicCmd
実行するコマンドは、 CommandText プロパティで指定します。
注:
このセクションのいくつかの例では、ユーティリティ関数 GetNewConnection を呼び出して、データ プロバイダーとの接続を確立します。 冗長性を回避するために、一度だけ一覧表示されます。
'BeginNewConnection
Private Function GetNewConnection() As ADODB.Connection
Dim oCn As New ADODB.Connection
Dim sCnStr As String
sCnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _
"Integrated Security='SSPI';Database='Northwind';"
oCn.Open sCnStr
If oCn.State = adStateOpen Then
Set GetNewConnection = oCn
End If
End Function
'EndNewConnection