Results 1 to 14 of 14

Thread: IsNumeric problems

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    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.

  6. #6

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    10

    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?

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: IsNumeric problems

    Code:
    If IsNumeric(criteria) Then
        If Val(criteria) < 0 Then ' is negative
    
        End If
    End If
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: IsNumeric problems

    Quote Originally Posted by isnoend07 View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    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.

  12. #12
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: IsNumeric problems

    Quote Originally Posted by BigSich View Post
    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.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  13. #13
    Hyperactive Member
    Join Date
    Jul 2002
    Posts
    481

    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

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: IsNumeric problems

    Quote Originally Posted by axisdj View Post
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width