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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing value from sql query

Status
Not open for further replies.

astraltango

Programmer
Joined
Nov 19, 2003
Messages
18
Location
AU
I want to run an SQL query to get a value from a table and store the result in a variable for further use.

How do I do this?

eg "select field1 from table1 where key = 121" may return one record: "fred" which I wish to store in a variable

I have found the following code in another thread, but I'm still a little confused on how to implement the "Execute" command.

Set rsMyValue = cnMyDB.Execute("Select Value from Table where key=2")
rsMyValue.MoveFirst
lngCurrentValue = rsMyValue.Fields("Value")

What is cnMyDB and what should I initialise it to?

Or maybe there is an easier way?

Cheers
 
If you are just returning a single value, a simpler way is to use the DLookup function. Look up the help for examples as it can be confusing.

strValue = DLookup("Field","TableName","Key=2")
 
thanks heaps, exactly what I need
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top