Solved

Define the type of data to be entered into a textbox

Posted on 2000-03-08
10
167 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now