Why does MS-Access fire an Enter event for a Save record button when opening a form?












0















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.
enter image description here



I created this button as a Save Record button and put the logic in the On Enter event.
enter image description here



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.










share|improve this question

























  • 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 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


















0















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.
enter image description here



I created this button as a Save Record button and put the logic in the On Enter event.
enter image description here



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.










share|improve this question

























  • 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 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
















0












0








0








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.
enter image description here



I created this button as a Save Record button and put the logic in the On Enter event.
enter image description here



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.










share|improve this question
















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.
enter image description here



I created this button as a Save Record button and put the logic in the On Enter event.
enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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





















  • 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 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



















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














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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'