Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

integer validation

Status
Not open for further replies.

Transcend

Programmer
Sep 29, 2002
858
AU
Hi

this may seem like a silly question but what is the best way to validate that a text box has had an integer entered so as to avoid a data overflow bug.

I'm validating that its a number but if a ridiculously long number is entered the overflow occurs. It seems messy to test for the value being between the min and max
integer values ...

Any thoughts?
Thanks in advance

Transcend
[gorgeous]
 
Something has to test that the value lies betwen the values you want, even if it is messy.

Try the VALIDATE event of the text box.
If the data does not validate, the focus will not leave the text box.

Here is an example:
Code:
'contants at the top of the form's code
'allow for easy changing later
Private Const theMin = 10
Private Const theMax = 1000

'event code for Text1 control
Private Sub Text1_Validate(Cancel As Boolean)
    Dim theVal As Double
    Dim szMsg as String
    theVal = Val(Text1.Text)
    If theVal < theMin Or theVal > theMax Then
        'do not allow the focus to leave
        Cancel = True
        'message box too?
        szMsg = "Please enter a value between "
        szMsg = szMsg  & theMin & " and " & theMax
        MsgBox szMsg,vbInformation,"Problem"       
    End If
End Sub
 
Hi Transcend.
Try this catch:
Code:
If not isnumeric(...) or if abs(..)>32767 then

Should do fine. ;-)

Cheers,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
IsNumeric() does have it's flaws when validating integers (well, not a flaw, but usually just improper usage).

So, test this on the above examples:

Dim iTest As Integer
On Error Resume Next
iTest = CInt(Text1.Text)
If err Then
MsgBox "Entered Data is NOT an valid Type (Integer)"
End If

 
Make sure you use err.clear, or On Error Goto X, after getting an error

 
Thanks everyone

will try using the validate function

Transcend
[gorgeous]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top