Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA: how to asign a value from a table to a variable 1

Status
Not open for further replies.

SashaBuilder3

Programmer
Jan 13, 2002
129
CA
Hi everybody,

I am wondering if there is a way to get a value from a table via SELECT statement and assign it to a variable in a VBA sub. For example, in Oracle it is possible to do the following:

SELECT SomeField INTO VarName WHERE...


Can anyone help with something similar for Access VBA?


Thanks

Alexandre

 
This obviously isn't SQL but in the meantime here's one way to do it.

VarName = DLookup("[SomeField]", "SomeTable", "[AnotherSomeField] = SomeCriteria")
 
The DLookup solution looks good. However, if you haven't made a query from your SQL statement, you can try this approach:

However, you can say something like:
Dim MyVariable as variant
Dim MyRst
Set MyRst = CurrentDb.OpenRecordset(&quot;SELECT * FROM <tablename> WHERE <condition>&quot;)
MyRst.MoveFirst
MyVariable = MyRst(0)
MyRst.Close
Set MyRst = Nothing
 
Billpower, Beetee,

Thanks for your help. I knew about the recordset way but it seemed to me a bit cumbersome to get just one value from a table. And I suspected there should have been another way. This DLookup function looks like what I was after.

Thanks a lot again!

Alexandre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top