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. 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.
However if i was to restart excel, then it seems to works fine again the first time. I've tried googling solutions however most of them say set any objects to Nothing and empty variables and it still doesn't work.
Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:UsersUser 1DocumentsDatabase21.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
excel vba
add a comment |
So I have this code in vba which exports data from excel to 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.
However if i was to restart excel, then it seems to works fine again the first time. I've tried googling solutions however most of them say set any objects to Nothing and empty variables and it still doesn't work.
Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:UsersUser 1DocumentsDatabase21.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
excel vba
add a comment |
So I have this code in vba which exports data from excel to 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.
However if i was to restart excel, then it seems to works fine again the first time. I've tried googling solutions however most of them say set any objects to Nothing and empty variables and it still doesn't work.
Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:UsersUser 1DocumentsDatabase21.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
excel vba
So I have this code in vba which exports data from excel to 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.
However if i was to restart excel, then it seems to works fine again the first time. I've tried googling solutions however most of them say set any objects to Nothing and empty variables and it still doesn't work.
Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:UsersUser 1DocumentsDatabase21.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
excel vba
excel vba
asked Nov 22 '18 at 23:01
xyzabc12341000
51
51
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 (e.g. what does "?Currentdb.Name" return)
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.)
Dim acc As Access.Application: Set acc = New Access.Application
2a) Qualify your calls to 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]
' 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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438895%2fmacro-only-runs-once-excel-vba-before-runtime-error-462-the-remote-server-does%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 (e.g. what does "?Currentdb.Name" return)
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.)
Dim acc As Access.Application: Set acc = New Access.Application
2a) Qualify your calls to 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]
' 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
add a comment |
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 (e.g. what does "?Currentdb.Name" return)
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.)
Dim acc As Access.Application: Set acc = New Access.Application
2a) Qualify your calls to 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]
' 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
add a comment |
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 (e.g. what does "?Currentdb.Name" return)
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.)
Dim acc As Access.Application: Set acc = New Access.Application
2a) Qualify your calls to 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]
' 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
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 (e.g. what does "?Currentdb.Name" return)
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.)
Dim acc As Access.Application: Set acc = New Access.Application
2a) Qualify your calls to 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]
' 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
answered Nov 23 '18 at 2:56
GraemeR
661
661
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438895%2fmacro-only-runs-once-excel-vba-before-runtime-error-462-the-remote-server-does%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown