944,189 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 7th, 2006
0

How to check data type

Expand Post »
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
Similar Threads
Reputation Points: 15
Solved Threads: 1
Newbie Poster
lover99509 is offline Offline
21 posts
since Jul 2006
Aug 7th, 2006
0

Re: How to check data type

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.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Aug 7th, 2006
0

Re: How to check data type

If Val(Text1.text) > 0 or Text1.text = "0" then
Reputation Points: 10
Solved Threads: 2
Light Poster
maheshsayani is offline Offline
45 posts
since Jul 2006
Aug 7th, 2006
0

Re: How to check data type

Hi
Try With This Code

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. If Val(Text1.text) > 0 or Text1.text = "0" then
  2. Print "Is Integer
  3. Else
  4. Print "Not Integer"
  5. End if
  6.  
Last edited by Comatose; Aug 7th, 2006 at 11:49 am. Reason: UnNeeded Bold Tags...?
Reputation Points: 10
Solved Threads: 2
Light Poster
maheshsayani is offline Offline
45 posts
since Jul 2006
Aug 7th, 2006
0

Re: How to check data type

I suggest stopping them from introducing anything other than numbers, right off the bat (in the text1 keypress event):
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. '
  3. ' Invalidate keystroke if not a digit, decimal point or backspace.
  4. '
  5. If (Not IsNumeric(Chr$(KeyAscii)) And (Chr$(KeyAscii) <> "." And Chr$(KeyAscii) <> vbBack)) Then KeyAscii = 0
  6.  
  7. End Sub

And then test between the int or long variable types using vartype as so:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if vartype(retval) = vbinteger then
  2. msgbox "is integer"
  3. end if

Here is a table of such data:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 0 vbEmpty Empty and not initialized argument
  2. 1 vbNull Invalid data or a null string argument
  3. 2 vbInteger Integer argument
  4. 3 vbLong Long argument
  5. 4 vbSingle Single argument
  6. 5 vbDouble Double argument
  7. 6 vbCurrency Currency argument
  8. 7 vbDate Date argument
  9. 8 vbString String argument
  10. 9 vbObject Object argument
  11. 10 vbError Error argument
  12. 11 vbBoolean Boolean argument
  13. 12 vbVariant Variant argument
  14. 13 vbDataObject Data Access Object (DAO) argument; an advanced database value such as a field or record
  15. 14 vbDecimal Decimal argument
  16. 17 vbByte Byte argument
  17. 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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. dim TypedData as long
  2. TypedData = CLng(Text1.Text)
You could also do the same with integer values, as in CInt(text1.text)
Last edited by Comatose; Aug 7th, 2006 at 2:43 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Aug 8th, 2006
0

Re: How to check data type

I think we're getting away from the original question, which was
Quote originally posted by lover99509 ...
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?
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Aug 8th, 2006
0

Re: How to check data type

this solution will work

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. '
  3. ' Invalidate keystroke if not a digit, decimal point or backspace.
  4. '
  5. If (Not IsNumeric(Chr$(KeyAscii)) And (Chr$(KeyAscii) <> "." And Chr$(KeyAscii) <> vbBack)) Then KeyAscii = 0
  6.  
  7. End Sub


but if i do like this:-

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. dim TypedData as long
  2. 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]
Last edited by Comatose; Aug 8th, 2006 at 6:16 am.
Reputation Points: 15
Solved Threads: 1
Newbie Poster
lover99509 is offline Offline
21 posts
since Jul 2006
Aug 8th, 2006
0

Re: How to check data type

Quote originally posted by WaltP ...
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.
Last edited by Comatose; Aug 8th, 2006 at 6:20 am.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Oct 18th, 2007
0

Re: How to check data type

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?

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub failsafe(ByVal bericht As String, ByRef cijfer As Integer, ByVal procedure As String)
  2. Dim tempVar As String
  3. Console.WriteLine(bericht)
  4. tempVar = Console.ReadLine
  5. 'Check for contents
  6. If (tempVar <> "") Then
  7. 'Check for numeretical value
  8. If IsNumeric(tempVar) Then
  9. cijfer = CByte(tempVar)
  10. 'Check to see if value is integer
  11. If (cijfer >= -2147483648) And (cijfer <= 2147483647) Then
  12. 'Procedure that is executed when all creteria are met
  13. procedure()
  14. Else
  15. Main()
  16. End If
  17. Else
  18. Main()
  19. End If
  20. Else
  21. Main()
  22. End If
  23. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bn2vs is offline Offline
2 posts
since Oct 2007
Oct 18th, 2007
0

Re: How to check data type

Click to Expand / Collapse  Quote originally posted by WaltP ...
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???

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim Value As Double
  2. Dim IntMaxValue As Integer
  3. Dim LongMaxValue As Long
  4.  
  5. IntMaxValue = 32767
  6. LongMaxValue = 2147483647
  7.  
  8. Value = CDbl(Text1.Text)
  9.  
  10. If Value > LongMaxValue Then
  11. MsgBox "Value is double"
  12. ElseIf Value > IntMaxValue Then
  13. MsgBox "Value is long"
  14. Else
  15. MsgBox "Value is integer"
  16. End If
Last edited by yello; Oct 18th, 2007 at 8:40 pm.
Reputation Points: 13
Solved Threads: 4
Light Poster
yello is offline Offline
30 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: DSN in Windows 7
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Guess number Game ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC