Solved

Problem trying to verify characters as Integers

Posted on 2005-05-07
167 Views
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.

Thanks.

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
0
Question by:rinkydink
6 Comments

LVL 18

Accepted Solution

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"
else
Msgbox "Its not an integer"
end if
end if
0

LVL 14

Expert Comment

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
'===========================

;-)
Shiju
0

LVL 9

Expert Comment

sethi,
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
else
MsgBox "You entered decimals values"
end if
end if
0

LVL 13

Expert Comment

Hi
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
Else
IsInteger = False
End If

Exit Function
Er1:
IsInteger = False
End Function
0

LVL 19

Expert Comment

<< 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

S
0

LVL 1

Author Comment

I went with:

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

Thanks everyone!
0

Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

Featured Post

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
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…

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.

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!