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

Problem with DoCmd.OpenQuery

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
From my current db, I open a form that exists in my library database (codedb). Upon pressing a button on the form, my code creates a query and saves it in my current db. I then want to open that query via DoCmd.OpenQuery "QueryName", acViewNormal. However, because the form and/or code is being executed within my library database (codedb or CodeProject), the DoCmd.OpenQuery is looking for the query in my library database rather than my current database.

What would be the syntax to force DoCmd.OpenQuery to run in my current database?

Here's a brief example of my code:
Code:
Dim dbs as DAO.Database
Dim qdf as DAO.QueryDef

Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef("TempQuery", "Select...")

DoCmd.OpenQuery "TempQuery", acViewNormal

I have tried:
CurrentProject.Application.DoCmd.OpenQuery "TempQuery", acViewNormal
AND
Dim app as New Access.Application
Set app = GetObject(CurrentProject.FullName)
app.DoCmd.OpenQuery "TempQuery", acViewNormal
 
I found a solution. Since DoCmd.OpenQuery first looks in the library database for the query and then in the current database, I use a different query name if my current database is my library database.

For example, if my current database is abc.accdb, then I name the query "TempQuery". If I'm running the library database directly (that is, the library database is my current database) then I name the query "TempQuery_Lib". Consequently, I have eliminated my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top