Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Val("Age: 37.1") returns 0

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Austin, TX
    Posts
    120

    Resolved [RESOLVED] [2005] Val("Age: 37.1") returns 0

    My code:

    VB Code:
    1. label1.text="Age: 37.1"
    2. msgbox Val(label1.text)

    The messagebox displays 0. Isn't val supposed to extract all numbers and strip the text? I was wanting it to return 37.1

    Thanks,
    Corey

    This is VS2005 .NET 2005... the program is written for PocketPC (CF 2) but I think this applies to any VB code.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Val("Age: 37.1") returns 0

    Val() does exactly what the MSDN help topic for that function say it does.
    The Val function stops reading the string at the first character it cannot recognize as part of a number.
    Your string starts with "A" which is not recognised as part of a number, so nothing gets read. If your string was something like "37.1 years" then Val() would be useful.

    I'm not sure where you're getting this string from but you should endeavour to keep your, which is the number, and your labels separate. Having a value in a string with some data describing that value is a bad situation and should be avoided if at all possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [2005] Val("Age: 37.1") returns 0

    Just a note, using VB6.0 I've also encountered a problem when the numeric value contains comma like 1,000 for example, though I am not sure if it is still applicable in .Net.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Val("Age: 37.1") returns 0

    Val, as already stated, will not work in that case. If you're just trying to find the value for this one instance, you could just do:

    VB Code:
    1. Label1.Text = "Age: 37.1"
    2. MsgBox Trim(Mid(Label1.Text, InStr(Label1.Text, ":") + 1))

    That will only work if the only text after "Age: " is what you are looking for. In other words, if you have "Age: 37.1, Name: John Doe" then the code will return "37.1, Name: John Doe" unless you set some sort of length boundaries to it. It would all depend on the complete text you are trying to get this information from. But if all you're trying to parse is "Age: 37.1" or some random number, then the above code will do just fine(although I haven't tested it, it should still work fine).
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Austin, TX
    Posts
    120

    Re: [2005] Val("Age: 37.1") returns 0

    Thank you for all of your suggestions. I was trying to take the easy way out, but I just seperate the label into two parts, the text and the value. When I originally created the label, I didn't think I would need to reference it later. Thank you for all of your help!

    -Corey

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