rs''// stServer: Name of SQL Server that you are linking to''// stDatabase: Name of the SQL Server database that you are linking to''// stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection''// stPassword: SQL Server user passwordFunction CreateDSNConnection(stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String) As Boolean On Error GoTo CreateDSNConnection_Err Dim stConnect As String If Len(stUsername) = 0 Then ''//Use trusted authentication if stUsername is not supplied. stConnect = "Description=myDSN" & vbCr & "SERVER=" & stServer & vbCr & "DATABASE=" & stDatabase & vbCr & "Trusted_Connection=Yes" Else stConnect = "Description=myDSN" & vbCr & "SERVER=" & stServer & vbCr & "DATABASE=" & stDatabase & vbCr End If DBEngine.RegisterDatabase "myDSN", "SQL Server", True, stConnect ''// Add error checking. CreateDSNConnection = True Exit FunctionCreateDSNConnection_Err: CreateDSNConnection = False MsgBox "CreateDSNConnection encountered an unexpected error: " & Err.Description End Function注意 如果再次, 调用 RegisterDatabase 方法 DSN 更新。