Microsoft KB Archive/101375

= ACC: How to Query SQL Server Tables Without Linking/Attaching =

Article ID: 101375

Article Last Modified on 1/26/2005

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition

-



This article was previously published under Q101375



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how to use the SQL IN clause to query an SQL Server table without linking (attaching) it.



MORE INFORMATION
You can use IN to query only one external database at a time.

The IN clause requires two arguments: database name and connect string. When you link SQL Server tables, you must specify an empty string for the database name. The connect string consists of the following items:

  Data Source Name (DSN)        The name of the SQL Server computer

Database Name (DATABASE)     The name of the SQL Server database

Workstation ID (WSID)        The name of the workstation that is                                 making the connection (optional)

User ID (UID)                The login ID*

Password (PWD)               The user password*

* This item is optional within the connect string. If it is not present, the system prompts the user with a dialog box to enter this information the first time the macro is executed each session.

The following is a sample connect string to SQL Server. Note that it starts with "ODBC," which tells the system that it uses the ODBC driver (as opposed to an installable ISAM driver).

  ODBC;DSN=ServerName;Database=Pubs;WSID=Wrk1;USID=JaneDoe;PWD=ABC123

The following example assumes that the ODBC driver manager and SQL Server driver are installed, that a data source called "Corp" was set up using the ODBC driver manager, and that the table "authors" can be successfully linked from the database Pubs, using the Get External Data/Link Tables commands on the File menu (or Attach Table command on the File menu in versions 1.x and 2.0).


 * 1) Open the sample database Northwind.mdb (or NWIND.MDB in version 1.x and ,2.0)
 * 2) Create a new query. Do not select a table.
 * 3) On the View menu, click SQL.
 * 4) Enter the following SQL statement into the SQL window:

Select *

FROM authors

IN "" "ODBC;dsn=Corp;database=pubs;uid= ;pwd= ";
 * 1) Close the SQL dialog box and close and save the query.

NOTE: If you return to the View SQL window after saving the query, Microsoft Access may place additional characters in the IN clause that you must remove to avoid a syntax error.

