简体   繁体   English

宏仅在运行时错误462之前运行一次excel vba:远程服务器不存在或不可用

[英]Macro only runs once excel vba before Runtime error 462: The remote server does not exist or is unavailble

So I have this code in vba which exports data from excel to access. 所以我在vba中有此代码,可将数据从excel导出到Access。 This works fine running the first time and then running the second time "Runtime error 462: The remote server does not exist or is unavailable" shows up. 第一次运行,然后第二次运行,此方法运行正常,显示“运行时错误462:远程服务器不存在或不可用”。

However if i was to restart excel, then it seems to works fine again the first time. 但是,如果我要重新启动excel,那么第一次似乎还可以正常工作。 I've tried googling solutions however most of them say set any objects to Nothing and empty variables and it still doesn't work. 我尝试了谷歌搜索解决方案,但是大多数人都说将任何对象设置为Nothing并清空变量,但仍然无法正常工作。

Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:\Users\User 1\Documents\Database21.accdb"
acc.DoCmd.TransferSpreadsheet _
        TransferType:=acImport, _
        SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
        TableName:=myValue, _
        Filename:=Application.ActiveWorkbook.FullName, _
        HasFieldNames:=True, _
        Range:="Sheet2$A1:AL104"
        CurrentDb.TableDefs(myValue).Fields("F4").Properties!ColumnWidth = 2500
        CurrentDb.TableDefs(myValue).Fields("F7").Properties!ColumnWidth = 2500

acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
myValue = Empty
MsgBox " The data has been exported"

Application.DisplayAlerts = False
Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.Delete
Range("A1").Select
Application.DisplayAlerts = True
End Sub

The below snippet of code is where this error seems to show up when running the second time round. 下面的代码片段是第二轮运行时似乎显示此错误的位置。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks 谢谢

CurrentDb.TableDefs(myValue).Fields("F4").Properties!ColumnWidth = 2500

I've gone for a code tidy-up approach which should sort the problem. 我去过一种代码整理方法,应该可以解决问题。 Examinig each of the objects, from the code break line, in the Immediate Window would also help (eg what does "?Currentdb.Name" return) 在立即窗口中从代码中断行检查每个对象也有帮助(例如,“?Currentdb.Name”返回什么)

1) VBA supports New as part of an object declaration, but... (best you research this bit. From memory it's always slower 'cause vba has to re-check if the object has been instantiated.) 1)VBA在对象声明的一部分中支持New,但是...(最好研究一下。从内存来看,它总是比较慢,因为vba必须重新检查对象是否已实例化。)

Dim acc As Access.Application: Set acc = New Access.Application

2a) Qualify your calls to CurrentDb 2a)限定您对CurrentDb的呼叫

acc.CurrentDb.TableDefs(myValue).Fields("F4").Properties!ColumnWidth = 2500
acc.CurrentDb.TableDefs(myValue).Fields("F7").Properties!ColumnWidth = 2500

2b) Even better, explicitly reference each of the objects in the tree [this will help with begugging] 2b)更好的是,显式引用树中的每个对象[这将有助于解决问题]

' add Reference to "Microsoft DAO 3.6 Object Library"
Dim db As DAO.Database
Dim tdf As DAO.TableDef

Set db = acc.CurrentDb
db.TableDefs.Refresh    ' CurrentDb should do this, but can't hurt
Set tdf = db.TableDefs(myValue)
With tdf
    .Fields("F4").Properties!ColumnWidth = 2500
    .Fields("F7").Properties!ColumnWidth = 2500
End With

Set tdf = Nothing
Set db = Nothing

I hope that this helps, GraemeR 我希望这会有所帮助,GraemeR

暂无
暂无

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

相关问题 Excel 2010 VBA运行时错误462:远程服务器计算机不存在或不可用 - Excel 2010 VBA runtime Error 462: The remote server machine does not exist or is unavailable 在 VBA 中打开 Excel 文件:运行时错误“462”:远程服务器计算机不存在或不可用 - Opening Excel file in VBA: Run-time error ‘462’: The remote server machine does not exist or is unavialiable 错误 462:通过 Excel VBA 使用 Word 时远程服务器计算机不存在 - Error 462: The remote server machine does not exist when working with Word via Excel VBA 使用VBA运行时错误462在Excel中合并Docx文件远程服务器计算机,然后文件损坏 - Merging Docx Files in Excel using VBA Runtime Error 462 The remote server machine, then File Corrupted 当Internet Explorer自动化脚本运行时,在VBA中遇到“错误462:远程服务器计算机不存在或不可用” - Encountering “Error 462: The remote server machine does not exist or is unavailable” in VBA when an internet explorer automation script is running 通过 Outlook 发送 email 后 VBA 错误 462“远程服务器计算机不存在或不可用” - VBA error 462 “The remote server machine does not exist or is unavailable” after sending email via Outlook 使用 Excel 对象访问代码,错误:462 远程服务器机器不存在或不可用 - Access code using Excel object, error: 462 The remote server machine does not exist or is unavailable 第二次在EXCEL中运行VBA代码时,出现“运行时错误462:远程服务器计算机不存在或不可用”(获取访问表) - “Run-time error 462: The remote server machine does not exist or is unavailable” when running VBA code in EXCEL for a second time (get ACCESS table) 使用Word的运行时错误462 Excel VBA - Runtime error 462 Excel VBA using Word 第二次运行 VBA 代码时出现“运行时错误 462:远程服务器计算机不存在或不可用” - "Run-time error 462 : The remote server machine does not exist or is unavailable" when running VBA code a second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM