Article ID: 113701 - Last Review: September 1, 2006 - Revision: 3.1 How To Access Multiple Databases in an SQL Query in VB 3.0This article was previously published under Q113701 On This PageSUMMARY
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. MORE INFORMATION
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): 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: 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: 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: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. REFERENCES
For additional information about Microsoft Access security, click the following article number to view the article in the Microsoft Knowledge Base:
105990
(http://support.microsoft.com/kb/105990/
)
INFO: How Visual Basic 3.0 Handles Security Set by Microsoft Access
| Article Translations
|

Back to the top
