HandsOn Finding a Record Based on Multiple Criteria

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

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

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

Sub Find_WithFilter()

Dim conn As ADODB.Connection Dim rst As ADODB.Recordset

Set conn = New ADODB.Connection conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & CurrentProject.Path & _ "\Northwind.mdb"

Set rst = New ADODB.Recordset rst.Open "Employees", conn, adOpenKeyset, adLockOptimistic rst.Filter = "TitleOfCourtesy ='Ms.' and Country ='USA'" Do Until rst.EOF

Debug.Print rst.Fields("LastName").Value

Part II

rst.MoveNext Loop rst.Close Set rst = Nothing conn.Close Set conn = Nothing End Sub

0 0

Post a comment