Link to home
Start Free TrialLog in
Avatar of gordonwwaugh
gordonwwaughFlag for United States of America

asked on

Default value for combo box does not trigger afterUpdate event

I have code that must execute when the value of a combo box field is entered for a new record. I also want to assign a default value to this field. Unfortunately, the afterUpdate event does not occur if the default value is used; it only occurs if the user selects a value.
   I tried removing the default value, and then writing some code that assigns the value in the box's gotFocus event. The afterUpdate event still isn't triggered--even though the combo box is definitely getting focus (i.e., the VBA code is successfully entering the default value into the box).
   How can I, essentially, assign a default value for a field, for new records, but also have the afterUpdate event occur regardless of whether the user selects a value or the default value is used?
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Default values on the property sheet only apply to New records.

You can also use the Form BeforeInsert event to assign default values in code.

Are you talking about the AfterUpdate of the Form or the control using the Default Value?

mx
Avatar of gordonwwaugh

ASKER

Right. I'm only interested in new records, so it seems default values  on the property sheet would normally be appropriate.

Whenever I mention the afterUpdate event, I'm talking about the afterUpdate event of the control.
"I'm only interested in new records, so it seems default values  on the property sheet would normally be appropriate."

Well ... unfortunately, the DV just doesn't work that way.  

May I ask why you need a Control's AU to trigger at the time the DV is set?

*Normally* ... there would be other controls (with no DV) wherein data would be entered ... resulting in their AU to be triggered ... and ultimately the Form's AU.

mx


You are correct. Other controls are always going to trigger the afterUpdate event for the form. For some reason, I had not considered putting the code in the form's afterUpdate event instead of the control's afterupdate event. That should work. I'll try it and see what happens.

By the way, the code that executes in the afterUpdate event inserts several records in a subform and assigns values to its fields. The field values differ depending upon the value chosen in the main form's combo box.
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Having the current data already saved is definitely an advantage.

This does bring up one other problem, however. The me.newrecord property is always false in the afterUpdate event procedure. So I need to use another way to distinguish between new records and existing records. I suppose I could declare a form-level variable (e.g., fNewRecord) and assign its value in the form's beforeUpdate event. Then it would be available in the form's afterUpdate event. Unless you have a better idea.
NewRecord will be 'active' in all events prior to AU, for example BeforeUpdate, OnCurrent, etc.  But yes, your flag idea is necessary if you need that info ... and a good way to do it.

Just remember to set the flag to False in the AU ... after use :-)

mx
It works!

Creating the fNewRecord form-level variable, as described above, fixes the new problem.

By the way, the assigning a default value in the form's beforeInsert proc was not allowed because the record is created from a modal dialog box. So I used the property sheet's default value.

Thanks very much!