Article ID: 190991
Article Last Modified on 3/2/2005
Option Explicit
Dim WithEvents Con As ADODB.Connection
Private Sub Form_Load()
Set Con = New ADODB.Connection
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, _
UnloadMode As Integer)
If (Con Is Nothing) Then
Exit Sub
ElseIf Con.State = adStateOpen Then
Con.Close
ElseIf Con.State = adStateConnecting Then
Con.Cancel
ElseIf Con.State = adStateExecuting Then
Con.Cancel
Con.Close
End If
Set Con = Nothing
End Sub
Private Sub Command1_Click()
Dim sServer As String
Dim sUserID As String
Dim sPassword As String
Dim sConnectStr As String
sServer = "<your server name>"
sUserID = "<your user ID>"
sPassword = "<your password>"
sConnectStr = "Driver={SQL Server};Server=" & sServer & ";"
sConnectStr = sConnectStr & "UID=" & sUserID & ";"
sConnectStr = sConnectStr & "PWD=" & sPassword & ";DSN=''"
Con.Open sConnectStr, , , adAsyncConnect
End Sub
Private Sub Con_ConnectComplete(ByVal pError As ADODB.Error, _
adStatus As ADODB.EventStatusEnum, _
ByVal pConnection As ADODB.Connection)
If adStatus = adStatusErrorsOccurred Then GoTo EH
MsgBox "Connection Established.", vbInformation, "Success!"
Con.Close
Exit Sub
EH:
MsgBox "Check your connection parameters.", vbCritical, _
"Connection Failed!"
set con = nothing
Err.Clear
End Sub
Keywords: kbhowto kbdatabase KB190991