"运行时错误462:远程服务器计算机不存在或不可用"在EXCEL中第二次运行VBA代码时(获取ACCESS表)

时间:2017-08-30 06:47:26

标签: excel vba ms-access

我在VBA EXCEL中有一个宏,它打开一个访问文件并运行4个查询,关闭访问文件,最后将表从ACCESS加载到EXCEL。 我的问题是,当我第二次运行它时,我得到了运行时错误' 462'。 我读到它与变量有关,我需要完全限定变量。你能告诉我如何分配我的变量吗?

    Sub open_Access()
    Application.ScreenUpdating = False

    Set xcess = New Access.Application

    xcess.Visible = True

    folderpath = ActiveWorkbook.Path
    objectname2 = folderpath & "\"
    fileName = "Merge17.accdb"
    objectname = objectname2 & fileName

    xcess.OpenCurrentDatabase (objectname)

    If Not IsNull(DLookup("Name", "MSysObjects", "Name='FinalTable' And Type In (1,4,6)")) Then
         'Table Exists

         DoCmd.DeleteObject acTable, "FinalTable"

    Else

     'Table not exists
     End If

    xcess.DoCmd.OpenQuery "start1a"
    xcess.DoCmd.OpenQuery "start1b"
    xcess.DoCmd.OpenQuery "start2"
    xcess.DoCmd.OpenQuery "start3"

    xcess.Quit

    Dim TextFileConn As ADODB.Connection
    Dim TextFileData As ADODB.Recordset
    Dim TextFileField As ADODB.Field

    Set TextFileConn = CreateObject("ADODB.Connection")
    Set TextFileData = CreateObject("ADODB.Recordset")

    TextFileConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & objectname

    TextFileConn.Open

    With TextFileData
    .ActiveConnection = TextFileConn
    .Source = "FinalTable"
    .LockType = adLockReadOnly
    .CursorType = adOpenForwardOnly
    .Open

    End With

    Worksheets.Add
    ActiveSheet.Name = "DATA from ACCESS"

    For Each TextFileField In TextFileData.Fields
        ActiveCell.Value = TextFileField.Name
        ActiveCell.Offset(0, 1).Select
    Next TextFileField

    Range("A1").Select
    Range("A2").CopyFromRecordset TextFileData
    Range("A1").CurrentRegion.EntireColumn.AutoFit

    TextFileData.Close
    TextFileConn.Close

    SecondsElapsed = Round(Timer - StartTime, 2)
    MsgBox ("Loading Data Complete! Time elasped: " & SecondsElapsed & " seconds")

    End Sub

那么如何更改我的变量呢? 非常感谢你。

0 个答案:

没有答案