Opening external data sources like dBase, Excel, 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 that we must know how to reference different external data sources correctly with Source Database Path and Connection String Values. But, these 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 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 few readers thought of copying the SQL string presented in the VBA Code into a Query and try them out. If not, it is time to do that. It is a better option to understand them and to look into few things associated with their usage.

To start with, let us try opening an MS-Access Table from another Database directly in a Select Query.

  1. Open one of your Databases or create a new one.
  2. 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.
  3. Click on the SQL Toolbar Button or select SQL View from View Menu.
  4. 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\. . .

  5. 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.

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.

  1. Create a New Query and open the SQL editing window following Steps-1 to 3 explained above.
  2. Write the SQL string SELECT Employees.* FROM Employees;
  3. Select Design View from View Menu, to change the Query from SQL View to Design View. We are now in the normal Query Design View. You will find the Employees Table object is appearing on the Query Design without any Field Names showing in it.
  4. Select Properties from View Menu to display the Property Sheet of the Query.
  5. Click on an empty area of the Query surface, to the right of the Table Name, to display the Query level Property Sheet correctly.
  6. 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 external MS-Access Table.

  7. 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.

  8. Select Datasheet View from View Menu.

You will now see the same result as of 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.

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 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 using Insert – – > Name – – > Define before attempting to use the name Categories in MS-Access Queries.

Next: MS-Access Live data in Excel

StumbleUpon Toolbar