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

Move results of a vba called sql query into a variable 1

Status
Not open for further replies.

ordendelfai

Technical User
Nov 8, 2002
187
US
Hello,

I am very new to VBA, and am having trouble getting this to work.

I want to take the returned value (1 result) of the below SQL statement, and assign it to a variable in VBA.

I have tried:
Private Sub Test()
Dim vProd1
vProd1 = "SELECT TOP 1 [Prod1] FROM
tbl_Client_Employee_Contrib ORDER BY [Prod1];"
End Sub


But that doesn't work at all, vProd1 just returns the sql string itself.

If you can help, would really appreciate! ;0)

~Orden
 
hi,

try this

Private Sub Test()
Dim vProd1
Dim rst as Recordset

set rst=CurrentDb.openRecordset("SELECT TOP 1 [Prod1] " _
& "FROM tbl_Client_Employee_Contrib ORDER BY [Prod1];")
if not rst.EOF then
vProd1 = rst!Prod1
else
'put here your error Code because the table is empty
end if
rst.Close
set rst=Nothing
End Sub
 
Hi ordendelfai,

All you are doing is assigning a string to a variable in VBA code (your instructions on what data to get); you have to pass it to Access so it can follow those instructions. That is what jcarneir's code does by using the recordset.

The Recordset approach is the more general one and one you must understand it but in your particular example you can do ..

Code:
VProd1 = DMax("Prod1","tbl_Client_Employee_Contrib")

Enjoy,
Tony
 
Tony, awesome, thank you, heres a star. Appreciate you trying to help jcarneir. As tony mentioned, your code did not produce the results I was looking for.

Thanks for helping =)

~Orden
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top