Define the type of data to be entered into a textbox


I would like to know if anybody could help with this question?
I think this is very straight forward but I can not find it in any documentation.  I tried also a keypress event but could not deine which type of data enry was being entered.

I would like to define the type of data to be entered into a textbox (alpha vs num)and display a error msg if the entry is not alpha.

Thanks for this time.
WebgrrlAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Erick37Connect With a Mentor Commented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case &H30 To &H39
        'numbers are allowed
    Case &H0 To &HF
        'Control characters are allowed
    Case &H2E, &H2C
        'period and comma allowed
    Case Else
        'nothing else
        Call MsgBox("Only Numbers Accepted")
        KeyAscii = 0
    End Select
End Sub
0
 
Erick37Commented:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case &H30 To &H39
        'numbers are allowed
    Case &H0 To &HF
        'Control characters are allowed
    Case &H2E, &H2C
        'period and comma allowed
    Case Else
        'nothing else
        KeyAscii = 0
    End Select
End Sub

You may also find the function IsNumeric() useful.
0
 
wsh2Commented:
The IsCharOk function below will test for any character you specify in the strTest constant. It is very readable and amaintainable.. if you want to add or delete characters.. just make the changes in the Const strText, between the brackets. If the Text field is only to be numeric.. you can add a If Isnumeric(Text1.Text) test as well.. Enjoy !!!

----------------------------
This is what you put in the Keypress event

<---------- Code ---------->

Private Sub Text1_KeyPress (KeyAscii As Integer)
 
If NOT KeyAscii = vbKeyBack _
  And NOT KeyAscii) = vbKeyReturn _
  Then
    If NOT xIsCharOk(chr(KeyAscii)) _
    Then
      KeyAscii = 0
      Beep
    End If
End If

Exit Sub

--------------------------------
And this is the xIsCharOk Function

<---------- Code ---------->

Private Function xIsCharOk _
(Byval strInput as String) _
as Boolean

' Place any characters that you would like to test for anywhere between the brackets

Const strTEST = "[ABCDEFGHIJKLMNOPQRSTUVWXYZ .1234567890]"

xIsCharOk = True
Dim intIndex As Integer
For intIndex = 1 To Len(strInput)
'   Ucase used to test for both upper and lower case
    If Not UCase(Mid(strInput, intIndex, 1)) Like strTEST _
    Then
        xIsCharOk = False
        Exit For
    End If
Next intIndex

End Function

---------------------------------

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
wsh2Commented:
Ooops.. please correct the following line
---------------------------------
From:

  And NOT KeyAscii) = vbKeyReturn

To:

  And NOT KeyAscii = vbKeyReturn
---------------------------------

And at the end of the Keypress procedure please Change..

---------------------------------
From:

  Exit Sub

To:

  End Sub
---------------------------------

IMPORTANT:

The xIsCharOk Function is set up to handle strings.. so that you can also run a character test on the entire Text1.Text during the Text_Change event, in case the user has copied and pasted into the Textbox, as no Keypress Event occurs for a copy/paste operation.

Enjoy.. <smile>
0
 
jbathijaCommented:
If (KeyAscii) >= Asc(0) And KeyAscii <= Asc(9) Then
    MsgBox Chr(KeyAscii)
Else
    KeyAscii = 0
End If


this is to avoid the alphabets in the numerical text box
0
 
Erick37Commented:
jbathija:
Please stop locking questions with answers which do not work.  This is against EE's guidlines, and not fair to those who have put some effort into providing a working solution.

Regards.
0
 
WebgrrlAuthor Commented:
Thanks for the help but I can not seem to get any of the suggestions to work.

wsh2  -  I tried your code by creating the key press statement for txtCustName
and then created the function but when it ran it did not detect the wrong entry.
there is two text boxes one one that requires only alpha entry
the second requires only numeric.  I would like to place a msg that the entry was wrong as well.
Did I take the wrong approach with this.


0
 
WebgrrlAuthor Commented:
ERIK37

I tried your code and it does block the entry of incorect data but does not allow a error msg to be displayed when a wrong entry is made.

thanks for your help.
0
 
WebgrrlAuthor Commented:
ERIK37

I tried your code and it does block the entry of incorect data but does not allow a error msg to be displayed when a wrong entry is made.

thanks for your help.
0
 
WebgrrlAuthor Commented:
erik37 Thanks for the help.

And the others as well your help is greatly appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.