繁体   English   中英

如何在Access 2016 VBA中连接到新的数据源?

[英]How do I connect to a new Datasource in Access 2016 VBA?

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

我在有关服务器上具有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' UserID='Admin'; Password='Test'

有人知道我在做什么错吗?

基本上,本地Access前端数据库和后端SQL Server数据库之间存在同名表的版本混淆。 目前,您的代码更新上的SQL Server后端侧tblUserLog,而不是在接入前端一侧可能是一个绑定到您的形式相同tblUserLog。 因此,为什么您看不到任何更新,因为服务器表从未显示。

只需执行以下两项操作之一:

  1. 使用SQL Server版本 :删除或存档Access的版本,并将tblUserLog添加为ODBC链接表 确保删除dbo_或其他架构前缀,并保持相同的名称tblUserLog 并且由于表名不会更改,因此诸如表单和VBA代码之类的所有内容都不需要更改。

  2. 使用访问版本 :保留本地访问表tblUserLog ,并在VBA中更新此表,这需要添加另一个连接对象并在该连接上运行记录集更新。 参见以下带有ADO对象上的server_local_前缀:

     ' 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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM