Results 1 to 14 of 14

Thread: [RESOLVED] count total lable ? number

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Resolved [RESOLVED] count total lable ? number

    label1.caption=4
    label2.caption=3
    label3.caption=6

    total is 13 ,

    by click of a button how do i count all that then output to label5.caption=total:13

    ?

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: count total lable ? number

    vb Code:
    1. Private Sub Command1_Click()
    2. Label4.Caption = Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption)
    3. End Sub

    Edit:

    or if you want the label to say the word "Total"

    vb Code:
    1. Label4.Caption = "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption)
    Last edited by Nightwalker83; Jul 26th, 2013 at 02:45 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: count total lable ? number

    (Learning from a thread from yesterday--posted by DataMiser)...
    Val() is not really a good choice as it can give bogus results. It basically reads any number at the begining of the string and returns that number or 0

    Val("1A") ' would return 1
    Val("A1") ' would return 0 but neither of these are a valid numeric entry

    Best to use the IsNumeric() function to test if the string contains numeric data and then the proper convert function to convert that string to the proper type of number
    CInt(), CLng() CCur() for example
    So perchance a 'value' other than a number (e.g. "A4") accidentally shows up in one of your labels, you could experience erroneous sums.

  4. #4
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: count total lable ? number

    Brief (using as few words as possible) Original Posts, do not earn you Brownie Points nor admiration (except from the other members who constantly post brief posts).
    For example you could tell us what precautions are already built into your program to ensure that only valid numbers are used, and that they are not empty ("" or " ").
    I notice that your post says the values are in Labels. Presumably they got there from somewhere else, and not directly typed by a user ?
    Why are you not using the source, as the input to the 'equation'/summing ?
    If your Original Post is just something you pulled out of thin air, with little thought, you are not giving potential helpers the courtesy they deserve.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: count total lable ? number

    Yep... to me the OP makes no sense. Why would you use labels and have a need to add them together.

    BTW the count would be 3, the sum would be 13

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: count total lable ? number

    And avoid using the Val() function which is locale-blind and always returns a Double.

    This is why we have typed locale-aware functions such as CInt(), CLng(), CSng(), etc.

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: count total lable ? number

    Quote Originally Posted by SamOscarBrown View Post
    So perchance a 'value' other than a number (e.g. "A4") accidentally shows up in one of your labels, you could experience erroneous sums.
    I just tried that and it ignored the data completely

    vb Code:
    1. Private Sub Form_Load()
    2. Label1.Caption = "A4"
    3. Label2.Caption = "6"
    4. Label3.Caption = "9"
    5. Label4.Caption = "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption)
    6. End Sub

    Returns 15
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: count total lable ? number

    Quote Originally Posted by Nightwalker83 View Post
    I just tried that and it ignored the data completely

    vb Code:
    1. Private Sub Form_Load()
    2. Label1.Caption = "A4"
    3. Label2.Caption = "6"
    4. Label3.Caption = "9"
    5. Label4.Caption = "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption)
    6. End Sub

    Returns 15
    Yep that is totally normal operation, VAL() looks left to right for numeric digits and stops when it finds anything not a digit, If there was a digit or more in the left most positions then it reports the value of those digits.

    So Val("457A756") would return 457 Val(">457A756") would return 0

    But of course neither of those source strings are numeric values and should not be counted at all hence the need to use IsNumeric unless you have other code in place to insure that only numeric data is in those strings.

    Then again using Label.Caption property to stored numbers and then add them up from there is just not a very good approach to start with,


    Another example to consider
    Code:
        Label1.Caption = "47"
        Label2.Caption = "1,506"
        Label3.Caption = "9.78"
        Debug.Print "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption)
    Returns
    Total is, 57.78
    Last edited by DataMiser; Jul 27th, 2013 at 12:56 AM.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: count total lable ? number

    Going a little off topic, IsNumeric recognises scientific notation so, for example, IsNumeric("1e2"), would return True. Val("1e2") would return 100. I tend to avoid using IsNumeric where User Input is involved.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: count total lable ? number

    So what would be the issue there with IsNumeric() ? Val() also recognizes this notation. It would seem that IsNumeric() works correctly, Val() on the other hand needs to have a valid numeric entry or not start with a digit.
    Consider these examples
    Code:
    Val("1e2")=100
    IsNumeric("1e2")=True
    
    Val("1a2")=1
    IsNumeric("1a2")=False
    In the first case both return a result that shows a valid number
    In the second case Val() fails and returns the number 1 where IsNumeric() shows that it was not a valid numeric entry which is the whole point of using the IsNumeric() function when user input is involved. Of course IsNumeric() is not fool proof either but is closer than Val()

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: count total lable ? number

    Depending on the Application and the User(s) there's a risk that accidently entering 1e2 into a field that expects a number would be accepted as valid.

    I might be making a generalisation but there's a lot of people out there that don't understand scientific notation and they would be unlikely to deliberately enter 1e2 instead of 100. Just relying on IsNumeric, to my mind, is not sufficient.

  12. #12

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: count total lable ? number

    this issue has been solved , each counter were it generates each number i placed my total code there

    so when each counter +1 goes up the total label will count 1 for each simple approach

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: count total lable ? number

    Excellent! I was just making sure you understood that "IF" one of your labels was NOT a number, then your summation might be incorrect.
    Please mark this thread as Resolved.

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: count total lable ? number

    Quote Originally Posted by Doogle View Post
    Depending on the Application and the User(s) there's a risk that accidently entering 1e2 into a field that expects a number would be accepted as valid.

    I might be making a generalisation but there's a lot of people out there that don't understand scientific notation and they would be unlikely to deliberately enter 1e2 instead of 100. Just relying on IsNumeric, to my mind, is not sufficient.
    Just to be clear I was not suggesting that IsNumeric() should be used alone but that it should be part of the process. First use IsNumeric() to see if the entry is a valid numeric entry and then you would use one of the conversion routines to convert the string to a numeric var of the type needed. If is numeric is not used then you can either get a bogus value [from Val()] or a RT error [from Cxxx() routines] and of course if you use neither then you could get a type mismatch error when trying to perform any numeric operation with the data.

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