On Exit Event

PA232

New member
Local time
Today, 08:47
Joined
Feb 16, 2009
Messages
5
Hi,

I have a subform with numerous data validation rountines attached to the On Exit events of the controls. They do exactly what I want them to do, but I started to get errors that referred to these On Exit events when I returned to the main form and tried to go to a new record. For example, this is the code from one of my controls:

Private Sub WallAngleLength_Exit(Cancel As Integer)
If IsNull(WallAngleLength) Then
MsgBox "Wall Angle Length must be selected"
WallAngleLength.SetFocus
Cancel = True
Else
If GuideDrawingNum = "400106" Or GuideDrawingNum = "400108" Then
MiddleAngleLength.Enabled = False
OutsideAngleLength.Enabled = False
End If
End If
End Sub

Once the subform is completed and all controls are completed properly, I move back to the main form. At this time, when I attempt to move to a new record I begin to get message box "Wall Angle Length must be selected". It may pop up 4 or 5 times before it allows me to enter data into the new main form record. The msgbox that pops up doesn't always reference the same control from the subform, it is very erratic.

Any ideas as to why this is occuring?

Thanks
Kenton
 
To start with I use the before update event so that I can cancel the event. Maybe you can put your validataion code there - ?
 
I tried that, but when I make a selection from my drop down box that is not correct, I get the error message and control stays in that control. The problem is that at this point I can hit the "Tab" key and I move to the next control while allowing a value that is not correct.
 
You kind of have to mix and match what you want. If you want all checks to be done before the record is saved then then before update is usefull. If you want to validate an individual control the you can use the control's on exit event.
 
The problem with using the OnExit event of a textbox like this is that the OnExit event fires every time you leave the textbox. It will fire even if you're simply "tabbing through," as you might do before you're actually ready to enter WallAngleLength, and it'll fire each time you pass thru the textbox, even after data has been entered correctly.

Using the textbox BeforeUpdate event causes the code to only execute if data has been entered/edited in the control.

Another problem with using the control's OnExit event to validate that the textbox isn't empty is that the user might not ever enter the textbox, in which case WallAngleLength can remain empty without the user being warned that they need to enter it.

Validation like this to force a user to enter data in a required field really needs to be done in the Form_BeforeUpdate.

What was happening to the OP here was that he was in the WallAngleLength field, which was empty, then tried to return to the main form. Since this caused the WallAngleLength OnExit event to fire, focus was returned to the textbox, over and over again as he repeatedly tried to go to the main form. The same code in the WallAngleLength_BeforeUpdate event wouldn't cause a problem, in this circumstance.
 
Last edited:
I can see the error in my ways by forcing everything into the On Exit events. Controls such as WallAngleLength can certainly be switched to Before Update for proper data validation. I will certainly remove those that can be removed. My problem is that I don't think that I can remove them all. I have certain controls that are dependent upon other controls as far as whether or not they are enabled. For example,

If ComboGuideDrawingNum = "400112" Then
ComboGuideConfig = "B"
ComboGuideType = "JAMB"
ComboBJGuideType.Enabled = True
ComboBelowHPJambSlotSize.Enabled = True
ComboHPAreaJambSlotSize.Enabled = True
BelowHPJambSlotSpace.Enabled = True
ComboGuideConfig.Enabled = False
ComboGuideType.Enabled = False
MiddleAngleLength.Enabled = True
OutsideAngleLength.Enabled = True
If Forms![frmOrders]![DoorSeries] <> "FD" Then
MsgBox "Guide Drawing # 400112 requires that Door Series = FD (Fire Door)."
ComboGuideDrawingNum.SetFocus
'Cancel = True
End If
End If

I never have problems editing data, my problems only occur when entering new records. The 1st record, main and subform, work perfectly. The MsgBoxes pop up when I move back to the main form and attempt to go to the new record. It is giving me the MsgBoxes from the subform's On Exit controls. All of data in the 1st record entered is correct, but it is acting as if it is reading the blank subform record and thus giving me the MsgBox's that in all reality are correct, because all of the fields are blank in the new record. So why, when I move to the new main form record, does it fire the On Exit events from the subform?

Thanks for your help
Kenton
 

Users who are viewing this thread

Back
Top Bottom