Solved

Define the type of data to be entered into a textbox

Posted on 2000-03-08
10
168 Views
Last Modified: 2010-05-02

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.
0
Comment
Question by:Webgrrl
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2599169
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2599203
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2599232
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Expert Comment

by:jbathija
ID: 2604523
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2604803
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
 

Author Comment

by:Webgrrl
ID: 2606891
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
 

Author Comment

by:Webgrrl
ID: 2606946
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 25 total points
ID: 2606949
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
 

Author Comment

by:Webgrrl
ID: 2606965
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
 

Author Comment

by:Webgrrl
ID: 2606972
erik37 Thanks for the help.

And the others as well your help is greatly appreciated.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question