PDA

View Full Version : import query from master db to array of db



richardSmith
07-24-2014, 02:44 PM
I have a master database that contains a query I need to import into 150 other databases. How can I use VBA to open master dB, open 1st db in array, import query, close 1st db in array, open 2nd db in array etc etc.

jonh
07-25-2014, 02:22 AM
Just to be clear. Do you want to export the query (definition) or the query result (data) to a new or existing table?

richardSmith
07-25-2014, 04:43 AM
Just to be clear. Do you want to export the query (definition) or the query result (data) to a new or existing table?
The definition only. The data itself should remain in the database. I just need the actual query itself. example, let's say I have query1 that is

Select vaccineGiven, vaccineGivenDate, GivenTo from vaccinesGiven

I want that query exported to all databases in the array and named query1 (just like it is in the source database) in all databases in the array.

jonh
07-25-2014, 06:43 AM
Obviously you'll need to change the path to suit your needs.


Private Sub Example()
Dim ws As DAO.Workspace
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("query1")
Set ws = DBEngine(0)

For i = 1 To 150
Set db = ws.OpenDatabase(CurrentProject.Path & "\db" & i & ".accdb")
db.CreateQueryDef qd.Name, qd.SQL
db.Close
Set db = Nothing
Next
End Sub