validation for alphanumeric

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpradeepa
    New Member
    • Apr 2008
    • 23

    validation for alphanumeric

    Hi


    How to give alphanumeric validation for textbox in VBA for ms access

    already the textboxvalue is filled with "6hy754"
    textbox="6hy754 "
    I have to check whether the value is alphanumeric or not
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Originally posted by rpradeepa
    Hi


    How to give alphanumeric validation for textbox in VBA for ms access

    already the textboxvalue is filled with "6hy754"
    textbox="6hy754 "
    I have to check whether the value is alphanumeric or not
    Hi

    If you want to confirm that both letters and numbers are present then perhaps this function will do
    Code:
    Function IsAlphaNumeric(ByVal ThisString As String) As Boolean
        Dim i As Integer
        Dim Alpha As Boolean
        Dim Numeric As Boolean
        
        Alpha = False
        Numeric = False
        For i = 1 To Len(ThisString)
            If IsNumeric(Mid(ThisString, i, 1)) Then
                Numeric = True
            Else
                Alpha = True
            End If
        Next i
        
        IsAlphaNumeric = Alpha And Numeric
    End Function
    ??

    HTH

    MTB

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      The problem with that is that if the OP wants at least one characters that is 0-9 and at least one that is a-z, the code can fail.

      98.6, 19+87, 12@3, #123, etc will all return true, because !@#$%^&*()_+ and so forth are all non-numeric, alpha characters. To assure only 0-9 and a-z characters, and at least one of each, we need to modify Mike's code a little bit. I think this does it:
      Code:
      Function IsAlphaNumeric(ByVal ThisString As String) As Boolean
          Dim i As Integer
          Dim Alpha As Boolean
          Dim Numeric As Boolean
          
          Alpha = False
          Numeric = False
          For i = 1 To Len(ThisString)
              If IsNumeric(Mid(ThisString, i, 1)) Then
                  Numeric = True
              End If
              If (Mid(ThisString, i, 1)) Like "[a-z]" Then
                 Alpha = True
              End If
          Next i
          
          IsAlphaNumeric = Alpha And Numeric
      End Function
      Welcome to Bytes!

      Linq ;0)>

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        I think it would be better if the OP specified more clearly exactly what they are after here. The question is not very clear and can be interpreted in a number of ways.

        Comment

        Working...