Force new record on main form if no data is changed by user

Michael Paravicini
Michael Paravicini used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
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

Author

Commented:
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!
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Try using in the OnLoad event of the form:

Me!SomeBoundFieldToEdit.SetFocus
Me.Dirty = True

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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!
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Oh, that sounds strange. But if it works, then be it.
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
You also need to add:

Me.Repaint
Me!SomeBoundFieldToEdit.SetFocus
Me.Dirty = True

 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.

Author

Commented:
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:

- Load - Subform
- Current - Subform
- Activate - Mainform

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
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
mpim,

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)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

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

Jim.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial