像SQL中一样,可以测试是否和数据库连接成功!
Private Function TestConnectSqlDatabase(VstrUser As String, VstrPwd As String, VstrServer As String, VstrDataBase As String) As Boolean
On Error GoTo ErrHandle
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
StrConn = "Driver={SQL Server};SERVER=" & "" & VstrServer & "" & ";DATABASE=" & "" & VstrDataBase & "" & ";UID=" & "" & VstrUser & "" & ";PWD=" & "" & VstrPwd & ""
cnn.ConnectionString = StrConn
cnn.Open connectstring
TestConnectSqlDatabase = True
Done:
Set cnn = Nothing
Exit Function
ErrHandle:
TestConnectSqlDatabase = False
GoTo Done
End Function