Object Focus

  • Thread starter Thread starter dsurrett
  • Start date Start date
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
 
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:
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..
 
Use the BeforeUpdate event of the textbox and Cancel=True if your criteria is not met.
 
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

Back
Top Bottom