Beginner Code and Query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Need a jump start here.
What is the syntax for calling a query in VB and how do you then reference
the various fieldsin the query.I am running Access 2002.

Query name: Test
Filed names: f1, f2, f3


tHANKS;
STEVE
 
Set a recordset object equal to the query and then reference the fields.
****untested air code, but you get the idea*** :)

Sub TestRecordset()
Dim rst as DAO.Recordset
Dim db as DAO.Database
Set db=CurrentDB
Set rst=db.OpenRecordset("Test", dbOpenSnapshot)
'dbOpenSnapshot is to open read-only so you can't accidentally
'alter any data in the recordset. Use 'dbOpenDynaset' if you want
'to make changes to the data
With rst
.MoveFirst
'print contents of field f1 to the immediate window
Debug.Print !f1
'print contents of f2 and f3 separated by a space
Debug.Print !f2 & " " & !f3
End With
Set rst=Nothing
Set db=Nothing
End Sub
 
Hi Mark,
That was it . Thanks
Mark said:
Set a recordset object equal to the query and then reference the fields.
****untested air code, but you get the idea*** :)

Sub TestRecordset()
Dim rst as DAO.Recordset
Dim db as DAO.Database
Set db=CurrentDB
Set rst=db.OpenRecordset("Test", dbOpenSnapshot)
'dbOpenSnapshot is to open read-only so you can't accidentally
'alter any data in the recordset. Use 'dbOpenDynaset' if you want
'to make changes to the data
With rst
.MoveFirst
'print contents of field f1 to the immediate window
Debug.Print !f1
'print contents of f2 and f3 separated by a space
Debug.Print !f2 & " " & !f3
End With
Set rst=Nothing
Set db=Nothing
End Sub
 
Back
Top