Lose focus at wrong time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newUser1234
    New Member
    • Jul 2007
    • 5

    #1

    Lose focus at wrong time

    Hello i have a data validation check upon lost focus for a vb textbox:
    Code:
    Private Sub fromText_LostFocus()
    
    If IsNull(fromText.Text) = True Then
        MsgBox StrMsg, vbExclamation   
          
    End If
    
    End Sub
    It will check if its null, if so it'll display an error message. But then it will lose focus, i want it to stay in that text box until the user enters something.
  • pureenhanoi
    New Member
    • Mar 2007
    • 175

    #2
    Originally posted by newUser1234
    Hello i have a data validation check upon lost focus for a vb textbox:
    Code:
    Private Sub fromText_LostFocus()
    
    If IsNull(fromText.Text) = True Then
        MsgBox StrMsg, vbExclamation   
          
    End If
    
    End Sub
    It will check if its null, if so it'll display an error message. But then it will lose focus, i want it to stay in that text box until the user enters something.
    Using Validate event instead of LostFocus.
    If the value still null, set the Cancel = True to keep the focus

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by pureenhanoi
      Using Validate event instead of LostFocus.
      If the value still null, set the Cancel = True to keep the focus
      The Validate event has an added advantage.
      Let's say you have a Cancel button or something. Generally, the user would not expect to have to enter a value in your field, just so they can exit by hitting Cancel. If you do the check in LostFocus, it will always complain about the null, even if the user is just trying to exit. But if you do it in Validate, you can skip it by setting the Cancel button's CausesValidatio n property to False. This simply means "don't bother triggering validation on the control we left, before entering this one".

      Comment

      Working...