whats the point of the command object in the first code

philfer

Registered User.
Local time
Yesterday, 19:54
Joined
Dec 15, 2008
Messages
29
Hello,

I have these two pieces of code in a book :-

1)
Dim cmd1 As ADODB.Command
Dim rst1 As ADODB.Recordset

Set cmd1 = New ADODB.Command

With cmd1
.ActiveConnection = CurrentProject.Connection
.CommandText = "SELECT * FROM mytbl;"
.CommandType = adCmdText
End With

Set rst1 = cmd1.Execute

Do Until rst.EOF
Debug.Print ......
Loop

2)

Dim rst1 As ADODB.Recordset
Set rst1 = New ADODB.Recordset

rst1.Open "SELECT * FROM mytbl;"

Do Until rst.EOF
Debug.Print ...
Loop

My question is what is the point of using Command in the first piece of code when it seems to function in exactly the same way without it in the second piece of code

Thanks
Phil
 
Frankly, I never use the command object where I've used ADO so I have the same question - why use it at all? I know in the VB6 world it is more important, but in the VBA world it seems superfluous.
 
Well, to be pedantic- when you do the second block of code, Command object is created for you implicitly. This is one of ADO's flexibility- it creates whatever is missing for you behind the scenes.

I would create a command object if I need it to have different default from the implicit command object created by ADO, to execute a stored procedure, or other query that doesn't return a recordset.
 

Users who are viewing this thread

Back
Top Bottom