Run Query from code

D

Darren

I need to run a query from code and be able to retrieve values from it.
Probably simple, but it's giving me a headache.

Thanks
 
R

Rick Brandt

Darren said:
I need to run a query from code and be able to retrieve values from
it. Probably simple, but it's giving me a headache.

Thanks

You would typically use the query to open a RecordSet object and then you can
retrieve the data from that in your code. VBA has no way to pull data from a
query directly other than to use DLookup() against it.
 
D

Darren

Actually, I figured it out. Thanks

Dim rs As DAO.Recordset
Dim db As Database
Dim qry As DAO.QueryDef

Set db = CurrentDb()
Set qry = db.QueryDefs("qryGetLastRegNum")
Set rs = qry.OpenRecordset
 
S

Steve Schapel

Darren,

That's probably more complicated than you need. This will do the job...

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryGetLastRegNum")
 
Top