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

Referencing a quert

Status
Not open for further replies.

asif79

Programmer
Oct 18, 2002
12
US
Hi all
This is the first time I am writing on this forum. I am trying to refer to a query in my vba code but it does not work. Do I have to create a connection even though the query is in the current database. Any help in this regard will be appreciated.

Asif
 
If you have a variable mstrQuery and fill it with the query name this would work.

CurrentDb.Execute mstrQuery -------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
Thanks scking
I tried it but it says that it cannot execute a select query. And is there any way that I can open the query as a record set.

Asif
 
Sorry, but I gave you the execute for running the action query. The following will run a select query. I'm doing it with a variable but if you replace the mstrQuery with the "YourQueryName" for the name of your query that would work also.

DoCmd.OpenQuery QueryName:=mstrQuery, View:=acViewNormal

'To open as a recordset
Dim rstIN As DAO.Recordst
Set rstIN = CurrentDb.OpenRecordset(mstrQuery)
If rstIN.RecordCount <> 0 Then
' proceed
Else
Exit Sub
End If

-------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
Hi

Thanks for all your help and it works now. I really appreciate it.

Asif
 
hi scking..
One more thing. I want to populate my form with records in the query and it does not allow me to navigate the records. My code is as follows:

Dim rstIN As DAO.Recordset
Set rstIN = CurrentDb.OpenRecordset(&quot;test&quot;)

Form_Form1.Text2 = rstIN!studentID
Form_Form1.Text0 = rstIN!courses
rstIN.MoveNext

Sorry to bother you but i really appreciate it.

asif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top