-
Aug 28th, 2009, 01:55 PM
#1
Thread Starter
New Member
IsNumeric problems
Hi, I'm using vb6 to control some linear motion stages at work. Basically they're set up in an XYZ layout. The user inputs the locations where they would like the stages to go. On the interface I just created 3 columns, X Y and Z, with 16 rows. What I want to happen is that if they only need the stages to go to 4 locations, that by leaving the rest of the cells blank or putting in "none", ie any non-numeric input, that the program will skip the rest of the cells. So I wrote a function called TestValuesNumeric as a boolean where I have the following code segment:
If IsNumeric(Val(Trim((TextX1.Text)))) Then
TestValuesNumeric = True
Else
TestValuesNumeric = False
Exit Function
End If
Then in the function controlling the stages, before I call each move I have a check condition.
If TestValuesNumeric = True Then
...Execute Moves...
End If
This should work so that if the input value was not a number it should skip over all that set of moves and go onto the next set. However, this is not happening, its blowing right through the check and attempting to parse the input into the distance calculator, which yells at me for not having numeric input. The weird part is that the distance calculator function uses the IsNumeric check.
Any help would be greatly appreciated. Thanks.
-
Aug 28th, 2009, 02:02 PM
#2
Re: IsNumeric problems
Welcome to the forums.
IsNumeric() may return true for things you don't expect: &Head, 1E23, $12, and possibly some other "numerical" formats. The three examples above are in formats: Hex, Scientific, money.
You might want to write to a log, temporarily, what values are being accepted as numeric, then review the log afterwards.
Last edited by LaVolpe; Aug 28th, 2009 at 02:10 PM.
-
Aug 28th, 2009, 02:11 PM
#3
Re: IsNumeric problems
The problem is also caused by the use of the Val function.
Val will always return a number (double), no matter what is in the textbox.
Val("none")
Val("")
Val("$12.00")
will all return 0.
-
Aug 28th, 2009, 02:23 PM
#4
Thread Starter
New Member
Re: IsNumeric problems
Thank you Brucede, that fixed everything. I've never written in vb before, I'm picking up where an employee before me left off. So I'm learning as I go. He called the IsNumeric like that elsewhere in the program, and there was no documentation left behind saying what worked and what didn't. But that was the last little kink I needed to work out. Thanks again for the prompt and accurate reply.
-
Aug 28th, 2009, 02:34 PM
#5
Thread Starter
New Member
Re: IsNumeric problems
One more quick question, is it possible to check to see if two values are numeric simultaneously? I want to check to see if Zstartdepth.Text and ZendDepth.Text are both numbers. If one isn't I want the whole expression to be parsed as false. This is probably possible, but like I said I am not familiar with syntax structure of vb.
-
Aug 28th, 2009, 02:59 PM
#6
Re: IsNumeric problems
If IsNumeric(Trim((TextX1.Text))) And IsNumeric(Trim((TextX2.Text))) Then
-
Aug 28th, 2009, 03:10 PM
#7
Re: IsNumeric problems
I have a lot of numbering calculations and found it best to restrict input to values only using the keypress(keyascii) and lost focus (incase they pasted)
Waiting for a full featured smart phone with out marrying a provider
Go Android
Go raiders
-
Aug 28th, 2009, 03:10 PM
#8
Thread Starter
New Member
Re: IsNumeric problems
Thank you. I swear this is my last question. Can you have it test to see if an input is a negative number?
-
Aug 28th, 2009, 03:13 PM
#9
Re: IsNumeric problems
Code:
If IsNumeric(criteria) Then
If Val(criteria) < 0 Then ' is negative
End If
End If
-
Aug 28th, 2009, 03:14 PM
#10
Re: IsNumeric problems
Originally Posted by isnoend07
I have a lot of numbering calculations and found it best to restrict input to values only using the keypress(keyascii) and lost focus (incase they pasted)
Instead of LostFocus I would advise the Validate event. This is because there are many cases where LostFocus is not fired, while the Validate event is quite reliable.
-
Aug 28th, 2009, 03:36 PM
#11
Re: IsNumeric problems
If you want to you can use my NumberBox ActiveX control which is a textbox that only allows numbers. It also has properties you can set that specify if a number can be negative and/or how many decimal places it can have.
-
Aug 28th, 2009, 06:56 PM
#12
Re: IsNumeric problems
Originally Posted by BigSich
Hi, I'm using vb6 to control some linear motion stages at work. Basically they're set up in an XYZ layout. The user inputs the locations where they would like the stages to go. On the interface I just created 3 columns, X Y and Z, with 16 rows. What I want to happen is that if they only need the stages to go to 4 locations, that by leaving the rest of the cells blank or putting in "none", ie any non-numeric input, that the program will skip the rest of the cells. So I wrote a function called TestValuesNumeric as a boolean where I have the following code segment:
If IsNumeric(Val(Trim((TextX1.Text)))) Then
TestValuesNumeric = True
Else
TestValuesNumeric = False
Exit Function
End If
Then in the function controlling the stages, before I call each move I have a check condition.
If TestValuesNumeric = True Then
...Execute Moves...
End If
This should work so that if the input value was not a number it should skip over all that set of moves and go onto the next set. However, this is not happening, its blowing right through the check and attempting to parse the input into the distance calculator, which yells at me for not having numeric input. The weird part is that the distance calculator function uses the IsNumeric check.
Any help would be greatly appreciated. Thanks.
hi Bigsich try like this
Code:
Private Sub Command1_Click()
Dim TestValuesNumeric As Boolean
Dim PointChr As Boolean
Dim ChekChr, Txt As String
PointChr = False
Txt = Trim(Text1.Text)
For i = 1 To Len(Txt)
ChekChr = Mid(Txt, i, 1)
Select Case ChekChr
Case Chr(48) To Chr(57), Chr(46), Chr(45) '0 to 9, dot, hyphen
TestValuesNumeric = True
Case Else
TestValuesNumeric = False
MsgBox TestValuesNumeric
Exit Sub
End Select
'check decimal value
If ChekChr = Chr(46) Then 'allow only one point charcter
If PointChr = True Then
TestValuesNumeric = False
MsgBox TestValuesNumeric
Exit Sub
End If
PointChr = True
End If
'check negative value
If ChekChr = Chr(45) Then 'if minus(hyphen) charcter
If i = 1 Then 'allow only in first position
TestValuesNumeric = True
Else
TestValuesNumeric = False
MsgBox TestValuesNumeric
Exit Sub
End If
End If
Next
MsgBox TestValuesNumeric
End Sub
Last edited by seenu_1st; Aug 29th, 2009 at 10:08 PM.
-
Jun 28th, 2017, 04:20 PM
#13
Hyperactive Member
Re: IsNumeric problems
this might be a simple solution
if isNumeric(theText) then
if CStr(val(theText)) = theText then
''really is a number and not hex
end if
end if
-
Jun 28th, 2017, 08:07 PM
#14
Re: IsNumeric problems
Originally Posted by axisdj
this might be a simple solution
if isNumeric(theText) then
if CStr(val(theText)) = theText then
''really is a number and not hex
end if
end if
Please do not post to very old threads. This one is 8 years old.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|