• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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?
0
gordonwwaugh
Asked:
gordonwwaugh
  • 5
  • 4
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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
0
 
gordonwwaughAuthor Commented:
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.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gordonwwaughAuthor Commented:
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.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
In your case then, I would definitely recommend using the Form's AU ... at which point the current data is saved.

mx
0
 
gordonwwaughAuthor Commented:
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.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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
0
 
gordonwwaughAuthor Commented:
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!
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Good job!

you are welcome ....

mx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now