Using Command or Recordset
Object
ADO also has a
short-hand method of opening a database connection that bypasses the connection
object altogether. Both the recordset and the command object will allow you to
pass a connection string instead of a connection object to their respective
ActiveConnection properties. Just set the Command/Recordset’s ActiveConnection
property to the desired connection string and the object is ready to
use.
Sub ADO_COMMAND_CONNECTION_TEST()
Dim cmd As New ADODB.Command
Dim rs As ADODB.recordset
Dim strConn As String
cmd.ActiveConnection = " DRIVER={SQL
Server};" & _
"Server=UKDUDE;DATABASE=pubs;UID=sa;PWD=;"
cmd.CommandText = "byroyalty"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1).Value = 25
Set rs = cmd.Execute
' Recordset now has authors with 25%
royalty.....
End Sub
Note that here I did not use the “Set”
keyword as in the previous example. I am assigning a string to a property which
is not the same as assigning a VBA object.
Note that this second method is actually a
more desired approach than explicitly using a connection object in code. In
general, most ODBC drivers do not support more than one active statement per
connection. This means that sharing a single Connection object over more than
one Command/Recordset object can cause errors due to multiple active statements
running. If the customer uses the connection string approach, then each
Command/Recordset object will have it’s own individual connection object
internally and “multiple active statements per connection” errors will be
avoided.
Sample ODBC and OLE
DB Connection Strings
Many customers are confused when using
ODBC and OLE DB connection strings. One of the most helpful articles I found to
explain various connection strings is the “Setting Connection String Parameters
in DAO” whitepaper.
http://msdn.microsoft.com
Here are some simple examples to connect
to various ODBC drivers and OLE DB Providers.
Microsoft Access
ODBC = “DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=C:\NW.MDB”
OLE DB =
“PROVIDER=Microsoft.JET.OLEDB.3.51;DATA SOURCE= C:\NW.MDB”
Microsoft SQL Server
ODBC = “DRIVER={SQL
Server};SERVER=MyServer;DATABASE=pubs;UID=xxx;PWD=yyy;"
OLE DB =
PROVIDER=SQLOLEDB;SERVER=MyServer;DATABASE=pubs;UID=xxx;PWD=yyy;"
Microsoft Oracle
ODBC = “DRIVER={SQL
Server};SERVER=MyServer;DATABASE=pubs;UID=xxx;PWD=yyy;"
OLEDB = “PROVIDER=MSDAORA;
SERVER=MyServer;DATABASE=pubs;UID=xxx;PWD=yyy;"
Microsoft Excel
ODBC= "Driver={Microsoft Excel Driver
(*.xls)};DBQ=C:\Book1.xls"
Microsoft FoxPro
ODBC=”DRIVER={Microsoft FoxPro Driver
(*.dbf)};DBQ=C:\FoxFiles;”
Using the
ADO Command Object
General Command
Object Issues
The ADO Command object is used when one
needs to provide parameter information to a stored procedure, a SQL DML
statement, or a SQL select statement. Parameters are indicated by embedding a
single question mark in the location when the parameter is desired. Parameters
can only replace literal values in a SQL statement, they cannot be used to
indicate a variable field names for example.
Here are some example SQL statements that
use parameters:
select * from authors where
au_id=’243-11-2334’
select * from authors where
au_id=?
insert into MyTable (field1,field2) values
(1,’hello’)
insert into MyTable (field1,field2) values
(?,?)
{call
MyStoredProcedure(‘la’,’dee’,’dah’)}
{call MyStoredProcedure(?,?,?)
Parameter markers are simply replaced from
left to right as encountered. The first parameter encountered is parameter 0
(parameter’s collection is zero based).
ADO provides an
“auto-populate” feature for parameters which is quite handy. If the driver
supports it, you can call ADO’s Parameters.Refresh method and this will
automatically build the parameters collection for you (rather than manually
adding each parameter one at a time programmatically).
Sub ADO_PARAM_TEST()
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.recordset
cmd.ActiveConnection = "Driver={SQL Server};"
& _
"Server=UKDUDE;DATABASE=pubs;UID=sa;PWD=;"
cmd.CommandText = "select * from authors
where au_id=?"
cmd.CommandType = adCmdText
cmd.Parameters.Refresh ‘ Auto-populate
here…
cmd.Parameters(0).Value =
"213-46-8915"
Set rs = cmd.Execute
' Read record here...
End Sub
For more information on manually
populating the parameters collection, see the online examples in the ADO
documentation. Unfortunately, the Microsoft Access ODBC and OLE DB drivers do
not currently support automatic parameter population. This has been submitted as
a feature request for future versions of the driver.
Using Stored
Procedures with Command Objects
When calling stored procedures using the
Command object, set the Command’s CommandText to just the name of the stored
procedure, then set the CommandType property to the adCmdStoredProc constant to
let ADO now that the SQL statement in the CommandText property is a stored
procedure.
Sub ADO_STORED_PROC_TEST()
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.recordset
cmd.ActiveConnection = "Driver={SQL Server};"
& _
"Server=UKDUDE;DATABASE=pubs;UID=sa;PWD=;"
cmd.CommandText = "byroyalty"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
' Skip parameter 0 which is the return
value!
cmd.Parameters(1).Value = 25
rs.Open cmd, , adOpenStatic,
adLockOptimistic, -1
End Sub
You can determine which parameters are
bound by running the code example in the following KB article against your
particular stored procedure:
HOWTO: Determine How ADO Will Bind
Parameters
http://support.microsoft.com/kb/q181199/
Using the
ADO Recordset Object
General Recordset
Issues
The ADO Recordset object is very similar
to the Recordset objects in Microsoft’s previous database API’s. The Recordset
has the concept of a “current record” or “record pointer” which points to the
currently selected record. The programmer can move the current record pointer
forwards and backwards by using the MoveNext and MovePrevious methods. In most
cases the developer simply loops through a set of records until the Recordset
EOF flag is set to True.
Dim rs As New ADODB.Recordset
rs.ActiveConnection = "Driver = {SQL
Server};" & _
"Server=UKDUDE; DATABASE=pubs; UID=sa;
PWD=;"
rs.Open “select * from authors”
While Not rs.EOF
‘ Process record here…
Rs.MoveNext
Wend
The developer can also use the Fields
collection to dynamically gather more information about the various columns in
the recordset.The Fields collection of the Recordset object allows the “For
Each” VBA collection syntax which makes coding quite efficient.
|