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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MsgBox Code Not Working. 2

Status
Not open for further replies.

roaml

Technical User
Feb 19, 2002
264
US
Hi,

I am trying to use the following code to alert users that a field was left blank. I would like the shift field completed before closing the form or moving on to a new record.

I have tried placing the code in various events procedures, but it is just not working.

[BEGIN CODE]
Private Sub Shift_LostFocus()

Dim Title As String

Title = "Shift Field"

If Me![fsd_shift] = "" Then
Beep
MsgBox "Please select a Shift from the items lilsted.", vbOKOnly, Title
Me![fsd_shift].SetFocus
End If

End Sub
[END CODE]

Much appreciated.
 
Looks to me like the name of you text box is Shift and not fsd_shift? This would be the case if this is the lost focus event for the text box you are testing.
Code:
Private Sub [b]Shift[/b]_LostFocus()

Dim Title As String

Title = "Shift Field"

[red]If Me![shift] = "" Then[/red]
     Beep
     MsgBox "Please select a Shift from the items lilsted.", vbOKOnly, Title
     Me![fsd_shift].SetFocus
End If

End Sub
 
sorry, but i didn't look close enough. Try this.
Code:
If IsNull(Me![SHIFT]) Then
     Beep
     MsgBox "Please select a Shift from the items lilsted.", vbOKOnly, Title
'     Me![SHIFT].SetFocus
End If

Where you set the focus back to the text box caused the event to keep firing. That's why I REM'd it out.
 
A safe location (on bound form) to do data validation is the BeforeUpdate event procedure of the Form:
If Trim(Me![shift] & "") = "" Then
Beep
MsgBox "Please select a Shift from the items listed.", vbOKOnly, "Shift Field"
Me![shift].SetFocus
Cancel = True
Exit Sub
End If
' Another checks here if required

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks jadams0173, that worked.

I uncommented the Setfocus and the cursor did not return to the Shift field. I would really like to lock the user in until an item is selected. How can I adapt the code to perform this action?

Thanks a bunch!
 
I'm not 100% on this one. But I think it doesn't set the focus back because it is actually in the LostFocus event...that's just a SWAG though.

As PHV recomended, it may be better to use the BEFOREUPDATE event. This may also solve the the SetFocus problem. I'd give his idea a shot.

His IF...THEN statement is also a better way! Keeps you from saving a ' ' as data...
 
Thank you PHV. The code actually works well in the “On Exit” event.

Thanks again to the both of you for your timely responses and valuable suggestions.

[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top