We help IT Professionals succeed at work.

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

Michael Paravicini
on
65 Views
Last Modified: 2018-10-01
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

Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
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!
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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!
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017

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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2017

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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.