+ Reply to Thread
Results 1 to 7 of 7

Can I return focus to a Textbox when Validation fails as part of a Sub?

Hybrid View

D3Pratt Can I return focus to a... 04-27-2009, 10:56 PM
Leith Ross Re: How do I return focus to... 04-27-2009, 11:01 PM
D3Pratt Re: How do I return focus to... 04-27-2009, 11:27 PM
D3Pratt Re: How do I return focus to... 04-27-2009, 11:29 PM
D3Pratt Re: Can I return focus to a... 04-28-2009, 09:21 AM
  1. #1
    Registered User
    Join Date
    03-25-2009
    Location
    San Jose
    MS-Off Ver
    Excel 2007
    Posts
    83

    Can I return focus to a Textbox when Validation fails as part of a Sub?

    As part of a UserForm I am Validating and Formatting in one pass. The problem I'm having is that when the Validation fails, I get the error message, and the focus moves on to the next text box.

    I want to trap the user in a loop until they have acceptable data


    Private Function CheckNum2(CurValue As String)
        
        On Error Resume Next
        Select Case True
            Case IsNumeric(CurValue)
                CheckNum2 = Format$(CurValue, "#,###.##")
            Case CurValue < 1
                MsgBox ("You must set a value of 1 or greater")
            Case Else
                MsgBox ("You must set a value of 1 or greater")
    
        End Select
    
    End Function
    Private Sub APV_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        
        With APV
            .Text = CheckNum2(.Text)
        End With
        
    End Sub
    Last edited by D3Pratt; 04-28-2009 at 06:01 PM. Reason: Subject clarification

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How do I return focus to a Textbox when Validation fails?

    Hello D3Pratt,

    You need to set Cancel = True in APV_Exit if the validation fails.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-25-2009
    Location
    San Jose
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: How do I return focus to a Textbox when Validation fails?

    Thanks for the fast reply!

    Cool... Can that be done within the code I have above, or do I need to do something different?

    I have 16 entries and I would prefer to have them all look at the one section of code on exit.

    I was able to get a partial fix this way, but like I said, this will require multiples

    Private Sub ATBV_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        
        If Not IsNumeric(ATBV.Value) Then
            MsgBox "You must set a value of 1 ISK or greater"
            Cancel = True
        End If
    
    '    With ATBV
    '        .Text = CheckNum(.Text)
    '    End With
    
    End Sub

  4. #4
    Registered User
    Join Date
    03-25-2009
    Location
    San Jose
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: How do I return focus to a Textbox when Validation fails?

    I think might be better to ask if it's possible to actually do the the validation as part of the formatting function.

    Because of the 16 entries I was thinking I wanted to have it all contained as a subroutine during Exit, but I'm not sure if I CAN do that.
    Last edited by D3Pratt; 04-28-2009 at 09:18 AM.

  5. #5
    Registered User
    Join Date
    03-25-2009
    Location
    San Jose
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Can I return focus to a Textbox when Validation fails as part of a Sub?

    Bump,

    Edited title to better reflect my issue/question.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Can I return focus to a Textbox when Validation fails as part of a Sub?

    Hello D3Pratt,

    You can reuse a validation routine in the Exit event. You need to create a function that will return a Boolean value as a result of the validation. This can be applied to the Cancel variable in the Exit event. Copy this code into a standard VBA module.
    Function ValidateTextBox(ByRef TB As MSForms.TextBox) As Boolean
    
         If Trim(TB.Value) = "" Then Exit Function
         
         If Not IsNumeric(TB) Then
            MsgBox "Please enter only Numeric values."
            ValidateTextBox = True
            TB.SelStart = 0
            TB.SelLength = Len(TB.Value)
         End If
             
    End Function
    The macro will validate that the entry is numeric. Spaces and empty entries are ignored. Now you can use this same line of code below (in blue) in every TextBox_Exit event.
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
       Cancel = ValidateTextBox(ActiveControl)
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1