[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Problem trying to verify characters as Integers

Posted on 2005-05-07
Medium Priority
Last Modified: 2010-05-02
Hi - I'm trying to create a procedure that will verify that the characters typed in a text box are integers, I can't figure out how to verify each character one by one. I'm guessing a for...next loop will work but am unsure of the code. Any advice will be appreciated.


Here is what I got so far:

Dim AnyString As String

AnyString = Text1
Dim x As Integer

x = Len(AnyString)

If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub

'For i = 1 To x
'Next i
Question by:rinkydink
LVL 18

Accepted Solution

Sethi earned 480 total points
ID: 13950058
You can use IsNumeric fucntion to see whether a number has been entered. Then you can use the maximum value of an integer to know whether its an integer. Example:

if IsNumeric(txtTextbox)=True then
   if txtTextbox<=32767 And txtTextbox>=-32767 then
     Msgbox "Its an integer"
     Msgbox "Its not an integer"
   end if
end if
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13950209
Hi try this

this will allow only integers to be entered
Private Sub Form_Load()
    Text1.MaxLength = 5
    Text1.Text = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> vbKeyBack Then
        KeyAscii = 0
    End If
End Sub


Expert Comment

ID: 13950225
there are a problem...
15,14 is numeric and 32767>= 15,14>= -32767

and view his code he want a 10 characters long
may be a long number no a integer
can it be zeros by the left?

    x = Len(AnyString)   or x = Len(cst(val(AnyString)))  

if IsNumeric(txtTextbox)=True then
    if round(val(txtTextbox),0)=txtTextbox then
        If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
        If x < 10 Then MsgBox "You entered too few numbers": Exit Sub
     MsgBox "You entered decimals values"
   end if
end if
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 13

Expert Comment

ID: 13950388
This is a very reliable way to detect if a string represents an Integer:

' TheString parameter is the string you want ot validate
Private Function IsInteger(TheString As String) As Boolean
Dim RefInt As Integer
Dim TInt

On Error GoTo Er1
    RefInt = CInt(TheString)
    TInt = Val(TheString)
On Error GoTo 0
If TInt = RefInt Then
    IsInteger = True
    IsInteger = False
End If

Exit Function
    IsInteger = False
End Function
LVL 19

Expert Comment

ID: 13950515
<< If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub>>

A length of 10 characters is not an integer, its a long type. Anyways, below you'll find how to make sure only numbers can be entered, and how to validate the length:

Private Sub Form_Load()
'set cause validation
Text1.CausesValidation = True
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
'make sure only numeric values are typed
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 44 Or KeyAscii = 46 Or KeyAscii = 8) Then
    KeyAscii = 0
End If
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
'validate the length of the text in text1
If Len(Text1.Text) < 10 Then
    MsgBox "You entered too few numbers"
ElseIf Len(Text1.Text) > 10 Then
    MsgBox "You entered too many numbers"
End If
End Sub


Author Comment

ID: 13993526
I went with:

If IsNumeric(Text1.Text) = True Then
Exit Sub
MsgBox "You must type only numbers": Text1.Text = ""

Thanks everyone!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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