Link to home
Create AccountLog in
Avatar of Webgrrl
Webgrrl

asked on

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.
Avatar of Erick37
Erick37
Flag of United States of America image

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.
Avatar of wsh2
wsh2

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

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

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>
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
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.
Avatar of Webgrrl

ASKER

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.


Avatar of Webgrrl

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Webgrrl

ASKER

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.
Avatar of Webgrrl

ASKER

erik37 Thanks for the help.

And the others as well your help is greatly appreciated.