[英]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.