我的部门使用使用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'
?
任何人都知道我做错了什么?
答案 0 :(得分:1)
基本上,您在本地Access前端数据库和后端SQL Server数据库之间存在同一命名表的版本混淆。目前,您的代码更新了SQL Server后端端的 tblUserLog ,并且与访问前端端的 tblUserLog 不同,这可能是绑定到表单的那个。因此,为什么你看不到任何更新,因为服务器表永远不会显示。
只需做以下两件事之一:
使用SQL Server版本:删除或存档Access版本,并将 tblUserLog 添加为ODBC linked table。请务必删除 dbo _ 或其他架构前缀,并保留相同名称 tblUserLog 。并且由于表名不会更改,因此表单和VBA代码等所有内容都不需要更改。
使用访问版本:保留本地Access表 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