Run query from VBA

  • Thread starter Thread starter AJ
  • Start date Start date
A

AJ

I have a query in my DB that returns 1 value. I would like to run it in VBA
and assign it to a variable. I thought this would be easy but cannot seem to
figure out how. Can it be done without a full recordset?
Example:
HV_QUERY = (results from query)

Thanks in advance.
 
There are other methods but a simple work-around would be to create a form
based on the query and place it in your main form as a subform (don't link
with master and child). Requery the subform and use

Me.somecontrol = Forms!MainFormName!SubFormName.Form!ControlOnSubForm

You can set the visible to no if you want for the new sub

Good luck
 
I have a query in my DB that returns 1 value. I would like to run it in VBA
and assign it to a variable. I thought this would be easy but cannot seem to
figure out how. Can it be done without a full recordset?
Example:
HV_QUERY = (results from query)

Thanks in advance.

HY_QUERY = DLookUp("[fieldname]", "[queryname]")

VBA has no way to know if the query returns one field or 255, or one record or
32844126 records. Therefore you can't just refer to the query name!

DLookUp will retrieve the first record (in this case the only record) if you
leave out the optional third parameter.

John W. Vinson [MVP]
 

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

Back
Top