Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with message box when closing form

Status
Not open for further replies.

NeilT123

Technical User
Jan 6, 2005
302
GB
I have a data input form which has an unbound combo box cbCroppingYear. On opening the form the focus is given to the combo box.

The Combo Box is used as a filter so to make sure that the user selects a year I have the following code in the lost focus of the combo box.

Code:
Private Sub cbCroppingYear_LostFocus()
If IsNull(Me!cbCroppingYear) Or Me!cbCroppingYear = "" Then
MsgBox "You must select a cropping year"
Me!cbCroppingYear.SetFocus
End If
End Sub

If the combo box is empty and the user moves away then the message box shows but on clicking OK the focus moves away from cbCroppingYear. Am I right to use LostFocus and how do I get the focus back to the combo box?

Finally, if the user opens the form in error and then navigates back to the main switchboard by closing the form they get the message box, is there any way around this?

Thanks as always for any suggestions.
 
I sorted this out by putting a default value into the combo box cbCroppingYear.
 
Did you know there is a null check with default to make writing if statements easir..

Code:
If IsNull(Me!cbCroppingYear) Or Me!cbCroppingYear = "" Then

becomes

Code:
If Nz(Me!cbCroppingYear,"") = "" Then

As you put a default value in it now, is there any point in checking if it's blank?

What if a user clears the field then tries to close the form because they changed their mind?

Perhaps the filter functionality should only perform a filter if that field has a valid date in it?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
You were simply using the wrong event! Should have used the OnExit event, which can be Canceled, like this:
Code:
Private Sub cbCroppingYear_Exit(Cancel As Integer)

If IsNull(Me!cbCroppingYear) Or Me!cbCroppingYear = "" Then
 MsgBox "You must select a cropping year"
 Cancel = True
End If

End Sub

The Missinglinq

Richmond, Virginia

The Devil's in the Details!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top