我的部门使用使用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'
?
任何人都知道我在做什么错了?
你是否给出错误信息?当你尝试时会发生什么? –
这不是VB.NET代码 – Plutonix
也,引号是正确的,但我相信用户ID应该是用户ID或用户ID(全部在一起)。 [检查出来](https://www.connectionstrings.com/sql-server-native-client-11-0-oledb-provider/standard-security/) –