Closed Thread
Results 1 to 3 of 3

Text box validation problems

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

    Text box validation problems

    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

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Text box validation problems

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If posting code please use code tags, see here.

  3. #3
    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
    52,926

    Re: Text box validation problems

    Welcome to the Forum, unfortunately:

    This is a duplicate post and as such does not comply with Rule 5 of our forum rules. This thread will now be closed, you may continue in your other thread.
    http://www.excelforum.com/excel-prog...-problems.html

    Thread Closed.
    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

Closed Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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