Object Focus (1 Viewer)

D

dsurrett

Guest
I'm trying to force a user to enter text into a field. To this end, I've created a subroutine for the exit method of the text box they are supposed to fill out. I check to see if the .text property is null, and if so create a message box that pops up a notification telling the user they need to fill in said field. After that, I try to use the Me.txtReason.SetFocus (the text box is called txtReason) to return the focus to that text box, but instead it moves on to the next text box. Is there a way I can prevent the user from leaving the text box until they've entered some sort of data into it?

Thanks,
Dave
 

godofhell

Database Guru
Local time
Yesterday, 16:12
Joined
May 20, 2005
Messages
180
You can't check for "" instead you need to check for null as shown below.

Code:
Private Sub txtReason_Exit(Cancel As Integer)
    If IsNull(txtReason.Value) Then
        msgbox "You Must enter information on this field"
        txtReason.SetFocus
    End If
End Sub
 
Last edited by a moderator:

modest

Registered User.
Local time
Yesterday, 19:12
Joined
Jan 4, 2005
Messages
1,220
godofhell said:
You can't check for "" instead you need to check for null as shown below.

Code:
Private Sub txtReason_Exit(Cancel As Integer)
    If IsNull(txtReason.Value) Then
        msgbox "You Must enter information on this field"
        txtReason.SetFocus
    End If
End Sub


If IsNull(txtReason.Value) or txtReason.Value = "" Then..
 
R

Rich

Guest
Use the BeforeUpdate event of the textbox and Cancel=True if your criteria is not met.
 

MarkK

bit cruncher
Local time
Yesterday, 16:12
Joined
Mar 17, 2004
Messages
8,187
Also, consider the Nz function if checking for nulls AND zero length strings.

Code:
Private Sub txtReason_BeforeUpdate(Cancel as Integer)
  If Nz(txtReason, "") = "" Then
    MsgBox "You gotta have a reason!"
    Cancel = True
  End If
End Sub
 

Users who are viewing this thread

Top Bottom