Avatar of Michael Paravicini
Michael ParaviciniFlag for Chile

asked on 

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

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

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
Avatar of 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!
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Michael Paravicini

ASKER

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!
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Oh, that sounds strange. But if it works, then be it.
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.
Avatar of 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:

- 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
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

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

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo