Running a query from another database

Hi all,

Is it possible to run a query from another database.

I have a make table query in database mdb_A. The table is linked to another database mdb_B and I was wondering whether I could run that make table query from mdb_B

If so what code or procedure could I do?

I am using A97 but I would welcome assistance also in A03

Thanks
PipMicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IrogSintaCommented:
Yes you can.  Your code in database B would be similar to this:

    Dim wsp As Workspace
    Dim db2 As DATABASE
    
    ' Return reference to default workspace.
    Set wsp = DBEngine.Workspaces(0)
    ' Return reference to Another.mdb.
    Set db2 = wsp.OpenDatabase("c:\FolderPath\databaseA.mdb")
    
    'To run a query in the other database
    db2.QueryDefs("NameOfQuery").Execute

    'Sample statement to execute a sql string in the other database
    db2.Execute ("Delete * From TableName")

Open in new window

0
PipMicAuthor Commented:
Thanks...

Would you be able to explain each line in order to understand it.

Can this be done in A97 and/or A03
0
IrogSintaCommented:
It can be run on either.  I added a comment before each line.  Which one in particular did you need help understanding?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

PipMicAuthor Commented:
hi,

thanks for your prompt rely...

but

how can apply this code to say A.mdb has the make table query but I also want to run that same query, independently from B.mdb

    Dim wsp As Workspace ******************don't understand?
    Dim B.mdb As DATABASE
   
    ' Return reference to default workspace.
    Set wsp = DBEngine.Workspaces(0) ************************ don't understand

    ' Return reference to Another.mdb.
    Set B.mdb = wsp.OpenDatabase("c:\FolderPath\A.mdb")
   
    'To run a query in the other database
    A.mdb.QueryDefs("NameOfQuery").Execute

    'Sample statement to execute a sql string in the other database
    B.mdb.Execute ("Delete * From TableName")

    'Sample statement to execute a sql string in the other database
    B.mdb.Execute ("Delete * From TableName") *************don't understand why delete???


Am I on the right  track?
0
IrogSintaCommented:
The changes you made are incorrect:  I've highlighted the ones you actually need to change:

    Dim wsp As Workspace
    Dim db2 As DATABASE
   
    ' Return reference to default workspace.
    Set wsp = DBEngine.Workspaces(0)

    ' Return reference to Another.mdb.
    Set db2 = wsp.OpenDatabase("c:\FolderPath\A.mdb")
   
    'To run a query in the other database
    db2.QueryDefs("Name of query in A.mdb that you want to execute").Execute

    'I only included this Sample SQL statement to show how how you would execute a sql string in another database.  The Delete statement is only an example.  You can use other SQL statements (i.e create, insert, update, etc.).
    db2.Execute ("Delete * From TableName")
0
IrogSintaCommented:
DBEngine.Workspaces(0) refers to the default workspace of the database you have open. A Workspace is akin to a session for the user and you can have multiple databases within that workspace.
 
One way to think about it is a Workbook in Excel that contains many Worksheets.  Not quite the same since you don't really see the Workspace or the other databases you opened in this workspace.
0
hnasrCommented:
Assumptions:
Database1: A
database2: B
 B has make table query:mt
Both databases A and B reside on drive D:

A is running: code in cmdCheck click event to run mt in B

Private Sub cmdCheck_Click()
    Dim ap2 As Access.Application ' ap2 is an access application objects.
    Dim db1 As Database, db2 As Database  ' db1, and db2 are database objects.

    Set ap2 = CreateObject("Access.Application")

    Set db1 = Application.CurrentDb ' this is A
    ap2.OpenCurrentDatabase ("D:\B.accdb") ' opens B in background.
    Set db2 = ap2.CurrentDb ' this is B

    db2.Execute ("mt") ' runs make table query mt in database B
Exit Sub

Open in new window

0
IrogSintaCommented:
@hnasr,
What is the purpose of line 7 since you aren't making use of db1?

Ron
0
hnasrCommented:
@IrogSinta,
This is a copy and paste operation from an existing code. It is not used for this issue.

The purpose of db1 in original code is to compare between objects in both db1 and db2.
0
PipMicAuthor Commented:
now I am completely lost?!!!???
0
hnasrCommented:
You should not.
Try this sample. Save on drive D:, otherwise change drive letter in code.

Check code to remove messages and to modify the visibility of db_B.
db-A.accdb
db-B.accdb
0
PipMicAuthor Commented:
I'm using A97 or A03 not A07
0
hnasrCommented:
Try this sample. Save on drive D:, otherwise change drive letter in code.

Check code to remove messages and to modify the visibility of db_B.
db-A.mdb
db-B.mdb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PipMicAuthor Commented:
Did exactly what I wanted...many thanks
0
hnasrCommented:
Welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.