Mask and validation

naungsai

Abecedarian
Local time
Tomorrow, 03:47
Joined
Sep 8, 2008
Messages
123
Dear Friends

I have a field which need 3 characters.
The first charactor can be "A" or "B" or "C" or "D". (No blank)
The Second charactoer can be "A" or "B" or "C" or blank.
The Third charactoer can be "A" or "B" or "C" or blank.

How should I do it? I prefer more in input mask.
 
If you want to validate this string from more than one location in your application then paste the following function into a module, otherwise paste it in the vba code of your form

Code:
Public Function vInput(AnyString As String) As Boolean

If Len(AnyString) > 3 Or Len(AnyString) = 0 Then
    vInput = False
    Exit Function
End If

If InStr("ABCD", Left(AnyString, 1)) = 0 Then
    vInput = False
    Exit Function
End If

Dim C As String

For i = 2 To Len(AnyString)
    C = Mid(AnyString, i, 1)
    If InStr("ABC ", C) = 0 Then
        vInput = False
        Exit Function
    End If
    
Next
vInput = True
    
End Function

To use this function

Code:
If vInput(Me.TxtField) = False then
   MsgBox "Invalid Code Entered"
End If

Where Me.TxtField = the name of your textbox

David
 
Code:
If vInput(Me.TxtField) = False then
   MsgBox "Invalid Code Entered"
End If

Where Me.TxtField = the name of your textbox

David

Dear DCrake
Thank you. It works.
In the second part, I want to clear the text that I have enter then get the focus on it. Can you please help me again?

Best
 
I am reading that if the response is false then you want to clear the contents of the text box and set the focus back to the text box?

add

Me.TextBox = ""
Me.TextBox.SetFocus

after the msgbox line
 

Users who are viewing this thread

Back
Top Bottom