Run Query from code

  • Thread starter Thread starter Darren
  • Start date Start date
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
 
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.
 
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
 
Darren,

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

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