我嘗試使用Access調用SQL Server中的存儲過程。但有困難建立的ODBC連接,我不知道我錯過了什麼?或者只需要在SQL網站上做一些設置?如何創建到SQL Server的ODBC連接?
我有這樣一個畫面:
和OK按鈕後面的代碼是這樣的:
Dim dbPUBS As dao.Database
Dim tdfPUBS As dao.TableDef
Dim qdfPUBS As dao.QueryDef
Dim strMsg As String
Dim strSQL As String
' Check for existence of Server, Database and User Name.
' If missing, inform user and exit.
If IsNull(Me!txtServer) Then
strMsg = "Enter name of your company's Server." & _
& "(See your database administrator)"
MsgBox strMsg, vbInformation, "Missing Data"
Me!txtServer.SetFocus
ElseIf IsNull(Me!txtDatabase) Then
strMsg = "Enter name of database. (Example: xxxx)"
MsgBox strMsg, vbInformation, "Missing Data"
Me!txtDatabase.SetFocus
ElseIf IsNull(Me!txtUID) Then
strMsg = "Enter user login. (Example: xx)" = ""
MsgBox strMsg, vbInformation, "Missing Data"
Me!txtDatabase.SetFocus
Else
strServer = Me!txtServer
strDatabase = Me!txtDatabase
strUID = Me!txtUID
' Password may be NULL, so provide for that possibility
strPWD = Nz(Me!txtPWD, "")
' Prepare connection string
strConnect = "ODBC;DRIVER={SQL Server}" _
& ";SERVER=" & strServer _
& ";DATABASE=" & strDatabase _
& ";UID=" & strUID _
& ";PWD=" & strPWD & ";"
End If
Private Function ValidateConnectString() As Boolean
On Error Resume Next
Err.Clear
DoCmd.Hourglass True
' Assume success
ValidateConnectString = True
' Create test Query and set properties
Set qdfPUBS = dbPUBS.CreateQueryDef("")
qdfPUBS.Connect = strConnect
qdfPUBS.ReturnsRecords = False
qdfPUBS.ODBCTimeout = 5
' Attempt to delete a record that doesn't exist
qdfPUBS.SQL = "DELETE FROM Authors WHERE au_lname = 'Lesandrini'"
' Simply test one Pass Through query to see that previous
' connect string is still valid (server has not changed)
qdfPUBS.Execute
' If there was an error, connection failed
If Err.Number Then ValidateConnectString = False
Set qdfPUBS = Nothing
DoCmd.Hourglass False
End Function
您可能剛剛使用了Google一秒鐘。這將帶來這個:http://www.sqlstrings.com/SQL-Server-connection-strings.htm – 2013-05-08 13:03:00
謝謝,我的連接字符串set.Butj它不起作用。我只是困惑。對不起 – 2013-05-08 13:11:58
但是根據我引用的頁面,你的連接字符串似乎是錯誤的...例如,它不應該說'DRIVER = {SQL SERVER}',但是'DRIVER = {SQLServer}'! – 2013-05-08 13:20:51