+ Reply to Thread
Results 1 to 9 of 9

Text box validation problems

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    5

    Text box validation problems

    HELP. I need to make sure textbox entries follow certain rules so that values passed to a modules doesn't crash while calculating. Textbox entries must be positive numbers and must not conatain any alpha characters or invalid characters such as blanks, symbols, etc. When tabbing to the next textbox, the textbox needs to be tested and if invalid entries are found then focus needs to be put back on that textbox until a valid number is entered. Not all of the message box calls are in their final form. I've been having problems getting some of the message boxes to close and return to the textboxes for new entries. (See below, attached .xlsm file might make more sense; 1st tab explains validation rules)

    **************************************************************************************************************
    **************************************************************************************************************

    
    Private Sub TextBox_a_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    ' Validate textbox on exit by checking for text entry, blanks, negative numbers, and zero
    Dim avalue As Double
    Dim ContainsInvalidCharacter As Boolean
    Dim Counter As Integer
    Dim Mbxret As Integer
    Dim StrPrompt1 As String
    Dim StrPrompt2 As String
    Dim StrPrompt3 As String
    
    Dim StrTitle As String
    
    StrPrompt1 = "Sorry, only non-negative numbers allowed."
    StrPrompt2 = "Sorry, must have a value" & vbCrLf & "Only non-zero and non-negative numbers allowed."
    StrPrompt3 = "Sorry, invalid character entered"
    StrTitle = "Constant a"
    
    With Me.ActiveControl
    
    Do While (Not IsNumeric(TextBox_a.Value))
    TextBox_a.SetFocus
    
    
    Select Case (Not IsNumeric(Me.ActiveControl))
    
    Case (Not IsNumeric(Me.ActiveControl))
    Mbxret = MsgBox(StrPrompt1, vbOKOnly, StrTitle)
    If Mbxret = vbOK Then Cancel = True
    TextBox_a.SetFocus
    Exit Do
    Case vbNullString
    Mbxret = MsgBox(StrPrompt2, vbOKOnly, StrTitle)
    If Mbxret = vbOK Then Cancel = True
    TextBox_a.SetFocus
    
    End Select
    
    Loop
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'ContainsInvalidCharacter = True
    
    
    
    If Me.TextBox_a.Value <> vbNullString Then
    Do
    ContainsInvalidCharacter = True
    'Iterate through each character and determine if its a number,
    'letter, or non-number of string.
    For Counter = 1 To Len(TextBox_a.Value)
    
    Select Case Asc(Mid(TextBox_a.Value, Counter, 1))
    Case 45 To 46
    ContainsInvalidCharacter = False
    
    Case 48 To 57, 65 To 90, 97 To 122
    ContainsInvalidCharacter = False
    
    Case Else
    
    ContainsInvalidCharacter = True
    'TextBox_a.Value = vbNullString
    'SendKeys "+{TAB}" ' This goes back to TextBox
    TextBox_a.SetFocus
    Exit For
    
    End Select
    
    Next
    
    
    'If ContainsInvalidCharacter Then
    'Contains a non alpha or numeric character
    Mbxret = MsgBox(StrPrompt3, vbOKOnly, StrTitle)
    If Mbxret = vbOK Then Cancel = True
    TextBox_a.SetFocus
    Exit Do
    Loop Until ContainsInvalidCharacter = False
    
    
    'Else no invalid characters found
    
    'Else
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    avalue = CDbl(Val(Me.TextBox_a.Value))
    Select Case avalue
    Case Is < 0
    MsgBox "Sorry, only non-negative numbers allowed.", vbOKOnly, "Constant a"
    SendKeys "+{TAB}" ' This goes back to TextBox
    TextBox_a.SetFocus
    
    Case Is = 0
    MsgBox "Constant a is non-zero and positive", vbOKOnly, "Constant a"
    SendKeys "+{TAB}" ' This goes back to TextBox
    TextBox_a.SetFocus
    
    End Select
    End If
    'Else:
    ' MsgBox "Sorry, must have a value" & vbCrLf & "Only non-zero and non-negative numbers allowed.", vbOKOnly, "Constant a"
    
    'End If
    
    'SendKeys "+{TAB}" ' This goes back to TextBox
    'TextBox_a.SetFocus
    End With
    End Sub
    Attached Files Attached Files
    Last edited by paxtrodamus; 05-31-2013 at 05:45 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Text box validation problems

    Interesting...this is pretty much the same post you made 8 months ago?

    http://www.excelforum.com/excel-prog...alidation.html

    Also, I have closed the other 2 duplicate threads you posted, you may continue on this 1
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    08-21-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Text box validation problems

    Moderator,

    The reason there were multiple posts was because I forgot to add tags. When I tried to edit my post it wouldn't let me edit the search tags so the only option I saw was to repost. Also, I didn't have any responses for 8 months so I thought I would try again.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Text box validation problems

    Hi paxtrodamus

    The Code in the attached appears to do as you require. You'll need to do this
    Not all of the message box calls are in their final form
    Let me know of issues...
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    08-21-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Text box validation problems

    John,

    Its not really an issue, but for the life of me I can't figure out in Module2 what is being returned from the function. Its a Boolean, but NumberVal doesn't show up anywhere after it is called. The function is being called from this Private Sub

    Private Sub TextBox_r_AfterUpdate()
        Dim txtValue As Variant
        If Val(Me.TextBox_r.Value) < 0 Or Val(Me.TextBox_r.Value) > 1 Then
            MsgBox Me.TextBox_r.Name & " Value must be > 0 and less < 1"
            Me.TextBox_r.Value = ""
            Me.TextBox_r.SetFocus
            Exit Sub
        End If
        txtValue = Me.TextBox_r.Value
        Call NumberVal(txtValue)
        If Numeric = False Then
            Me.TextBox_r.Value = ""
            Me.TextBox_r.SetFocus
            Exit Sub
        Else
        End If
    End Sub
    It looks like operationally the NumberVal function toggles the Public Numeric between TRUE & FALSE. If that's so why must the Function NumberVal be declared as Boolean? Is there a reason? Sorry, as you can tell from my code which doesn't want to work, I'm by no means an expert at all the ins & outs of VBA. Your code works with a third of the code I had to use. Your function is below

    Public txtValue As Variant
    Public Numeric As Boolean
    Option Explicit
    Function NumberVal(txtValue) As Boolean
        Dim LPos As Integer
        Dim LChar As String
        Dim LValid_Values As String
        LPos = 1
        LValid_Values = " .0123456789"
        While LPos <= Len(txtValue)
            LChar = Mid(txtValue, LPos, 1)
            If InStr(LValid_Values, LChar) = 0 Then
                Numeric = False
                Exit Function
            End If
            LPos = LPos + 1
        Wend
        Numeric = True
    End Function

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Text box validation problems

    Hi paxtrodamus

    It's a bit late today...perhaps tomorrow I'll get into the confusion...and I think I understand the confusion...perhaps...maybe...

  7. #7
    Registered User
    Join Date
    08-21-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    5

    Thumbs up Re: Text box validation problems

    Hi John,

    I went old school and drew a flow chart of how I think your textbox validation function works, like I was taught when I was a wee young boy (see attached pdf and see if you agree). From further reading it appears functions don't always have to return values. I'm ashamed to say that I never considered using the AfterUpdate event . I used Exit events but had problems. This is the best solution I've come across for textbox validation. I never thought of Mid/InStr/While combination. This will help me with my hydrology project at work.

    Paxton

    Private Sub TextBox_r_AfterUpdate()
        Dim txtValue As Variant
        If Val(Me.TextBox_r.Value) < 0 Or Val(Me.TextBox_r.Value) > 1 Then
            MsgBox Me.TextBox_r.Name & " Value must be > 0 and less < 1"
            Me.TextBox_r.Value = ""
            Me.TextBox_r.SetFocus
            Exit Sub
        End If
        txtValue = Me.TextBox_r.Value
        Call NumberVal(txtValue)
        If Numeric = False Then
            Me.TextBox_r.Value = ""
            Me.TextBox_r.SetFocus
            Exit Sub
        Else
        End If
    End Sub
    Public txtValue As Variant
    Public Numeric As Boolean
    Option Explicit
    Function NumberVal(txtValue) As Boolean
        Dim LPos As Integer
        Dim LChar As String
        Dim LValid_Values As String
        LPos = 1
        LValid_Values = " .0123456789"
        While LPos <= Len(txtValue)
            LChar = Mid(txtValue, LPos, 1)
            If InStr(LValid_Values, LChar) = 0 Then
                Numeric = False
                Exit Function
            End If
            LPos = LPos + 1
        Wend
        Numeric = True
    End Function
    Attached Files Attached Files

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Text box validation problems

    Hi paxtrodamus

    I'm also from the old school...retired CPA...have used Flow Charts extensively in system development. I'm a bit tired this evening...these 71 year old bones can't handle the beating they've taken these past several days.

    I'll try to look at it later this week...promise...have more physical work yet to do this week.

    I've also had issues with Focus on Exit Events...AfterUpdate appears to solve that issue...at least in this case...it's all a bit squirrelly at times.

    I assume the Code works for you? If so... please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Thanks.

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Text box validation problems

    Hi paxtrodamus

    To edit your post (to add tags or change text etc, click the "edit post" button at the bottom right of that post

+ 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