3

I try to use Access to call a stored procedure in SQL Server. But have trouble to build the ODBC connection, I do not know am I missing something? Or just need do some set in sql site?

I have a screen like this:

enter image description here

and code behind the OK button is this:

      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
4

2 回答 2

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

“[COMPUTERNAME]\[SQL SERVER INSTANCE]”在哪里sDataSource(与 SSMS 相同,类似于“MyHomePC\SQLEXP”)并且sDBName是默认目录,即要打开的默认 DB。您需要添加对 , 和对象的引用Microsoft ActiveX Data ObjectsADODB ConnectionCommandAccess RecordsetVB 窗口中:“工具”->“引用...”)。
MSDataShape不是强制性的,但对于分层网格很方便。


编辑:顺便说一句,来自 connstr。站点:(Driver={SQL Server Native Client 11.0};Server=myServerAddress;Database=myDataBase; Uid=myUsername;Pwd=myPassword;同样,对于 SQL Server 2012,对于 2008,它是“...Client 10”。)

于 2013-05-08T13:49:14.240 回答
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 & ";"
于 2013-05-08T13:22:40.183 回答