Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Database Connection String Properties

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.

  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.

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.

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

Earlier Post Link References:

Share:

1 comment:

  1. Great information! I’ve been looking for something like this for a while now. Thanks!

    ReplyDelete

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code