这个问题对于“VBA创建DSN”第一谷歌的结果,但是因为他们似乎围绕着触摸注册表或以其他方式避免使用一个DSN的,我不喜欢的答案。就我而言,我有一位项目经理想要使用DSN,因为这是他们所熟悉的,所以我无法避免它。对于任何为此而苦苦挣扎的人,我发现了一种非常简单的方式来在别处做到这一点。值得注意的是here。
我使用的代码在那里发现,here和here凑齐这一点,并把它放在一个闪屏形式的开放活动:
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" _
(ByVal hwndParent As Long, ByVal fRequest As Long, _
ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
If fDsnExist("DSN=YOUR_DSN_NAME") = True Then
'Do all of your loading or just close this form.
Else
Dim doContinue As Integer
doContinue = MsgBox("There is an issue with the database connection. This can be corrected now or you can reach out to support." _
& vbCrLf & vbCrLf & "Do you want to attempt to correct the issue now?", vbYesNo, "Connection Error")
If doContinue = vbYes Then
Dim vAttributes As String
vAttributes = "DSN=YOUR_DSN_NAME" & Chr(0)
vAttributes = vAttributes & "Description=Self Explnatory" & Chr(0)
vAttributes = vAttributes & "Trusted_Connection=Yes" & Chr(0)
vAttributes = vAttributes & "Server=YOUR_SQL_SERVER_ADDRESS" & Chr(0)
vAttributes = vAttributes & "Database=YOUR_DATABASE_NAME" & Chr(0)
SQLConfigDataSource 0&, 1, "SQL Server", vAttributes
If Err.Number <> 0 Then
MsgBox "The connection could not be restored. Please report this error to support: " & vbCrLf & vbCrLf & Err.Description
Err.Clear
DoCmd.Close acForm, "frmSplash"
DoCmd.Quit acQuitSaveNone
Else
MsgBox "The Connection has been restored.", , "Success"
End If
Else
MsgBox "Please contact support to resolve this issue.", vbCritical + vbOKOnly, "Error"
DoCmd.Close acForm, "frmSplash"
DoCmd.Quit acQuitSaveNone
End If
End If
End Sub
Function fDsnExist(strDsn)
On Error Resume Next
' ------------------------------------------------------
' Declare Variables
' ------------------------------------------------------
Dim objConnection
Dim strReturn
' ------------------------------------------------------
' Create database object
' ------------------------------------------------------
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strDsn
objConnection.Open
' ------------------------------------------------------
' Check if database is open Correctly
' ------------------------------------------------------
If Err.Number <> 0 Then
strReturn = False
Err.Clear
Else
strReturn = True
' ------------------------------------------------------
' Close database connection
' ------------------------------------------------------
objConnection.Close
End If
Set objConnection = Nothing
' ------------------------------------------------------
' Return database status
' ------------------------------------------------------
fDsnExist = strReturn
End Function
现在,当用户打开Access数据库,飞溅的形式检查对于DSN的存在,如果找不到,则为用户提供创建它的选项。
你提到NT验证;我假设用户已经登录到一个域并且已经使用这些凭证向SQL服务器提供了访问权限,我使用了一个可信连接。您可能需要修改DSN连接字符串以提示输入密码和用户名。
我认为,用户必须建立一个ODBC系统DSN第一。然后他们可以通过Access访问SQL Server。 –
所以,你想以编程方式创建使用vba的ODBC系统DSN?那是对的吗? – RubberDuck
我不是要求SQL访问链接服务器我正在寻找访问SQL。 – CaptainKid