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

Pull field from table with SQL, then put into variable with VBA

Status
Not open for further replies.

ShaneJones

Programmer
May 6, 2002
6
US
I am basically trying to get the first value in the first field of a table into a variable that I can use. I have been trying to manipulate the SQL query and play with that, but have come to a brick wall. Any suggestions on how to get the SQL to work or a new directoin would be greatly appreciated.
 
Shane,
The first value of the first field can be retrieved with DLookup
try:
Code:
Dim MyVariable as String

MyVariable = Cstr(DLookup("[My_Field_Name]", "MyTable"))

MyVariable = "The value of MyVariable is " & MyVariable

Msgbox MyVariable

hth
Scoty ::) "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Scoty
We are getting closer. When I use the Dlookup, I get a compile error saying "Invalid outside procedure". Any chance for help out of this one. I am still a newby, and am stuck again.
Shane
 
Shane
on what line of code is the program bombing on?
Scoty "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Scoty
Using this example, the code errors at My_Field_Name. Obviously I have my data in here, but that is the place.

MyVariable = Cstr(DLookup("[My_Field_Name]", "MyTable"))
Shane
 
Shane
All I can say is make sure you have your field name in quotes and brackets. And if you are using a caption you should make sure you are using the actual field name. I have no other advise

Sorry
Scoty ::) "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top