Retrieving a Value from a Query

  • Thread starter Thread starter glnbnz
  • Start date Start date
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.
 
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.
 
Back
Top