Access.2007.VBA.Bibl..

(John Hannent) #1
ErrorHandler:
MsgBox “Error No: “ & Err.Number _
& “; Description: “ & Err.Description
Resume ErrorHandlerExit
End Sub

Recordset ......................................................................................................

ADO recordsets represent sets of records in a database, much like DAO recordsets, though their
attributes are more generic. An ADO recordset can be based on a table, query, SQL statement, or
Command object. The TestForwardReadOnlyprocedure listed here uses a saved select query
as the recordset source:

Private Sub TestForwardReadOnly()

On Error GoTo ErrorHandler

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset

Create a connection to the current database.

Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset

Create a recordset based on a select query.

rst.Open Source:=”qryCompanyAddresses”, _
ActiveConnection:=cnn.ConnectionString, _
CursorType:=adOpenForwardOnly, _
LockType:=adLockReadOnly

Iterate through the query, and print values from fields to the Immediate window.

Do While Not rst.EOF
Debug.Print “Company ID: “ & rst![CompanyID] _
& vbCrLf & vbTab & “Category: “ _
& rst![Category] _
& vbCrLf & vbTab & “Company Name: “ _
& rst![Company] & vbCrLf
rst.MoveNext
Loop

ErrorHandlerExit:

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf