VBGuy
Y3by posted.
"i need to test if a value in the textbox is INTEGER."
Typename is not going to do anything in this situation.
And
Won't "CInt(Valu)=CInt(Valu)" always return True if it is an integer.
Try
Function IsInteger(valu as String) as Boolean
On Error Resume Next
IsInteger = (CInt(Valu)= Valu)
If Err.Number <> 0 then IsInteger = false
End Function
Dim X As Integer
X = txtNum 'txtNum is the textbox where the
'integer is filled in
If X <> txtNum Then
MsgBox "No integer"
Else
MsgBox "Integer"
End If
So the following could test if a number is in an integer range:
If CInt(Text1.text)=CInt(Text1.Text) Then
'is integer
end if
The above if statement will either pass as true or generate an error. This if statment will never return false. CInt, CCur, CStr are all casting functions that will not accept anything out of it's range. Therefore by simply calling the statement you can determine if it is an integer.
Consider the statment you wrote: IsInteger=(CInt(Valor) = Valor)
This statment will return the same result as IsInteger=(CInt(Valor) = CInt(Valor))
because VB is doing the type casting (conversion) in the first one. Try IT!! You have to think like a computer.They both will require error trapping however.
You cannot write CInt(32768) without generating an overflow error. So if any value in your text box is out of the integer range, an error will generate.
You could also do: IsInteger=Val(Valor)>=-32767 and Val(Valor)<=32767
That would require minimal error trapping.
CInt(13.33) = 13, integers are not floating point numbers. The numbers are rounded. If you want to disallow 13.33 as a valid integer in your program then you would do a statement like this:
IsInteger=(CInt(Valor) = Valor) and (Int(Valor)=Valor)
That should help, unless I'm just missing the point of your question. :->
Small Point:
I don't believe you even need the If Err.Number = 0 because the IsInteger will return FALSE by Default. The following should be okay.
Public Function IsInteger(valor As String) As Boolean
On Error Resume Next
IsInteger = (CInt(valor) = valor)
End Function
That's a little more streamlined.
The previous post is right, I meant to say -32768 [16 bits]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.