DLookup() versus FindFirst on a Recordset

Access has a number of ways in which you can query your database. You could create a query using the easy-to-use QBE grid. Or you could use the DLookup() as one of your options.

DLookup() looks through your table, using any indexes if available, to help locate the first matching record. When the table contains many records (approx over 3000 records), a better method is to create a recordset and do a FindFirst on it using the matching criteria. Recordsets, however can be created through code only. So you may have a field in your form which looks up a value based on others and you may discover that writing code for it is not as simple as the DLookup() function. But it really is not all that difficult. Simply figure out which event you want to attach the look up to and in the code window write the following....

Dim db as database
Dim rsMySet as recordset
On Error Goto Module_Err

Set db = dbengine(0)(0)
Set rsMySet = db.OpenRecordset ("SELECT * FROM YourTable;")
RsMySet.FindFirst "YourColumn = 1000"
...
...
Module_Exit:
Exit_Sub

Module_Err:

Select case Err
Case 3021
	Msgbox "No records in your table..."
Case else
	Msgbox "Error " & Err & " hs occurred..."
End Select
Resume Module_Exit

This is just a basic outline to help you get familiar with manipulations using Access Basic.

See Also Doing FindFirst on a Table?!!!

Back to MS Access Tips

Perfect Paradigm - A Solutions Company