| HOWTO: Access Multiple Databases in an SQL Query in VB 3.0ID: Q113701 
 | 
When accessing data from more than one data source simultaneously in a single query, you will probably want to use the fastest and most general method -- attaching the tables from the different data sources to a single Microsoft Access database. At that point, queries that span two different databases can be constructed as if all the tables, attached or local, were local to the Microsoft Access database. For more information on attaching tables, please see the following article in the Microsoft Knowledge Base:
Q108423 How to Attach an External Database Table to a VB 3.0 DatabaseAttaching tables has powerful performance and administrative advantages -- especially if you are executing queries repeatedly. However, sometimes you might want to take the slower route described in this article when you need to use ad hoc queries that encompass two or more databases. This article explains how to construct these slower cross-database queries.
There are two methods you can use to specify a database outside the one that's currently open.
SELECT Customers.CustomerID
FROM Customers
IN "C:\DBASE\DATA\SALES" "dBASE IV;"
WHERE Customers.CustomerID Like "A*" 
Dim db As Database
Dim ds As Dynaset
Dim sql As String
' Open a database:
Set db = OpenDatabase("C:\VB\BIBLIO.MDB")
' Build the select statement, referencing the external dBASE IV file:
sql = "SELECT Customers.CustomerID, FROM Customers"
sql = sql & " IN 'C:\DBASE\DATA\SALES' 'dBASE IV;'"
sql = sql & " WHERE Customers.CustomerID Like 'A*'"
' Create the recordset:
Set ds = db.CreateDynaset(sql)
' Loop through and display the records:
While Not ds.EOF
   For i = 0 To ds.Fields.Count - 1
      Print ds(i); "  ";
   Next i
   Print
   ds.MoveNext
Wend 
SELECT tbl1.fld1,tbl2.fld1
FROM [odbc;dsn=datasource;database=pubs;uid=sa;pwd=].tbl1,
[dbase iii;database=C:\DBASE3].tbl2
WHERE tbl1.fld1 = tbl2.fld1 
   [odbc;dsn=datasource;database=thedatabasename;uid=sa;pwd=<password>] 
   [paradox 3.X;database=C:\DATABASE\PARADOX3]
   [foxpro 2.5;database=C:\DATABASE\FOX25]
   [dbase iv;database=C:\DATABASE\DBASEIV]
   [btrieve;database=C:\DATABASE\BTRIEVE\FILE.DDF] 
   [;database=C:\VB\BIBLIO.MDB] 
Dim db As database
Dim ds As dynaset
Dim sql As String, Uid$, Pwd$
Set db = OpenDatabase("C:\VB\BIBLIO.MDB")
' This obtains a valid database object. It does not have to be a Microsoft
' Access database; the following works equally as well:
' Set db = OpenDatabase("C:\FOXPRO25\", 0, 0, "foxpro 2.5")
' The values here are hard-coded, but you could prompt the user for their
' user id and password.
Uid$ = "sa"
Pwd$ = ""
' Build the select statement, concatenating the user's id and password:
sql = "SELECT T1.F2, TestTab.F2, TestTab.F3"
sql = sql & " FROM [;database=C:\ACCESS\DB1.MDB].T1 , "
sql = sql & " [odbc;dsn=texas;database=playpen;uid=" & Uid$
sql = sql & ";pwd=" & Pwd$ & "].TestTab"
sql = sql & " WHERE T1.F1 = TestTab.F1"
' Execute the select query:
Set ds = db.CreateDynaset(sql)
' Loop through and display the records:
While Not ds.EOF
   For i = 0 To ds.Fields.Count - 1
      Print ds(i); "  ";
   Next i
   Print
   ds.MoveNext
Wend 
   ' Establish the location of the SYSTEM.MDA files if in another
   ' directory other than the \WINDOWS directory:
   SetDataAccessOption 1, "C:\MYDIR\MYAPP.INI"
   ' Log on to a valid account:
   SetDefaultWorkspace "admin", "<password>" For additional information on Microsoft Access security, please see the following article in the Microsoft Knowledge Base:
Q105990 How Visual Basic Handles Security Set by Microsoft Access
Additional query words:
Keywords          : kbAccess kbSQL kbVBp300 kbGrpVB kbDSupport 
Version           : WINDOWS:3.0
Platform          : WINDOWS 
Issue type        : kbhowto Last Reviewed: July 20, 1999