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

Forcing user to enter something in a text box 1

Status
Not open for further replies.

cpc34

Technical User
Aug 16, 2005
22
GB
I have a text box in a form that I want to force the database user to change. I have put a default value in the box (Clock No.), and in the "on close" event I want to put an IF loop in so that when you click on the cross in the top right corner it checks if the text box still says "Clock No.". if it does I want a message such as "Enter Clock Number" to come up, and the form not to close. This way the user cannot add an entry that doesn't have a clock number. Is this possible without putting in a custom made button (which I am reluctant to do in case people choose not to use it).
 
If Clock No. is a bound control and the user WILL change some other value on the form, then put your check in the BeforeUpdate event of the form. Something like this:

If ([txtClockNum] = "Clock No") then
MsgBox "...."
Cancel = True
Exit Sub
End if
 
use the forms unload event to test for the value

Private Sub Form_Unload(Cancel As Integer)
If Me.Text0 = clockno Then
Cancel = True
msgbox "need number"
end if
End Sub
 
also something like will do
Code:
Private Sub txtClockNumber_Exit(Cancel As Integer)
If Me.txtClockNumber="Clock No." Then
    MsgBox "Please enter a clock number"
    Me.Anothercontrol.SetFocus
    Me.txtClockNumber.SetFocus
End If
End Sub

________________________________________________________________________
Zameer Abdulla
Visit Me
Children are poor men's riches.
 
You can do all this from the field definition itself. Specify that it is "Required" and add a useful text thing for access to display should it be blank. Look at the properties where the field is defined.

Regards
 
Thanks all. I have tried FancyPrarie's way. However, after clicking OK on the message box it still seems to close the form. Am I doing something wrong, or do I need to insert something to undo the "Close" operation?
 
Rather than use the Form_Close method use the Form_Unload method

Private Sub Form_Unload(Cancel As Integer)
Cancel = -1

End Sub

Will prevent the form closing

Regards
 
OnClose and OnUnload events may be too late to verify the data. Try the code before to that stage...

Also worth to read
Form - looping thru the controls faq702-5010

________________________________________________________________________
Zameer Abdulla
Visit Me
Children are poor men's riches.
 
Many thanks everyone. Put the If loop in the OnUnload event, using BuilderSpec's Cancel = -1. Seems to work beautifully now.
 
Zameer,

I've given you a star because your post solved my issue almost perfectly. Only thing I noticed is that if a user enters something in the field, leaves it, then goes back and deletes the entry from that field, then they can close the form with the field in question being null. I had to add some code to an exit command button in order to remedy this unlikely but possible event.

Thanks,
Mike
 
Code:
Private Sub txtClockNumber_Exit(Cancel As Integer)
If Me.txtClockNumber = "Clock No." Or IsNull(Me.txtClockNumber) Then
    MsgBox "Please enter a clock number"
    Me.AnotherControl.SetFocus
    Me.txtClockNumber.SetFocus
End If
End Sub

________________________________________________________________________
Zameer Abdulla
Visit Me
A person who misses a chance and the monkey who misses its branch can't be saved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top