如何在Access 2016 VB应用程序中连接到新的数据源?

时间:2017-06-20 18:09:31

标签: vb.net ms-access

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

我对相关服务器拥有'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
                '===================================================================

我的表单有一个下拉列表,可以从表格中选择用户列表。我在该表中添加了一个测试用户,但该测试用户没有显示在下拉列表中。因此,我认为没有建立联系。

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

0 个答案:

没有答案