Replace Multiple DLookup Calls with a Recordset in Microsoft Access
Question details
Retrieve several fields from the same Access record without running a separate DLookup call for each field.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Access code or a form needs multiple values from a table using the same matching criteria.
- Observed behavior
- Repeated DLookup calls search the same table multiple times.
Identify the table, fields, and criteria used by the existing DLookup calls. For the VBA solution, check that the Microsoft Office Access database engine object library is available under Tools > References in the VBA editor.
Read multiple fields from a parameterized DAO recordset
Use this approach when VBA code needs several values from the same matching record.
A parameterized query supplies the criteria once and returns all selected fields in one recordset. If the criteria can match multiple records, define how to select the intended record rather than relying on an unspecified first match.
In Access, press Alt+F11, then open the module or event procedure that contains the DLookup calls.
Replace the example table, field names, parameter type, and value with your own. For example, use the following code for a numeric CustomerID: Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset Set db = CurrentDb Set qdf = db.CreateQueryDef("", _ "PARAMETERS [pID] Long; " & _ "SELECT [CompanyName], [City] FROM [Customers] " & _ "WHERE [CustomerID] = [pID];") qdf.Parameters("pID").Value = 123 Set rs = qdf.OpenRecordset(dbOpenSnapshot)
Check for a match before reading the fields, then release the objects. For example: If Not rs.EOF Then Debug.Print Nz(rs.Fields("CompanyName").Value, "") Debug.Print Nz(rs.Fields("City").Value, "") Else Debug.Print "No matching customer" End If rs.Close Set rs = Nothing Set qdf = Nothing Set db = Nothing
Run the procedure with a known ID and view the output in the VBA editor's Immediate window with Ctrl+G. Also test an ID with no match.

Bind a form or report to a query
Use a query as the record source when the values only need to be displayed.
Work with exported Access data in WPS Spreadsheets
WPS Office is a free, lightweight alternative for everyday office work, with a familiar interface and support for Microsoft Office formats. Export Access data to Excel to work with it in WPS; WPS does not replace Access databases, DAO, or forms.

Frequently Asked Questions
Why use a recordset instead of several DLookup calls?
A query can return all the required fields in one result, avoiding repeated domain-function searches. Open it as a DAO recordset when VBA needs to read the values.
What happens if the query finds no matching record?
Check rs.EOF before reading rs.Fields. If EOF is True immediately after opening the recordset, handle the missing match instead of attempting to read field values.
How do I display the values without writing VBA?
Create and test a query with the required fields and criteria, then set a form or report's Record Source property to that query and bind its controls to the fields.
Can WPS Office run an Access DAO recordset?
No. DAO recordsets and Access forms require Microsoft Access. You can export query results from Access to an Excel file and open that file in WPS Spreadsheets.




