I typically use ADO (ActiveX Data Object) recordsets to get data via a query in code. Here is a code snippet:
Code:
Dim cnn1 As ADODB.Connection
Set cnn1 = CurrentProject.Connection
Dim myrecset As New ADODB.Recordset
myrecset.ActiveConnection = cnn1
mySQL= "SELECT ..."
myrecset.Open mySQL
If myrecset.BOF And myrecset.EOF Then
MsgBox "no records returned"
Else
Do Until myrecset.EOF
'do stuff here in the loop
myrecset.MoveNext
Loop
End if
The code snippet above sets up the objects and opens a recordset and then loops through the recordset. Obviously, you would do something within the loop.
This might be more than you need, and as I and Paul (pbaldy) mentioned, if you just want a couple of values from a record it would be easier using a DLookup() function.