2013-05-08 51 views
3

我嘗試使用Access調用SQL Server中的存儲過程。但有困難建立的ODBC連接,我不知道我錯過了什麼?或者只需要在SQL網站上做一些設置?如何創建到SQL Server的ODBC連接?

我有這樣一個畫面:

enter image description here

和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 
+1

您可能剛剛使用了Google一秒鐘。這將帶來這個:http://www.sqlstrings.com/SQL-Server-connection-strings.htm – 2013-05-08 13:03:00

+0

謝謝,我的連接字符串set.Butj它不起作用。我只是困惑。對不起 – 2013-05-08 13:11:58

+1

但是根據我引用的頁面,你的連接字符串似乎是錯誤的...例如,它不應該說'DRIVER = {SQL SERVER}',但是'DRIVER = {SQLServer}'! – 2013-05-08 13:20:51

回答

1

這是錯誤的

strConnect = "ODBC;DRIVER={SQL Server}" _ 
      & ";SERVER=" & strServer _ 
      & ";DATABASE=" & strDatabase _ 
      & ";UID=" & strUID _ 
      & ";PWD=" & strPWD & ";" 

它應該閱讀

strConnect = "DRIVER={SQL Server Native Client 10.0}" _ 
      & ";SERVER=" & strServer _ 
      & ";DATABASE=" & strDatabase _ 
      & ";UID=" & strUID _ 
      & ";PWD=" & strPWD & ";" 
+0

謝謝,對不起,連接仍然失敗 – 2013-05-08 13:33:51

+0

我編輯了我的答案。這是與SQL Server 2008連接。請訪問http://www.connectionstrings.com/獲取更多詳細信息。 – 2013-05-08 13:52:39

+0

謝謝,我會試試看。 – 2013-05-08 14:02:09

2

您應該訪問ConnectionStrings網站了解詳情,但是,如果我是您,我不會使用ODBC。
我的連接(用於SQL Server 2012):

Private oCon As ADODB.Connection

Public Sub InitConnection(ByRef sDataSource As String, ByRef sDBName As String) Dim sConStr As String Set oCon = New ADODB.Connection sConStr = "Provider=MSDataShape;Data Provider=SQLNCLI11;" & _ "Integrated Security=SSPI;Persist Security Info=False;Data Source=" & _ sDataSource & ";Initial Catalog=" & sDBName On Error Resume Next Call oCon.Open(sConStr) If (Err.Number = 0) Then 'all OK Else 'Show Error Message/Throw/Sink/etc End If On Error GoTo 0 End Sub

哪裏sDataSource是 「[計算機] \ [SQL Server實例]」(同例如SSMS,它就像 「MyHomePC \ SQLEXP」)和sDBName是默認目錄,即打開默認數據庫。您需要添加對Microsoft ActiveX Data Objects的引用,以便您可以使用ADODBConnection,CommandRecordset對象(在Access VB窗口中:「工具」 - >「參考...」)。
MSDataShape不是強制性的,但適用於分層網格。


編輯:順便說一句,從connstr。網站:Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase; Uid=myUsername;Pwd=myPassword;(同樣,對於SQL Server 2012,對於2008,它是「...客戶端10」)。

+0

非常感謝。對於初學者來說,也許這個項目對我來說太難了。我會閱讀你給我的鏈接,看看我能走多遠。 – 2013-05-08 14:00:50

+0

不客氣!一切都很簡單 - 如果你知道如何。你只需要學習它,就是這樣;) – 2013-05-08 14:03:20

+0

我看到我的Sever是'SQL server 9.0.5000'這是否意味着Server 2005?和客戶端9? – 2013-05-08 14:10:12