Results 1 to 9 of 9

Text box validation problems

Threaded View

  1. #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

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