IT tutorials
 
Office
 

Microsoft Access 2010 : DATA ACCESS WITH ADO (part 2) - Creating Recordsets

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
11/21/2011 10:05:59 AM

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.
 
Others
 
- Microsoft Access 2010 : DATA ACCESS WITH ADO (part 1) - Using the Execute Method
- Microsoft Word 2010 : Adding Supplementary Elements - Creating a Bibliography
- Microsoft Word 2010 : Adding Supplementary Elements - Figure Captions & Adding a Table of Figures
- Microsoft Visio 2010 : Tips for Creating Organizational Charts
- Microsoft Visio 2010 : Tips for Creating Timelines
- Microsoft PowerPoint 2010 : Prepare for Delivery - Rehearsing Presentations
- Microsoft PowerPoint 2010 : Prepare for Delivery - Adapting Presentations for Different Audiences
- Microsoft Excel 2010 : Analyzing Worksheet Data - Creating Groups and Outlines, Converting Text to Columns
- Microsoft Excel 2010 : Analyzing Worksheet Data - Charting a PivotTable
- Microsoft Outlook 2010 : Managing a Calendar - Setting Up a Meeting
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us