3. Creating Recordsets
The examples provided so far have returned Recordset objects that were created implicitly as a result of the actions executed on the data source. The Open method of the Recordset object is used to retrieve the records from a data source and store them in the Recordset object. This method takes five parameters: Source, ConnectionString, CursorType, LockType, and Options. The Open method can be used in several different ways and this section will discuss a couple of examples for creating Recordset objects.
3.1. Creating a Recordset from a SQL Statement
A very common way to create a Recordset object is to open it directly from a SQL statement. Simply call the Open method with the SQL statement string and the connection and a new Recordset will be created.
Public Function OpenRecordsetFromSql() As ADODB.Recordset
' Define Variables
Dim rs As New ADODB.Recordset
' Open the Recordset using a SQL statement
rs.Open "SELECT [CONTACTS].* FROM [CONTACTS]", CurrentProject.AccessConnection
' Return the Recordset
Set OpenRecordsetFromSql = rs
End Function
In this case, the CurrentProject connection was supplied, but we could have easily created a Connection object to the desired source and supplied that instead.
3.2. Creating a Recordset from a Table or View
Another common way to create a Recordset object is to open it as a table or view. Simply call the Open method with the table or view name string and a new Recordset will be created for that object:
Public Function OpenRecordsetFromTable() As ADODB.Recordset
' Define Variables
Dim rs As New ADODB.Recordset
' Open the Recordset using a SQL statement
rs.Open "Contacts", CurrentProject.AccessConnection
' Return the Recordset
Set OpenRecordsetFromView = rs
End Function
The SQL statement used here
is pretty run-of-the-mill, but ADO also provides a lot of flexibility
for very complex statements and record types, as we will show next.
3.3. Creating a Shaped Recordset
A powerful feature of ADO is the ability to create shaped Recordset objects using the SQL language. Data shaping
enables the developer to define the columns of a recordset, the
relationships between them, and the manner in which the recordset is
populated with data. This only works with providers that support
MSDataShape, such as SQL Server.
The next example uses two tables with a parent-child relationship: Orders and Order Details. This code
sample must be executed against a provider that supports the SQL Shape
statement, like the SQL Server provider. Closely examine the following
code, paying particular attention to the SQL statement:
Public Function OpenShapedRecordset() As ADODB.Recordset
' Define Variables
Dim strSQL As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
' Set the connection and open
cn.ConnectionString = "<Your SQL Server Connection String>"
cn.Provider = "MSDataShape" ' We are using the MS Shape provider
cn.CursorLocation = adUseClient
cn.Open
' Create the SQL statement that builds the shaped Recordset
strSQL = _
"SHAPE" & _
"{SELECT DISTINCT OrderID, CustomerID, OrderDate " & _
"FROM Orders " & _
"WHERE OrderID BETWEEN 50 AND 70 " & _
"ORDER BY OrderDate DESC} " & _
"APPEND(" & _
"{SELECT OrderID, ProductID, UnitPrice, Quantity " & _
"FROM [Order Details]} AS CustomerOrders " & _
"RELATE OrderID TO OrderID)"
' Open the Shaped Recordset
rs.Open strSQL, cn
' Return the Recordset
Set OpenShapedRecordset = rs
End Function
In this SQL statement, the parent record is selected and then the child records are appended as a child Recordset object. Shaped Recordsets such as this are called hierarchical Recordsets. They exhibit a parent-child relationship in which the parent Recordset is the container in which the child Recordset is contained.
3.4. Creating a Recordset Containing Multiple Recordsets
There are many cases in which a command will generate a result containing multiple Recordset objects. When the Recordset object is returned, the first Recordset is already referenced by default. Each additional Recordset can be accessed by calling the NextRecordset method, which will return the next Recordset if it exists.
Public Function CreateMultiRecordset() As ADODB.Recordset
' Define Variables
Dim rs As New ADODB.Recordset
Dim rsTemp As New ADODB.Recordset
Dim strSQL As String
' Create the Multi Recordset SQL Statement and open the Recordset
strSQL = "SELECT Prices.* FROM Prices; SELECT Contacts.* FROM Contacts"
rs.Open strSQL, "<Provider that supports commands returning multi Recordsets>"
' Get the Next Recordset returned from the command
Set rsTemp = rs.NextRecordset
' Return the Recordset
Set CreateMultiRecordset = rs
End Function
Note that this example requires that the provider support commands that return multiple Recordset
objects as a result, which the Access 2010 data provider does not.
However, this type of result is very common in standard SQL statements,
and the SQL Server provider often returns multiple Recordset objects in views and stored procedures.
3.5. Verifying Recordset Options
The Supports method is used to verify the features (Recordset methods) that a specific ADO data provider supports for a Recordset object, which is determined by the provider and cursor type. The Supports method takes one parameter: CursorOptions, which is specified as a member of the CursorOptionEnum object. The method returns True if the option is supported, otherwise False. Consider the following code:
Public Function SupportsFind() As Boolean
' Define Variables
Dim rs As New ADODB.Recordset
' Open the Recordset using to Contacts connection
rs.Open "Contacts", CurrentProject.Connection
' Return whether or not the Find method is supported
SupportsFind = rs.Supports(adFind)
End Function
In this case, the function should return True because the Find method is supported by the provider, which is Access.