Guys anyone knows how to check if a value entered in a text box
i.e. text3.text=integer value is integer or long

i found a function that checks wether the text is numeric or not "isNumeric" but i did not find any other function that checks if the value entered is integer?


any help??!!


best regards ,

lover99509

Recommended Answers

All 13 Replies

There is no function to do what you want. All you have is a string of characters. When you convert into a number, you'll know. Short of counting the characters and analyzing whether the characters are greater than "32767" or "65535", just convert into a long and don't worry about it.

If Val(Text1.text) > 0 or Text1.text = "0" then

Hi
Try With This Code

If Val(Text1.text) > 0 or Text1.text = "0" then
  Print "Is Integer
Else
 Print "Not Integer"
End if

I suggest stopping them from introducing anything other than numbers, right off the bat (in the text1 keypress event):

Private Sub Text1_KeyPress(KeyAscii As Integer)
'
' Invalidate keystroke if not a digit, decimal point or backspace.
'
If (Not IsNumeric(Chr$(KeyAscii)) And (Chr$(KeyAscii) <> "." And Chr$(KeyAscii) <> vbBack)) Then KeyAscii = 0

End Sub

And then test between the int or long variable types using vartype as so:

if vartype(retval) = vbinteger then
     msgbox "is integer"
end if

Here is a table of such data:

0   	vbEmpty   	Empty and not initialized argument
1 	vbNull 	        Invalid data or a null string argument
2 	vbInteger 	Integer argument
3 	vbLong 	        Long argument
4 	vbSingle 	Single argument
5 	vbDouble 	Double argument
6 	vbCurrency 	Currency argument
7 	vbDate 	        Date argument
8 	vbString 	String argument
9 	vbObject 	Object argument
10 	vbError 	Error argument
11 	vbBoolean 	Boolean argument
12 	vbVariant 	Variant argument
13 	vbDataObject 	Data Access Object (DAO) argument; an advanced database value such as a field or record
14 	vbDecimal 	Decimal argument
17 	vbByte 	        Byte argument
8192+int 	vbArray 	Array argument of the type specified by the int addition to 81920   	 vbEmpty   	Empty and not initialized

Now, as was stated above, a textbox will only return a string. There is no other means by which a textbox will contain data. A number, too, can be a string, "1" is a string, but it's a character that represents a digit. If you need a number in a specific variable type, though, you can "cast" it to the desired type. So, if you need the information in the textbox to be a long, you can simply assign a variable to it's cast type:

dim TypedData as long
TypedData = CLng(Text1.Text)

You could also do the same with integer values, as in CInt(text1.text)

I think we're getting away from the original question, which was

Guys anyone knows how to check if a value entered in a text box
i.e. text3.text=integer value is integer or long

Doesn't this mean:I have a number in a text box. How can I tell if I need an Integer or a Long when it's converted from ASCII to binary?
Or am I interpreting this wrong?

this solution will work

Private Sub Text1_KeyPress(KeyAscii As Integer)
'
' Invalidate keystroke if not a digit, decimal point or backspace.
'
If (Not IsNumeric(Chr$(KeyAscii)) And (Chr$(KeyAscii) <> "." And Chr$(KeyAscii) <> vbBack)) Then KeyAscii = 0

End Sub

but if i do like this:-

dim TypedData as long
TypedData = CLng(Text1.Text)

Data accurecy will be lost because if the user entered by mistake 9.8
then when the above instruction is excuted TypedData =10 (!!! rounding)

i have another solution which is using the method inStr which will test if the entered value contains the charecter "."

check if user entered only numeric values and test if it does not contain "."


lover99509
[/LEFT]

I think we're getting away from the original question, which was
Doesn't this mean:I have a number in a text box. How can I tell if I need an Integer or a Long when it's converted from ASCII to binary?
Or am I interpreting this wrong?

I would guess that he wants to ensure that numbers are all that is in the textbox, especially if he wants to see if the data entered is a long or integer.... so It looks to me as though the information is entirely relevant.

Lover99509: Right, because long is a integer type. You could cast it to a single, instead of clng, you could use csng, or cdbl. Which would cast it to a single or a double, which are both floating points. That would maintain your decimal point, and solve you from having to do string parsing.

Euhm, I have a similair problem.

I wanna make sure a value entered in a console application is not empty, a number, and and intiger. I just started learning VB, so i came up with this noobish code, what can i do better?

Private Sub failsafe(ByVal bericht As String, ByRef cijfer As Integer, ByVal procedure As String) 
        Dim tempVar As String 
        Console.WriteLine(bericht) 
        tempVar = Console.ReadLine 
        'Check for contents 
        If (tempVar <> "") Then 
            'Check for numeretical value 
            If IsNumeric(tempVar) Then 
                cijfer = CByte(tempVar) 
                'Check to see if value is integer 
                If (cijfer >= -2147483648) And (cijfer <= 2147483647) Then 
                    'Procedure that is executed when all creteria are met 
                    procedure() 
                Else 
                    Main() 
                End If 
            Else 
                Main() 
            End If 
        Else 
            Main() 
        End If 
    End Sub

I think we're getting away from the original question, which was
Doesn't this mean:I have a number in a text box. How can I tell if I need an Integer or a Long when it's converted from ASCII to binary?
Or am I interpreting this wrong?

How about trying the following way???

Dim Value As Double
    Dim IntMaxValue As Integer
    Dim LongMaxValue As Long
    
    IntMaxValue = 32767
    LongMaxValue = 2147483647
    
    Value = CDbl(Text1.Text)
    
    If Value > LongMaxValue Then
        MsgBox "Value is double"
    ElseIf Value > IntMaxValue Then
        MsgBox "Value is long"
    Else
        MsgBox "Value is integer"
    End If

No, i know how conversion functions work. I'm not sure, but wouldn't you get an error when the user enetered a letter instead of a number?

Yes you will get an error. To prevent this check the value in the textbox with IsNumeric() function.

If IsNumeric(Text1.Text) Then
'continue with the code
Else
msgbox "Invalid Number"
Exit Sub
End If

Try this:

Dim str as String

str = TypeName(inputVariable)

Hoppy

hahahhahahhahaha this is actually a laugh, how on earth do you know what any of this means?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.