The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills.
MORE INFORMATIONChanging the Type of Workspace You UseThe first thing you must do when implementing ODBCDirect is to create ODBCDirect Workspace objects in your code. In Microsoft Access 97, you can create ODBCDirect workspaces in two ways:
Once you create a workspace, its Type property is read-only, and is set to
either dbUseJet or dbUseODBC.
Changing the Database You OpenPrior to Microsoft Access 97 and ODBCDirect, the only way to connect to an ODBC data source was through the Database object. ODBCDirect offers you two connection options: you can use the traditional call to the OpenDatabase method, or you can create a Connection object using the OpenConnection method. The difference between the Database object and the Connection object is that the performance of the Connection object is tuned for remote database connectivity. For example, the Connection object can perform asynchronous operations and can create temporary QueryDef objects against remote data; in contrast, the Database object follows the traditional DAO model using the Jet database engine.
In this syntax, the workspace argument is the name of the ODBCDirect
workspace from which you are creating the new Connection object. The
connect argument is a valid connect string that supplies parameters to
the ODBC driver manager. These parameters can include user name,
password, default database, and data source name (DSN). The
connect argument overrides the value in the name argument; if you
specify a registered ODBC DSN in the connect argument, then the name
argument can be any valid string. If a valid ODBC DSN is not included
in the connect argument, then the name argument must refer to a valid
ODBC DSN. Note that all connection strings start with "ODBC;" and must
contain a series of values required by the ODBC driver to access data.
The minimum requirements for the connect argument include a userID, a
password, and a DSN, as shown below:
NOTE: If one or more required arguments is missing from your connection
string, the ODBC driver manager will prompt you for the missing
information if you use any of the following constants as the second
argument of the OpenConnection method:
If you do not want to be prompted for missing information, make sure
your connection string contains all the required information, or use
the dbDriverNoPrompt constant as the second argument of the
OpenConnection method.
In some cases, opening connections to data sources can take a long time.
For that reason, you may want to open your connections asynchronously.
This allows other users to work in your application while the connection
is being established. To open a connection asynchronously, add the
dbRunAsync constant to the options argument of the OpenConnection
method. When you open an asynchronous connection, you can use the Cancel
property of the Connection object to cancel the connection if it takes
too long to connect. In addition, if you want to check to see if the
connection has been established, you can check the StillExecuting
property of the Connection object, as shown in the following example:
Handling Data Definition Language (DDL) OperationsDAO's ODBCDirect functionality does not support the TableDefs or Indexes collection. This means that an application that programmatically creates new TableDef objects or looks up indexes in the Indexes collection of a TableDef object will not work in the ODBCDirect object model of DAO version 3.5. There are two ways you can work around this limitation:
Creating and Using QueryDef ObjectsWhen you use the OpenDatabase method in an ODBCDirect workspace to connect to an ODBC data source, the CreateQueryDef method is not supported. Therefore, the only way to successfully execute the CreateQueryDef method against an ODBC data source using ODBCDirect, is through a Connection object. If you have existing code that uses the CreateQueryDef method, and the ODBCDirect connection was established using a Database object, you must change your CreateQueryDef calls to execute on the Connection property of the Database object rather than on the Database object itself.NOTE: QueryDef objects that you create in an ODBCDirect workspace are not stored in the database, and are lost when the Workspace object is closed or goes out of scope. QueryDef objects are powerful because they are prepared and optimized statements that can be called again and again. QueryDef objects, like Connection objects, support asynchronous execution through the Execute and OpenRecordset methods. Also, you can use the QueryDef object to set up properties for the resulting Recordset. For example, when you use ODBCDirect, you can use the CacheSize method of a QueryDef object to limit the number of records cached locally. The following example illustrates this technique:
For more information about QueryDef objects and their properties, search
the Help Index for "QueryDef objects," and then select "QueryDef Object
(DAO)."
Opening Recordset ObjectsAnother consideration when you use ODBCDirect workspaces is that recordsets open differently by default than they do in a Jet workspace. For example, Recordset objects opened in an ODBCDirect workspace default to the fastest Recordset type, which is a forward-only, read-only Recordset.The syntax for creating a Recordset object is:
In this syntax, the Source argument is required; it refers to the
name of the table, query, view, or an SQL statement that returns records.
The Type argument is optional; it indicates the type of Recordset to open
or the manner in which records are retrieved from the server and buffered.
The constants you can use for the Type argument in ODBCDirect are:
NOTE: If you do not specify a Type argument with the OpenRecordset method
in an ODBCDirect workspace, the object defaults to dbOpenForwardOnly. In
order to update records, or to scroll backward through the recordset, be
sure to use dbOpenDynaset or dbOpenDynamic in the Type argument.
The Options argument is also optional; it specifies the characteristics of the new Recordset. The Options argument can be any of the following constants in a Microsoft Jet Workspace
However, you can only supply a zero (0) for the Options argument in an
ODBCDirect Workspace, for example:
In a Microsoft Jet Workspace, you can use constants in the Options argument
in combination, for example:
However, you must be careful when choosing the combinations you create.
The type you choose must work with the options that can be selected for
that type. For example, in the following statement the dbSeeChanges option
is not necessary with a dbOpenSnapShot type recordset:
The LockEdits argument is optional; it specifies the record locking
mechanism to use if you open your recordset as dbOpenDynaset or
dbOpenDynamic. The constants you can use in this argument are:
Using Parameterized QueriesWhen you work with parameterized QueryDef objects in an ODBCDirect workspace, you continue to work with the Parameter object common to Microsoft Jet database engine version 3.0. One new feature which has been added to Parameter objects which makes them more useful for client/server applications is the Direction property. This property sets or returns a value that indicates whether a Parameter object represents an input parameter, an output parameter, both input and output, or the return value from the procedure. Although the ODBC driver will attempt to determine the direction of the parameter, the Direction property is read/write so you can set it if you need to.NOTE: Some ODBC servers require you to specify information in the Direction property before you execute the query; others will set the property for you. ODBCDirect does not support named parameters. Therefore, the syntax for a parameter in a SQL statement in ODBCDirect workspaces is a question mark (?), instead of a name as it is in Microsoft Jet workspaces. For example, the Microsoft Jet SQL expression "SELECT * FROM Employees WHERE LastName = [txtName]" creates a parameter named txtName. With ODBCDirect, the same SQL statement reads as "SELECT * FROM Employees WHERE LastName = ?" For an example that uses the Direction property, search the Help Index for "Direction property." Performing Batch Optimistic UpdatingAnother advantage to using ODBCDirect is the ability to decrease network traffic between client and server computers using Batch Optimistic Updating. This means that all changes to a recordset are cached locally until you specifically tell DAO to flush all changes to the server. This is accomplished by specifying the type argument dbUpdateBatch when you call the Update method.Before you call the Update method, it is recommended that you specify how individual rows will be updated. To accomplish this, you can use the UpdateOptions property of the Recordset object. Unless you specify otherwise, the UpdateOptions property defaults to the following:
This means that Microsoft Access is going to use the primary key value
when it constructs the Where clause during the batch update. This
property accepts any combination of the following constants:
To use Batch Optimistic Updating in Microsoft Access 97, you must satisfy
the following conditions:
REFERENCES
Microsoft Office 97 "Visual Basic Programmer's Guide," Chapter 11, "Data
Access Objects," pages 289-312
Additional query words:
Keywords : kbinterop kbprg AccCon OdbcHowto |
Last Reviewed: September 28, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |