Article ID: 190988
Article Last Modified on 3/2/2005
Option Explicit
Dim WithEvents con As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim iExecutionCount As Integer
Private Sub Form_Load()
Dim sConnect As String
Dim sServer As String
Dim sUID As String
Dim sPWD As String
On Error GoTo EH
' Specify connection parameters.
sServer = "<your server name>"
sUID = "<your SQL Server user ID>"
sPWD = "<your SQL Server user ID password>"
sConnect = "Driver={SQL Server};Server=" & sServer & _
";Database=Pubs;"
sConnect = sConnect & "UID=" & sUID & ";"
sConnect = sConnect & "PWD=" & sPWD & ";DSN='';"
Set con = New ADODB.Connection
con.CursorLocation = adUseClient
' Open the connection.
con.Open sConnect
command1.Caption = "Open Recordsets"
command2.Caption = "Cancel"
Exit Sub
EH:
MsgBox "Could not establish ODBC connection.", vbCritical + vbOKOnly
Set con = Nothing
End
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
rst.Close
Set rst = Nothing
Set con = Nothing
End Sub
Private Sub Command1_Click()
' This code begins the process of repeatedly opening ADO recordsets.
command1.Enabled = False
command2.Enabled = True
GetRecordsetData
End Sub
Private Sub Command2_Click()
' This code ends the process of repeatedly opening ADO recordsets.
On Error Resume Next
rst.Close
MsgBox "The recordset was opened " & iExecutionCount & " time(s)."
iExecutionCount = 0
command1.Enabled = True
command2.Enabled = False
End Sub
Private Sub GetRecordsetData()
iExecutionCount = iExecutionCount + 1
If rst.State <> adStateClosed Then
rst.Close
End If
rst.Open _
"Select * From Pubs..Publishers, Pubs..Titles, Pubs..Authors", _
con, adOpenKeyset, adLockOptimistic, adAsyncExecute
End Sub
Private Sub con_ExecuteComplete(ByVal RecordsAffected As Long, _
ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, _
ByVal pCommand As ADODB.Command, _
ByVal pRecordset As ADODB.Recordset, _
ByVal pConnection As ADODB.Connection)
' When the ADO recordset has been populated with data, begin opening
' the next ADO recordset.
GetRecordsetData
End Sub
Keywords: kbhowto kbdatabase KB190988