Form reset to default after hitting Esc button

mabrenna

New member
Local time
Today, 13:00
Joined
Apr 5, 2013
Messages
5
Hello,

I have a form that contains a combo box along with other controls. The combo box On_Change event triggers changes to other controls' properties (visible-true/false, location, enabled,etc.) I have a default value for the combo box as well as code to call the On_Change event when navigating to other records.

If I'm in a new record and started typing a value in a textbox, Access assigns an ID (record ID/primary key which is an autonumber) even though it's not technically saved. If I hit the Esc button, that value is erased. This is fine, but if at that point I hit Esc again it resets the form to a brand new record (no record ID). The combo box value goes back to the default value; however, the combo box's On_Change event doesn't get triggered for some reason. Textboxes that should be hidden are still visible. I can't figure out why it's not getting triggered when using Esc to get back to a new record.

Any suggestions would be greatly appreciated!
 
I'm currently not on a machine with Access, so can't test, however I think you probably need to have some code in the form's On Current event, to set the visible property as appropriate.
 
Hi John,

I'm already calling the On Change event function inside the Form's On_Current event so that can't be it. Thanks though.
 
Quick thought, have you set any command buttons as the default button / cancel button in the properties(Other tab)?
I use this to set my Exit button Cancel property as Yes, so when anyone presses Esc, it activates this button and the code attached to it
 
m,

You probably don't want the combo's OnChange event.

It should probably be the Before Or After Update event.

hth,
Wayne
 
Hi Vera and Wayne,

Vera,
I tried setting up a default button with the settings you mentioned. I threw in some code to display a message box as a test. When I pressed Esc the first time it triggered the button's code. Hitting Esc undid the typing I had done, but still left the record as dirty, which is fine. When I hit Esc a second time the default button didn't get triggered. It turned the record into a brand new record and dirty is equal to false.

I see that you can use the dirty event, but this is kind of the opposite. It's going from dirty to not dirty and I haven't found an event that handles this. Any ideas?

Wayne,
I had already tried calling the function in both the before and after update events with no luck. I would have thought that trying those, along with Form_Current that one of those would have done it, but none of these work. Every other situation such as navigating records, creating a new record, after saving a record (which creates a new record) works fine. It's only causing problems when I'm in a new record that is dirty and I want to Esc out of it which would undo any data entry I had done up to that point.

P.S. This is my first major project in Access so I'm still a beginner.
 
Last edited:
when you press escape, you don't go to a new record, so the current event does not fire - what happens is that all entries affecting the edited record are cleared, so you just get either the unedited values of an existing record, or a blank (clean) record for a new record. the code you are using in the current event will no doubt be dirtying the record.

there is no event, so I can't see how you can re-fill your combo box at that point.

put a record selector on the form, and you will see the pencil (dirty) change to a triangle (clean)

personally, i prefer not to dirty records in the current event, as doing this with a new record might lead you to get partially edited records in your table.

you could use an unbound combo box, which would not get cleared by pressing the escape, but it depends what you are doing with it.
 
Hi Gemma,

I have a form along with a subform. Think of it as an order header and order detail section (1 to many). My subform isn't static. I have numerous textboxes whose properties are driven from a combo box's value. For instance, a value of 1 may result in textboxes 1 and 2 to be visible, where a value of 2 in the combo box results in textboxes 3 and 4 to be visible. The form_current event ensures the correct textboxes are visible when searching through records.

When I hit escape it's clearing out the values on a new record, which sets the combo box value back to the default. Unfortunately the VBA has already overriden the default properties as soon as the combo box's value was changed. Like you said, it's not treated as event so I can't think of how I can get the rest of the controls to follow in the footsteps of the combo box. I'm not sure how adding an unbound combo box would help. Can you please explain what you had in mind? Thanks!
 
you could use an unbound combo box, as that won't clear, when you press escape

the thing is, if you press escape, it should be because you want to start over - becuase that's what it will do,

if you have an initiallisation process for the form/subform - then you could add a "new record" button on the form to run your initiallisation sub.

This sort of issue is quite tricky, especially with a subform. However, the act of entering the sub form to enter the order details (or vice versa) should save the current record on the container form, so you shouldn't be able to completely "unedit" (iie clear) - the main form at that point, by pressing escape.

maybe you could put some code in the on enter or got focus event (or similar) of the subform, to make sure the details on the main form are valid.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom