Access.2007.VBA.Bibl..

(John Hannent) #1
If Not cnn Is Nothing Then
If cnn.State = adStateOpen Then
cnn.Close
Set cnn = Nothing
End If
End If

Exit Sub

ErrorHandler:
MsgBox “Error No: “ & Err.Number _
& “; Description: “ & Err.Description
Resume ErrorHandlerExit
End Sub

Command......................................................................................................

The ADO Command object represents SQL commands, roughly equivalent to queries in Access
databases, or QueryDefs in the DAO object model. You don’t need to use this object to query or fil-
ter Access data; this can be done using a SQL statement (as in the preceding code segment) or the
name of a saved query for the Source argument when opening a recordset. However, the Command
object can be useful when you want to reuse a command later in the code, or if you need to pass
detailed parameter information with the command.


The procedure uses a Command object to create a recordset, which can be used later in the code:


Private Sub OpenRecordsetCommand()

On Error Resume Next

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmdSQL As ADODB.Command
Dim strDBName As String
Dim strConnectString As String
Dim strSQL As String
Dim strCursorType As String
Dim strLockType As String
Dim strDBNameAndPath As String
Dim strCurrentPath As String
Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Dim strPrompt As String

Create connection to an external database.


strCurrentPath = Application.CurrentProject.Path & “\”
strDBName = “Northwind.mdb”
strDBNameAndPath = strCurrentPath & strDBName

Working with Access Data 5

Free download pdf