Force new record on main form if no data is changed by user
I have a main form (1 reservation header) and a subform (with many reservation details). As I know the reservation header info I fill out the fields and hence the user can move immediately to the subform and add the reservation details and only change the header if necessary. The problem I have, is how to force a new record at the Load event of the main form. Nothing I have tried so far worked. Even if i try to fill some dummy data in one of fields of the main form and the erase it again - it does not create a new record. If I use lostfocus at the first field Access dies (not sure why). However, the form works fine if the user manually changes any of the main form data. Any help is greatly appreciated! Thanks Michael
Microsoft Access
Last Comment
Jim Dettman (EE MVE)
8/22/2022 - Mon
Dale Fye
Usually, if you use syntax similar to:
docmd.OpenForm "formname", , , , acFormAdd
it should open the form to a new record, but you might also check to make sure that the forms AllowAdditions property is set to Yes.
Are you displaying the navigation buttons on the main reservation form? If so, you should be able to click the "new record" button in the navigation bar.
Dale
Michael Paravicini
ASKER
Thank you Dale and yes to all of your questions. I use acFormAdd, AllowAdditions is set to yes. I do show the navigation bar in the subform but not in the main form for control purpose. The system did work correctly some time ago (ie adding new record) but suddenly it changed the behaviour and now only adds a new record when i specifically change a field value in the main form. I cannot figure out what i (inadvertentely) changed to get this new and unwelcome behaviour. Any help would really be appreciated!
Thank you Gustav - this worked perfectly. However, i found something really strange. The OnLoad event does not trigger on loading of the main form but only after i fill in the first field in the main form. Hence, i moved the code to OnOpen which works fine.. I will reenter the code to see if this fixes it... Again thank you!
If you plan to use the OnOpen event. Controls are not guaranteed to exist until the OnLoad event and the recordset may or may not be open yet. In fact I'm surprised that works.
<<As I know the reservation header info I fill out the fields and hence the user can move immediately to the subform and add the reservation details and only change the header if necessary.>>
How are you filling in the controls? If you do a .SetFocus to a control, then Me.mycontrol.text = "some value", then that should dirty the record.
I would explore this a bit more and figure out something other than using the OnOpen.
Jim.
Michael Paravicini
ASKER
Thank you Jim - the problem is that if I move everything to OnLoad (as I had it before) the event is NOT fired until after the user enters a data. No clue why I have this behaviour. The sequence of the events is:
At this point the screen is shown for data input. Now if I enter any data on the main form it shows the following events:
- Current - Subform (not yet sure what triggers this)
At only once I move to the next field on the Mainform does the
- Load - Main form trigger happen!
Again, no clue why and this is the reason why I moved everything to Open. Cheers Michael
I frequently leaave the SourceObject of the subform blank, and actually hide the subform control.
This way the main form loads first.
I then use the main forms Current event to check to see whether it is on a new record. If not, I then populate the subforms SourceObject property and LinkMaster/LinkChild properties and make the subform visible.
Then, in the Forms AfterUpdate event I make sure that the subform is displayed for further data entry.
Dale
Jim Dettman (EE MVE)
<<At only once I move to the next field on the Mainform does the
- Load - Main form trigger happen! >>
That almost sounds like a bug; what version of Access is this?
The subform events do before the main's, but the OnLoad should be occurring (assuming it is bound) and the OnCurrent as well.
docmd.OpenForm "formname", , , , acFormAdd
it should open the form to a new record, but you might also check to make sure that the forms AllowAdditions property is set to Yes.
Are you displaying the navigation buttons on the main reservation form? If so, you should be able to click the "new record" button in the navigation bar.
Dale