2017-03-07 85 views
0

我正在開發一個項目,將Microsoft訪問後端遷移到SQL Server後端,並且我的客戶堅持使用Access創建表單以執行任何插入,更新和刪除操作。問題是他的機器上有SQL數據工具,而他的團隊中的其他人(沒有數據工具)也需要能夠使用這些表單。我認爲最好的方法是通過訪問使用OleDb連接來連接表單,以便他的團隊可以使用它,而且我的訪問知識非常有限。到目前爲止,我所擁有的全部是從使用OleDb的訪問表單連接到SQL服務器

「Driver=SQLOLEDB;Data Source=SomeServer;Initial Catalog=SomeDatabase;Integrated Security=SSPI」 

我知道用戶在SQL框中有信任,並且可以通過ODBC連接。我們只是無法讓OleDb工作。任何關於如何在訪問表單中部署OleDB連接的幫助將不勝感激。

+0

我不明白你的問題。 「在Access窗體中部署OleDB連接」是什麼意思?你有代碼給我們看?什麼不工作? – nicomp

+0

我擁有的所有代碼都是我包含的內容。由於能力,客戶無法使用程序化解決方案。我根本不熟悉訪問,並且有一種方法可以建立從訪問SQL Server的連接。我非常喜歡。我的問題是我們所嘗試過的一切都無法正常工作或出現錯誤。就像找不到連接文件或其他東西。 –

回答

1

這是我們用於SQL Server的連接。它支持使用可信連接或SQL Server身份驗證。

Call GetConnection(gvstr_SQLServer_Name, gvstr_SQLServer_Database, _ 
     cnConn, adUseServer, False, False) 

    If GetConnection(gvstr_SQLServer_Name, gvstr_SQLServer_Database, _ 
      gv_DBS_SQLServer, adUseServer, True, False) = True Then 
     gvbln_UsingSQLServer = True 
     DoCmd.Hourglass True 
     ReLink_SQLSERVER_Tables 
    Else 
     gvbln_UsingSQLServer = False 
    End If 



Public Function GetConnection(ByVal strDSN As String, _ 
    ByVal strDatabase As String, _ 
    ByRef cnLocal As ADODB.Connection, _ 
    ByVal CursorLoc As CursorLocationEnum, _ 
    ByVal UsePassword As Boolean, _ 
    ByVal blnTrusted As Boolean) As Boolean 

Dim intWaitDuration  As Integer 
Dim strConnectString As String 
Dim strDisplay   As String 

Const CURRENT_METHOD As String = "GetConnection" 
On Error GoTo ERROR_HANDLER 
GetConnection = False 
intWaitDuration = 60  
Retry_Connection: 
If cnLocal Is Nothing Then Set cnLocal = New ADODB.Connection 
If cnLocal.State = adStateOpen Then 
     Write_To_Log "Connection already open -- -will not reopen!!" 
    GetConnection = True 
    GoTo Proc_Exit 
End If 

With cnLocal 
    Debug.Print "Use TRUSTED CONNECTION (ABOVE)" 
    If gvstr_Workstation = "my-pc" Then 
     strConnectString = "Driver={SQL Server};" & _ 
           "Server=" & strDSN & ";" & _ 
           "Database=" & strDatabase & ";" & _ 
           "Trusted_Connection=yes" 
    Else 
     If blnTrusted = True Then 
      strConnectString = "Driver={SQL Server};" & _ 
           "Server=" & strDSN & ";" & _ 
           "Database=" & strDatabase & ";" & _ 
           "Trusted_Connection=yes" 
     Else 
      strConnectString = "Driver={SQL Server};" & _ 
           "Server=" & strDSN & ";" & _ 
           "Database=" & strDatabase & ";" & _ 
           "User Id=Sql_myuid;Password=ppppp" 

      strDisplay = "Driver={SQL Server};" & _ 
           "Server=" & strDSN & ";" & _ 
           "Database=" & strDatabase & ";" & _ 
           "User Id=S*********t;Password=****************" 

     End If 
    End If 
    Write_To_Log "Will use Conn String: " & strDisplay 
    .ConnectionString = strConnectString 
    .CursorLocation = CursorLoc 
    .Open 
End With 
GetConnection = True 
Proc_Exit: 
Exit Function 

ERROR_HANDLER: 
Debug.Print Err.Number & vbCrLf & Err.Description 

Err.Source = "Module_Utilities: GetConnection at Line: " & Erl 
DocAndShowError 
Resume Proc_Exit 
Resume Next 
Resume 
End Function 
+0

我需要添加這個才能讓它工作?我是否需要更改代碼中的任何內容我沒有看到任何變量? –

+0

對不起,如果您使用我所示的第一條指令調用該函數,它將看起來像TRUSTED CONNECTION的值:Call GetConnection(「PROD_0001」,「MySQLDAtabase」,_ cnConn,adUseServer,False,True)或者像這對於SQL Server身份驗證:調用GetConnection(「PROD_0001」,「MySQLDAtabase」,_ cnConn,adUseServer,True,False) –

+0

謝謝我將用它來爲將來的應用程序。這個實例需要一個非編程解決方案。 –