Introduction.
Opening external data sources like dBase, Excel, and Table from another MS-Access Database directly in VBA Code is only one of the options available to us. Those who are not comfortable with VBA have a better and much easier method available, without linking them permanently to MS-Access Database. In either case, one thing is certain, we must know how to reference different external data sources correctly with Source Database Path and Connection String Values. These methods are very easy to learn.
We may face little difficulty in finding ODBC (Open Database Connectivity) Connection string Syntax, but this also we can find out by going through some shortcut tricks. Let us not mix this with the ODBC part now with our current example and we will deal with it later.
We have seen that we can display Values from Excel Cells or Range directly on MS-Access Form controls.
After going through the earlier examples for opening Access Table, dBase Table, and Excel tables directly in VBA, I hope at least a few readers thought of copying the SQL string presented in the VBA Code into a Query and trying them out. If not, it is time to do that. It is a better option to understand them and to look into a few things associated with their usage.
Open Recordset in Select Query, from External Access Database
To start with, let us try to open an MS-Access Table from another Database directly in a Select Query.
- Open one of your Databases or create a new one.
- Select the Query Tab and Click the New Button on the Database Menu. Select Design View from the displayed Options and Click Close without selecting any Table from the list.
- Click on the SQL Toolbar Button or select SQL View from View Menu.
- Copy and Paste the following SQL String in the SQL Editing Window, Save the Query with the Name EmployeesQ.
SELECT Employees.* FROM Employees IN 'C:\Program Files\MicrosoftOffice\Office\samples\Northwind.mdb';
Note: If you are using MS-Access2003 then change the Path to . . .\Office11\Samples\.
- Open the Query in Datasheet View (View - -> Datasheet View) to display the Employees Table contents from the Sample Database Northwind.mdb.
In the SQL String, an IN Clause is used for pointing to the correct database path, and the entire Path Name is put in quotes and ends with a semi-colon indicating the end of SQL String, which is applicable for all Queries.
A Different and simple Method
There is another way that you can do this. But, the first part you have to write in SQL window, and the IN Clause part we can add separately in the Source Database and Source Connection Str properties of the Query so that you don't have to remember where to put the word IN or where to put the Opening/Closing quotes or worry about such syntax issues.
- Create a New Query and open the SQL editing window following Steps-1 to 3 explained above.
- Write the SQL string SELECT Employees.* FROM Employees;
- Select Design View from View Menu, to change the Query from SQL View in Design View. We are now in the normal Query Design View. You will find the Employees Table object appearing in the Query Design without any Field Names showing in it.
- Select Properties from View Menu to display the Property Sheet of the Query.
- Click on an empty area of the Query surface, to the right of the Table Name, to display the Query level Property Sheet correctly.
- There are two properties on the Property Sheet that we are interested in.
- Source Database
- Source Connect Str
Here we will be using only the Source Database Property for the external MS-Access Table.
- Enter the following Path Name in the Source Database Property over-writing the text (current).
C:\Program Files\Microsoft Office\Office\samples\Northwind.mdb
Or with the change explained above for MS-Access2003 cases.
- Select Datasheet View from View Menu.
You will now see the same result as the first Select Query we tried. If you change the Query into its SQL View and inspect the SQL String, you can see that the IN Clause is formed using the Source Database property Value.
Source Connect Str for dBase and Excel
You can use the same method for dBase Table and Excel Table (Named Range). In these cases, we have to use the Source Connect Str Property also.
Example: opening the dBase Table directly.
Table Name : Customer.dbf
SQL String: SELECT Customer.* FROM Customer;
Source Database Property Value = C:\mydBase
Source Connect Str Property Value = DBASE IV; (check the semicolon at the end)
Replace the Customer table name and the Path C:\mydBase location address with your own dBase File Name and Folder name, where the dBase Table is located.
Example: opening Excel Table (Named Range) direct.
Table Name: Categories
SQL String: SELECT Categories.* FROM Categories;
Source Database Property Value = C:\My Documents\Products.xls
Source Connect Str Property Value = Excel 5.0;
(note the semicolon at the end)
Replace the Categories table name and the Path C:\My Documents\Products.xls Excel file with your own.
NB: You must first define the Name Categories in Excel Table Range use Insert - - > Name - - > Define before attempting to use the name Categories in MS-Access Queries.
Next: MS-Access Live data in Excel
Great information! Ive been looking for something like this for a while now. Thanks!
ReplyDelete