Using the value returned by a SQL Query in VBA

  • Thread starter Thread starter sbfq
  • Start date Start date
S

sbfq

Guest
Im new here on the forums, so please excuse me if a similar question has already been asked...

I am writing some VBA code and want to use the value returned by a SQL script in the VBA code. My idea is to run the sql script (which returns just 1 value) and then put that value into a variable. Then I can use the variable in the rest of my code.

Can someone help me please? Is this possible??
 
This may give you the idea.
Code:
Dim rst As DAO.Recordset
Dim strSQL As String
Dim varResult

strSQL = "SELECT MyData FROM MyTable"
Set rst = CurrentDb.OpenRecordset(strSQL)
If Not (rst.EOF And rst.BOF) Then
    varResult = rst!MyData
End If
Set rst = Nothing

Peter
 
As Bat said, or,

varResult = DLookUp("MyData","MyTable")
 

Users who are viewing this thread

Back
Top Bottom