2017-06-21 54 views
0

我的部门使用使用VBA和Access 2016创建的应用程序。它最初指向我们的一台服务器上的Access数据库。在将数据迁移到不同的服务器之后,我现在需要将它指向该不同服务器上的其他SQL Server数据库。但我似乎无法建立联系。如何连接到Access 2016 VBA中的新数据源?

我有dbowner有关服务器的权限。

连接字符串中的原代码如下:

'Remove the filter, close the log on form, and open the Switchboard 
rst.Filter = 0 
DoCmd.Close acForm, "frmUserLogOn" 

'Open the Main Switchboard 
DoCmd.OpenForm "frmMain", acNormal 

'Open the InactiveShutDown form in Hidden mode 
DoCmd.OpenForm "frmInactiveShutDown", acNormal, , , , acHidden 
... 

Set conn = CurrentProject.Connection 
... 

rstLog.Open "tblUserLog", conn, adOpenKeyset, adLockOptimistic 
rstLog.AddNew 
    rstLog!UserID = rst!UserID 
    rstLog!TimeIn = Now() 
rstLog.Update 

我的新代码如下:

'DoCmd.OpenForm "frmInactiveShutDown", acNormal, , , , acHidden 
'Commented out the above statement 
...  
'Set conn = CurrentProject.Connection 
'================================================================== 
'Start of Added Code for SQL Migration 
Set conn = New ADODB.Connection 
With conn 
    .ConnectionString = "Provider=SQLNCLI11;Data Source=(My Server Name);Initial Catalog=(My Database Name);User ID=(Username);Password=(Password)" 
    .Open 
    If .State = adStateClosed Then 
     MsgBox "Connection unsuccessful. Could not open connection.", vbInformation, "Error:" 
    End If 
End With 

Set rst = New ADODB.Recordset 
With rst 
    .ActiveConnection = conn 
    .CursorLocation = adUseClient 
    .CursorType = adOpenStatic 
    .LockType = adLockOptimistic 
    .Open "tbl_Users" 
End With 
'End of Added Code for SQL Migration - See Section 2 for rest of code. 
'================================================================== 
... 

'Section 2 of Code 
'================================================================== 
'Start of added code for SQL Migration 
rstLog.Open "tblUserLog", conn, adOpenStatic, adLockOptimistic 
rstLog.AddNew 
    rstLog!UserID = rst!UserID 
    rstLog!TimeIn = DateTime.Now() 
rstLog.Update 
MsgBox "Success! Connection was made successfully.", vbInformation 
'End of added code for SQL Migration 
'=================================================================== 

我的形式有一个下拉选择列表中的用户形成表。我在该表中添加了一个测试用户,但该测试用户没有显示在下拉列表中。因此,我认为这种联系没有形成。

是否必须在引号中输入数据源,初始目录,用户ID和密码的名称?那么UserID='Admin'; Password='Test'

任何人都知道我在做什么错了?

+0

你是否给出错误信息?当你尝试时会发生什么? –

+1

这不是VB.NET代码 – Plutonix

+0

也,引号是正确的,但我相信用户ID应该是用户ID或用户ID(全部在一起)。 [检查出来](https://www.connectionstrings.com/sql-server-native-client-11-0-oledb-provider/standard-security/) –

回答

1

基本上,本地Access前端数据库和后端SQL Server数据库之间的版本混淆了相同的命名表。目前,您的代码更新了SQL Server后端上的tblUserLog,并且在Access前端端不一样,这可能是绑定到您的表单的那一端。因此,为什么你看不到任何更新,因为服务器表从不显示。

简单地做一两件事情:

  1. 使用SQL Server版本:删除或存档访问版本,并添加tblUserLogODBC linked table。请务必删除dbo_或其他架构前缀并保留相同名称tblUserLog。并且由于表名不会改变,因此表单和VBA代码等所有内容都不需要更改。

  2. 使用Access版本:保持本地Access表,tblUserLog,并更新这一个在VBA这需要增加另一个连接对象,并运行该连接上一个记录更新。见下面server_local_的前缀对ADO对象:

    ' OPEN SERVER CONNECTION AND SERVER TABLE RECORDSET 
    Set server_conn = New ADODB.Connection 
    
    With server_conn 
        .ConnectionString = "Provider=SQLNCLI11;Data Source=(My Server Name);Initial Catalog=(My Database Name);User ID=(Username);Password=(Password)" 
        .Open 
        If .State = adStateClosed Then 
         MsgBox "Connection unsuccessful. Could not open connection.", vbInformation, "Error:" 
        End If 
    End With 
    
    Set server_rst = New ADODB.Recordset 
    With server_rst 
        .ActiveConnection = conn 
        .CursorLocation = adUseClient 
        .CursorType = adOpenStatic 
        .LockType = adLockOptimistic 
        .Open "tbl_Users" 
    End With 
    
    ' OPEN LOCAL CONNECTION AND UPDATE LOCAL TABLE RECORDSET 
    Set local_conn = CurrentProject.Connection 
    
    Set local_rstLog = New ADODB.Recordset 
    local_rstLog.Open "tblUserLog", local_conn, adOpenStatic, adLockOptimistic 
    local_rstLog.AddNew 
        local_rstLog!UserID = server_rst!UserID  ' NOTICE SERVER DATA USED HERE 
        local_rstLog!TimeIn = DateTime.Now() 
    local_rstLog.Update 
    
    ... 
    ' RELEASE RESOURCES 
    Set server_rst = Nothing: Set local_rstLog = Nothing 
    Set server_conn = Nothing: Set local_conn = Nothing