-
Jul 26th, 2013, 02:31 AM
#1
Thread Starter
Banned
[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
?
-
Jul 26th, 2013, 02:37 AM
#2
Re: count total lable ? number
vb Code:
Private Sub Command1_Click() Label4.Caption = Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption) End Sub
Edit:
or if you want the label to say the word "Total"
vb Code:
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
-
Jul 26th, 2013, 07:40 AM
#3
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.
-
Jul 26th, 2013, 07:51 AM
#4
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.
-
Jul 26th, 2013, 08:43 AM
#5
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
-
Jul 26th, 2013, 01:21 PM
#6
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.
-
Jul 26th, 2013, 10:40 PM
#7
Re: count total lable ? number
Originally Posted by SamOscarBrown
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:
Private Sub Form_Load() Label1.Caption = "A4" Label2.Caption = "6" Label3.Caption = "9" Label4.Caption = "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption) 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
-
Jul 27th, 2013, 12:52 AM
#8
Re: count total lable ? number
Originally Posted by Nightwalker83
I just tried that and it ignored the data completely
vb Code:
Private Sub Form_Load() Label1.Caption = "A4" Label2.Caption = "6" Label3.Caption = "9" Label4.Caption = "Total is, " & Val(Label1.Caption) + Val(Label2.Caption) + Val(Label3.Caption) 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.
-
Jul 27th, 2013, 01:13 AM
#9
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.
-
Jul 27th, 2013, 01:49 AM
#10
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()
-
Jul 27th, 2013, 02:07 AM
#11
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.
-
Jul 27th, 2013, 04:43 AM
#12
Thread Starter
Banned
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
-
Jul 27th, 2013, 07:19 AM
#13
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.
-
Jul 27th, 2013, 10:53 AM
#14
Re: count total lable ? number
Originally Posted by Doogle
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|