Article ID: 164481
Article Last Modified on 1/19/2007
Dim wrkODBC As Workspace
DBEngine.DefaultType = dbUseODBC
Set wrkODBC = DBEngine.CreateWorkspace("NewODBCWrk", "admin", "")
Dim wrkODBC as Workspace
Set wrkODBC = DBEngine.CreateWorkspace("NewODBCWrk", "admin", "", _
dbUseODBC)
Dim ODBCWorkSp as Workspace
Dim MyDB as Database
Dim strConnect As String
StrConnect = "ODBC;DSN=Pubs;UID=sa;PWD=;DATABASE=Pubs"
Set ODBCWorkSp = DBEngine.CreateWorkspace("NewODBCDirect", "admin", _
"", dbUseODBC)
Set MyDB = ODBCWorkSp.OpenDatabase("Pubs", dbDriverNoPrompt, False, _
strConnect)
Set connection = workspace.OpenConnection (name, options, readonly, _
connect)
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:
ODBC;UID=UserName;PWD=MyPassWord;DSN=DataSourceNameNOTE: 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:
dbDriverPrompt
dbDriverComplete
dbDriverCompleteRequired
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:
Sub CancelConnectionX()
Dim wrkODBC As Workspace
Dim conODBC As Connection
Set wrkODBC = CreateWorkspace("ODBCWorkspace", "admin", "", _
dbUseODBC)
' Open the connection asynchronously.
Set conODBC = wrkODBC.OpenConnection("Publishers", _
dbDriverNoPrompt + dbRunAsync, False, _
"ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")
' If the connection has not been made, ask the user
' if he/she wants to keep waiting. If the user does not, cancel
' the connection and exit the procedure.
Do While conODBC.StillExecuting
If MsgBox("No connection yet--keep waiting?", _
vbYesNo) = vbNo Then
conODBC.Cancel
MsgBox "Connection cancelled!"
wrkODBC.Close
Exit Sub
End If
Loop
' Close the Connection and Workspace objects.
conODBC.Close
wrkODBC.Close
End Sub
Sub DeleteRecords()
Dim dbs As Database
Dim strConnect As String
Dim cnn Connection
' Open database in default workspace
strConnect = "ODBC;DSN=Pubs;DATABASE=Pubs;UID=sa;PWD=;"
Set dbs = OpenDatabase("", False, False, strConnect)
' Try to create a Connection object from a Database object. If
' workspace is an ODBCDirect workspace, the query runs
' asynchronously. If workspace is a Microsoft Jet workspace, an
' error occurs and the query runs synchronously.
Err = 0
On Error Resume Next
Set cnn = dbs.Connection
' Check to verify whether or not the currently opened workspace
' is an ODBCDirect workspace.
' If there was no error, then it is ODBCDirect Workspace.
If Err = 0 Then
cnn.Execute "DELETE FROM Authors", dbRunAsync
Else
dbs.Execute "DELETE FROM Authors"
End If
End Sub
Sub SetCacheSize()
Dim wrksp As Workspace, qdf As QueryDef, rst As Recordset
Dim cnn As Connection, strConnect As String
Set wrksp = CreateWorkspace("ODBCDirect", "Admin", "", dbUseODBC)
strConnect = "ODBC;DSN=Pubs;UID=sa;PWD=;DATABASE=Pubs"
Set cnn = wrksp.OpenConnection("", dbDriverNoPrompt, False, _
strConnect)
Set qdf = cnn.CreateQueryDef("tempqd")
qdf.SQL = "Select * from authors"
'The local cache for the Recordset is 200 records
qdf.CacheSize = 200
Set rst = qdf.OpenRecordset()
Debug.Print rst.CacheSize
rst.Close
cnn.Close
End Sub
For more information about QueryDef objects and their properties, search
the Help Index for "QueryDef objects," and then select "QueryDef Object
(DAO)."
Set rs = object.OpenRecordset(source, type , options, lockedits)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:
dbOpenDynaset dbOpenDynamic dbOpenSnapShot dbOpenForwardOnlyNOTE: 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.
dbAppendOnly dbSQLPassThrough dbSeeChanges dbDenyWrite dbDenyRead dbForwardOnly dbReadOnly dbRunAsync dbExecDirect dbInconsistent dbConsistentHowever, you can only supply a zero (0) for the Options argument in an ODBCDirect Workspace, for example:
Set rs=cn.OpenRecordset("Source", dbOpenDynaset, 0, dbOptimistic)
In a Microsoft Jet Workspace, you can use constants in the Options argument
in combination, for example:
Set rs=cn.OpenRecordset("Source", dbOpenDynaset,dbSeeChanges+dbRunAsync)
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:
Set rs=cn.OpenRecordset("Source", dbOpenSnapShot, dbSeeChanges _
+ dbConsistent
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:
dbPessimistic dbOptimistic dbOptimisticValue dbOptimisticBatch
dbCriteriaKey+dbCriteriaUpdateThis 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:
Constant Description
-----------------------------------------------------------------------
dbCriteriaKey (Default) Uses just the key column(s) in the
Where clause.
dbCriteriaModValues Uses the key column(s) and all updated columns
in the Where clause.
dbCriteriaAllCols Uses the key column(s) and all the columns in
the Where clause.
dbCriteriaTimeStamp Uses just the timestamp column if available
(will generate a run-time error if no timestamp
column is in the result set).
dbCriteriaDeleteInsert Uses a set of DELETE and INSERT statements for
each modified row.
dbCriteriaUpdate (Default) Uses an UPDATE statement for each
modified row.
To use Batch Optimistic Updating in Microsoft Access 97, you must satisfy
the following conditions:
Function BatchUpdate()
Dim wrkMain As Workspace
Dim conMain As Connection
Dim rstTemp As Recordset
Dim ConnStr as String
Set wrkMain = CreateWorkspace("ODBCWorkspace", "admin", "", _
dbUseODBC)
' This DefaultCursorDriver setting is required for
' batch updating.
wrkMain.DefaultCursorDriver = dbUseClientBatchCursor
ConnStr = "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers"
Set conMain = wrkMain.OpenConnection("Publishers", _
dbDriverNoPrompt, False, ConnStr)
' The following locking argument (dbOptimisticBatch) is required for
' batch updating.
Set rstTemp = conMain.OpenRecordset("SELECT * FROM Authors", _
dbOpenDynaset, dbRunAsync, _
dbOptimisticBatch)
With rstTemp
' Increase the number of statements sent to the server
' during a single batch update, thereby reducing the
' number of times an update would have to access the
' server.
.BatchSize = 25
' Change the UpdateOptions property so that the WHERE
' clause of any batched statements going to the server
' will include any updated columns in addition to the
' key column(s). In addition, DAO 3.5 is going to use an Update
' statement for each modified row.
.UpdateOptions = dbCriteriaAllCols + dbCriteriaUpdate
Do While Not rstTemp.EOF
rstTemp.Edit
rstTemp.Fields("au_lname") = rstTemp.Fields("au_lname") & _
" Test"
rstTemp.Update
rstTemp.MoveNext
Loop
rstTemp.Update (dbUpdateBatch)
.Close
End With
conMain.Close
wrkMain.Close
End Function
Keywords: kbhowto kbinterop kbprogramming KB164481