Article ID: 113701
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.
Attaching 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.
Method OneMicrosoft Access SQL provides an IN clause that allows you to connect to an external database (a database other than the current database). This method does, however, limit you to only one external database at a time.
The IN clause has two parts, database name and connect string. The database name is a fully-qualified path to the file or directory containing the database file and the connect string contains the database type and other parameters as needed.
To specify an external database, append a semicolon (;) to the connect part, and enclose it with single or double quotation marks. The following example uses the IN clause to specify a table (Customers) in a dBASE IV database (SALES):
In Visual Basic, you can create a dynaset from the above example by using the following Visual Basic code:
Method TwoThe Microsoft Access engine incorporated into Visual Basic version 3.0 can parse SQL queries to include the connect string used to open a database object. The From clause of the SQL statement accepts a fully qualified table name, which allows the placement of the connect string in square brackets before the table name. The connect string is separated from the table name by a period. This method allows you to connect to multiple external databases at the same time.
You can access any table in either of two databases inside a single select statement by using this syntax:
This example joins two tables from two different databases, one an ODBC data source and the other a dBASE III table in the directory C:\DBASE3.
In general, the connect string used here in square brackets is identical to the Connect property of a TableDef when attaching or the fourth parameter of the OpenDatabase statement. It will be in one of three forms depending on the database (ODBC, ISAM, or Microsoft Access).
For ODBC databases:
For ISAM databases:
For Microsoft Access databases:
Note the leading semicolon for Microsoft Access databases is important. This is exactly the same string needed to fill the Connect property of a TableDef object before attaching the table to a Microsoft Access format database. The leading semicolon is a place holder for the unneeded database format specification and allows the "database=" clause to follow.
Code Example of a Multiple Database QueryThis example creates a dynaset joining two tables from two data sources, one an SQL Server and the other a Microsoft Access database. The TestTab table is on the SQL Server and the T1 table is in the Microsoft Access database.
Note You must change <username> and <strong password> to the correct values before you run this code. Make sure that Username has the appropriate permissions to perform this operation on the database.
Special Note Concerning Secured Microsoft Access DatabasesIf the Microsoft Access database is secured, the Visual Basic application must execute the SetDataAccessOption and SetDefaultWorkspace commands before executing any data access related code. This is required for a successful logon because Microsoft Access does not use the "Username=" and "PWD=" sections of the connect string. For example:
When this is done, queries to the secured Microsoft Access database will succeed. However, note that because of this process, there is a built-in limitation for this ad hoc technique; only one secured Microsoft Access database can be accessed with an ad hoc query. This is because once the Microsoft Access engine is initialized in a session, with a particular user name and password combination, those values are retained until the session (Visual Basic executable program or session of the environment, VB.EXE) ends.
However, if more than two secured Microsoft Access databases need to be accessed for a query, the best approach is to move the actual tables from secured databases into one secured database. To do this, you need to change the password for the admin account to "" temporarily during the transfer operations. Then you could use Visual Basic code, such as that in the data access sample Visdata, to copy the tables.
For additional information about Microsoft Access security, click the following article number to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/105990/ )INFO: How Visual Basic 3.0 Handles Security Set by Microsoft Access
Article ID: 113701 - Last Review: June 17, 2014 - Revision: 4.0