Need help accessing a query table from code

ZanyJanie

Registered User.
Local time
Today, 11:04
Joined
Mar 27, 2001
Messages
30
Hello,

I'm a hopeless newbee when it comes to writing VBA code; so I could really use your help.

All I want to do is write code to read in the results from an existing query. For the sake of example, my query is named "my-Qry"

Below is the code I've written; but when I try to run it, I get a compile error saying "External table name not defined" The error indicated that the compiler doesn't recognize "my-Qry"

Can someone PLEASE tell me what I'm doing wrong here?

Thanks in advance!
Jane

------Code Sample -------

Public Sub ReadQueryTable()

Dim db As DAO.Database
Set db = CurrentDb

Dim QryRs As DAO.Recordset
Set QryRs = db.OpenRecordset([my-Qry], dbOpenSnapshot)

Do Until QryRs.EOF
Debug.Print QryRs.Fields(0)
QryRs.MoveNext
Loop

End Sub
 
Janie,
Replace the square brackets around my-Qry with double quotes. Also, the dash in the name is not good programming practice. Whilst it will work, an underscore is much better. Punctuation in names means having to place square brackets around them all the time. In complex statements the prolification of brackets tends to make things a bit confusing.
Jon
 
Jon,

Thanks for your reply. I tried my code with the double quotes and it worked! YEAH! Also, thanks for the info about punctuation. As you can tell, I've got a long way to go; but this site has some great examples and super nice users.

Janie
 

Users who are viewing this thread

Back
Top Bottom