
Problem using the ADO Command object
Delving now into ADO I am looking at using the Command object to execute SQL
queries. I have trouble getting them to work.
The following code, using the Open method of a recordset, works fine,
returning the
correct number of records (in objRs.RecordCount) from the database table in
the recordset:
Dim objConn As ADODB.Connection
Dim objRs As ADODB.Recordset
Dim sqlStr As String
Set objConn = New ADODB.Connection
Set objRs = New ADODB.Recordset
objConn.Provider = "Microsoft.Jet.OLEDB.4.0"
objConn.Open "C:\Inetpub\Databases\Timecard\Tdata2.mdb"
sqlStr = "SELECT * FROM tdata2 WHERE BADGENUM = 'R21010'"
' this open works fine, returns RecordCount with correct number ...
objRs.Open sqlStr, objConn, adOpenStatic, adLockOptimistic
objRs.Close
objConn.Close
Set objRs = Nothing
Set objConn = Nothing
However, the following code, doing the same except using the Command Object,
returns
a record count of -1 (obviously an error indication):
Dim objConn As ADODB.Connection
Dim objCmd As ADODB.Command
Dim sqlStr As String
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command
objConn.Provider = "Microsoft.Jet.OLEDB.4.0"
objConn.Open "C:\Inetpub\Databases\Timecard\Tdata2.mdb"
sqlStr = "SELECT * FROM tdata2 WHERE BADGENUM = 'R21010'"
objCmd.CommandType = adCmdText
objCmd.CommandText = sqlStr
objCmd.ActiveConnection = objConn
' this call returns objRs with RecordCount set to -1 ...
Set objRs = objCmd.Execute
objRs.Close
objConn.Close
Set objRs = Nothing
Set objCmd = Nothing
Set objConn = Nothing
Can anyone tell me why this seemling simple use of the ADO Command object
does
not work as expected? Are there some other attributes of the Command object
I need
to set up first, or what?
TIA - Regards,
Carl