Running a query in a different DB

aziz rasul

Active member
Local time
Today, 08:42
Joined
Jun 26, 2000
Messages
1,935
What code can you use to run a query that's in a different ms access database?
 
Dim DB As Database
Set DB = OpenDatabase("MyDataBase.mdb")
DB.Execute ("MyQuery")
DB.Close
 
Thanks for that Ken.

The query I wanted to run was a make table query, which created 'tblTemp'. When I ran your code it came with an error saying that 'tblTemp' already exists. How do I get around that?
 
Dim DB As Database
Set DB = OpenDatabase("MyDataBase.mdb")
DB.TableDefs.Delete ("tbltemp")
DB.Execute ("MyQuery")
DB.Close

???
 
Does tblTemp already exist in the other database at the time you run the query? If it does then of course you will get the error, you could check if it exists first, then delete it if it does:
Code:
Dim DB As Database
dim tdf as tabledef
Set DB = OpenDatabase("MyDataBase.mdb")

on error resume next
set tdf = db.tabledefs("tblTemp")

if not tdf is nothing then
    db.tabledefs.delete "tblTemp"
end if
on error goto 0

DB.Execute ("MAKE tblTemp query")
DB.Close

HTH,
Chris
 
Glad to help. And Chris's error checking stuff is a good idea - :)
 

Users who are viewing this thread

Back
Top Bottom