assigning the result of a query to a declared variable in VBA code

A

Allen Browne

The result of a query is a complete recordset.

You can assign all the fields and records of the query to a Recordset
variable like this:
Dim rs As DAO.Recordset
Set rs = dbEngine(0)(0).OpenRecordset("Query1")

If you just wanted to assign a single value (one field from one record), you
could use DLookup(). Details in:
Getting a value from a table: DLookup()
at:
http://allenbrowne.com/casu-07.html

If you wanted to achieve the same functionality as DLookup() using a SQL
statement, this article explains how to do that:
ELookup - an extended replacement for DLookup()
at:
http://allenbrowne.com/ser-42.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top