Why does MS-Access fire an Enter event for a Save record button when opening a form?
I have to process access and excel files from different departments in my MS-Access database.
The user must select these files, so I created a table of departments in which the user must fill in file names.
On the form to fill in these file names, I added a button to open a file selection dialog.
I created this button as a Save Record button and put the logic in the On Enter
event.
Now each time the user opens the form, the file selection dialog already opens for the first file, before the form is even shown.
Why does this happen and how can I prevent it?
For completeness: this is my code for the Enter event:
Private Sub SelectCmd_Enter()
On Error GoTo hell
Dim FileDlg As FileDialog
' If is_loaded Then
Select Case Me.Controls("Filter")
Case "Excel File"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Excel File", "*.xlsx; *.xlsm; *.xls"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Access DB"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Access DB", "*.accdb"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Folder"
Set FileDlg = Application.FileDialog(msoFileDialogFolderPicker)
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, ""))
End Select
FileDlg.Title = Me.Controls("Caption").Value
If FileDlg.Show Then
Me.Controls("FilePath").Value = FileDlg.SelectedItems.Item(1)
End If
Set FileDlg = Nothing
' Else
' is_loaded = True
' End If
Exit Sub
hell:
Debug.Print Err, Error
Debug.Assert False
Resume
End Sub
I tried solving the isue by setting a private boolean is_loaded
to false when loading the form:
Private is_loaded As Boolean
Private Sub Form_Activate()
is_loaded = False
[...]
End Sub
but then the SelectCmd
button on the first line did not react to clicks anymore when the file was loaded.
vba forms ms-access data-binding access-vba
|
show 1 more comment
I have to process access and excel files from different departments in my MS-Access database.
The user must select these files, so I created a table of departments in which the user must fill in file names.
On the form to fill in these file names, I added a button to open a file selection dialog.
I created this button as a Save Record button and put the logic in the On Enter
event.
Now each time the user opens the form, the file selection dialog already opens for the first file, before the form is even shown.
Why does this happen and how can I prevent it?
For completeness: this is my code for the Enter event:
Private Sub SelectCmd_Enter()
On Error GoTo hell
Dim FileDlg As FileDialog
' If is_loaded Then
Select Case Me.Controls("Filter")
Case "Excel File"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Excel File", "*.xlsx; *.xlsm; *.xls"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Access DB"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Access DB", "*.accdb"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Folder"
Set FileDlg = Application.FileDialog(msoFileDialogFolderPicker)
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, ""))
End Select
FileDlg.Title = Me.Controls("Caption").Value
If FileDlg.Show Then
Me.Controls("FilePath").Value = FileDlg.SelectedItems.Item(1)
End If
Set FileDlg = Nothing
' Else
' is_loaded = True
' End If
Exit Sub
hell:
Debug.Print Err, Error
Debug.Assert False
Resume
End Sub
I tried solving the isue by setting a private boolean is_loaded
to false when loading the form:
Private is_loaded As Boolean
Private Sub Form_Activate()
is_loaded = False
[...]
End Sub
but then the SelectCmd
button on the first line did not react to clicks anymore when the file was loaded.
vba forms ms-access data-binding access-vba
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
2
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
2
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
1
@Dirk Horsten: This is by design. TheClick
event fires, when you click, and theEnter
event fires when the related control receives the focus (actually, even before that). The online help says:The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32
|
show 1 more comment
I have to process access and excel files from different departments in my MS-Access database.
The user must select these files, so I created a table of departments in which the user must fill in file names.
On the form to fill in these file names, I added a button to open a file selection dialog.
I created this button as a Save Record button and put the logic in the On Enter
event.
Now each time the user opens the form, the file selection dialog already opens for the first file, before the form is even shown.
Why does this happen and how can I prevent it?
For completeness: this is my code for the Enter event:
Private Sub SelectCmd_Enter()
On Error GoTo hell
Dim FileDlg As FileDialog
' If is_loaded Then
Select Case Me.Controls("Filter")
Case "Excel File"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Excel File", "*.xlsx; *.xlsm; *.xls"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Access DB"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Access DB", "*.accdb"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Folder"
Set FileDlg = Application.FileDialog(msoFileDialogFolderPicker)
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, ""))
End Select
FileDlg.Title = Me.Controls("Caption").Value
If FileDlg.Show Then
Me.Controls("FilePath").Value = FileDlg.SelectedItems.Item(1)
End If
Set FileDlg = Nothing
' Else
' is_loaded = True
' End If
Exit Sub
hell:
Debug.Print Err, Error
Debug.Assert False
Resume
End Sub
I tried solving the isue by setting a private boolean is_loaded
to false when loading the form:
Private is_loaded As Boolean
Private Sub Form_Activate()
is_loaded = False
[...]
End Sub
but then the SelectCmd
button on the first line did not react to clicks anymore when the file was loaded.
vba forms ms-access data-binding access-vba
I have to process access and excel files from different departments in my MS-Access database.
The user must select these files, so I created a table of departments in which the user must fill in file names.
On the form to fill in these file names, I added a button to open a file selection dialog.
I created this button as a Save Record button and put the logic in the On Enter
event.
Now each time the user opens the form, the file selection dialog already opens for the first file, before the form is even shown.
Why does this happen and how can I prevent it?
For completeness: this is my code for the Enter event:
Private Sub SelectCmd_Enter()
On Error GoTo hell
Dim FileDlg As FileDialog
' If is_loaded Then
Select Case Me.Controls("Filter")
Case "Excel File"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Excel File", "*.xlsx; *.xlsm; *.xls"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Access DB"
Set FileDlg = Application.FileDialog(msoFileDialogFilePicker)
FileDlg.Filters.Add "Access DB", "*.accdb"
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "")) & Me.Controls("FileNameTemplate").Value
Case "Folder"
Set FileDlg = Application.FileDialog(msoFileDialogFolderPicker)
FileDlg.InitialFileName = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, ""))
End Select
FileDlg.Title = Me.Controls("Caption").Value
If FileDlg.Show Then
Me.Controls("FilePath").Value = FileDlg.SelectedItems.Item(1)
End If
Set FileDlg = Nothing
' Else
' is_loaded = True
' End If
Exit Sub
hell:
Debug.Print Err, Error
Debug.Assert False
Resume
End Sub
I tried solving the isue by setting a private boolean is_loaded
to false when loading the form:
Private is_loaded As Boolean
Private Sub Form_Activate()
is_loaded = False
[...]
End Sub
but then the SelectCmd
button on the first line did not react to clicks anymore when the file was loaded.
vba forms ms-access data-binding access-vba
vba forms ms-access data-binding access-vba
edited Nov 22 '18 at 10:25
June7
4,28451126
4,28451126
asked Nov 22 '18 at 9:19
Dirk HorstenDirk Horsten
2,2051329
2,2051329
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
2
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
2
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
1
@Dirk Horsten: This is by design. TheClick
event fires, when you click, and theEnter
event fires when the related control receives the focus (actually, even before that). The online help says:The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32
|
show 1 more comment
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
2
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
2
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
1
@Dirk Horsten: This is by design. TheClick
event fires, when you click, and theEnter
event fires when the related control receives the focus (actually, even before that). The online help says:The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
2
2
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
2
2
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
1
1
@Dirk Horsten: This is by design. The
Click
event fires, when you click, and the Enter
event fires when the related control receives the focus (actually, even before that). The online help says: The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32
@Dirk Horsten: This is by design. The
Click
event fires, when you click, and the Enter
event fires when the related control receives the focus (actually, even before that). The online help says: The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32
|
show 1 more comment
0
active
oldest
votes
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%2f53427493%2fwhy-does-ms-access-fire-an-enter-event-for-a-save-record-button-when-opening-a-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53427493%2fwhy-does-ms-access-fire-an-enter-event-for-a-save-record-button-when-opening-a-f%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
Please supply the code you have, expected result.
– krish KM
Nov 22 '18 at 9:23
2
Why would you use the Enter event of a command button instead of Click event?
– June7
Nov 22 '18 at 9:33
Thanks June7. With the Click event is works fine. If you can also explain why, please share your knowledge in an answer.
– Dirk Horsten
Nov 22 '18 at 10:27
2
Most likely the button gets focus when the form opens. Possibly because its TabIndex property is set to 0.
– June7
Nov 22 '18 at 10:32
1
@Dirk Horsten: This is by design. The
Click
event fires, when you click, and theEnter
event fires when the related control receives the focus (actually, even before that). The online help says:The Enter event occurs before a control actually receives the focus from a control on the same form.
– Unhandled Exception
Nov 22 '18 at 10:32