HandsOn Moving Around in a Recordset

1. In the Visual Basic Editor window, choose Insert | Module.

2. In the module's Code window, type the MoveAround procedure shown below.

3. Choose Run | Run Sub/UserForm to execute the procedure.

Sub MoveAround()

Dim conn As ADODB.Connection Dim rst As ADODB.Recordset Dim fld As ADODB.Field Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & CurrentProject.Path & _ "\Northwind.mdb"

Set conn = New ADODB.Connection conn.Open strConn

Set rst = New ADODB.Recordset rst.Open "Select * from Customers where ContactTitle = 'Owner'", _

conn, adOpenForwardOnly, adLockReadOnly Do While Not rst.EOF

For Each fld In rst.Fields

Debug.Print fld.Name & " = " & fld.Value Next rst.MoveNext Loop rst.Close Set rst = Nothing conn.Close Set conn = Nothing End Sub

0 0

Post a comment