Retrieving a Value from a Query

G

glnbnz

Hello,
I am trying to retrieve a value from a query. I have a form and when I
press a command button I would like the code to open a query, take the value
and place it into a variable then close the query.

Here is my code so far:

Dim sngValue As Single

DoCmd.OpenQuery "qryMyQuery", acViewNormal

sngValue = [Queries]![qryMyQuery]![SomeValue]

DoCmd.Close acQuery, "qryMyQuery", asSaveNo

Me.Text18.Value = sngValue

My problem lies in the 3rd line of the code and I can't seem to find the
correct syntax for obtaining "SomeValue" and storing it as "sngValue".
I know that I can do this a different way, but there is a method to my
madness where I am going to do a calculation from multiple queries.

Could someone help me with this?

Thank you.
 
G

glnbnz

Thanks Doug answered my question perfectly

Glenn

Douglas J. Steele said:
Replace what you have with


Me.Text18.Value = DLookup("[SomeValue]", "qryMyQuery")


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


glnbnz said:
Hello,
I am trying to retrieve a value from a query. I have a form and when I
press a command button I would like the code to open a query, take the
value
and place it into a variable then close the query.

Here is my code so far:

Dim sngValue As Single

DoCmd.OpenQuery "qryMyQuery", acViewNormal

sngValue = [Queries]![qryMyQuery]![SomeValue]

DoCmd.Close acQuery, "qryMyQuery", asSaveNo

Me.Text18.Value = sngValue

My problem lies in the 3rd line of the code and I can't seem to find the
correct syntax for obtaining "SomeValue" and storing it as "sngValue".
I know that I can do this a different way, but there is a method to my
madness where I am going to do a calculation from multiple queries.

Could someone help me with this?

Thank you.
 

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