Solved

Define the type of data to be entered into a textbox

Posted on 2000-03-08
10
169 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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