[RESOLVED] How to calculate the total sum of the numbers
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: [RESOLVED] How to calculate the total sum of the numbers

1. [RESOLVED] How to calculate the total sum of the numbers

Hi guys, i need your help. I am beginner on this vb6 so i hope you can help me.

In Vb6 i create 1 Command button, 1 Label and 8 TextBox.

In Command button i wrote this code:

Command1_Click()

Label1 = Val(Text1) + Val(Text2) + Val(Text3) + Val(Text4) + Val(Text5) + Val(Text6) + Val(Text7) + Val(Text8)

In 8 TextBox for example i write date of birth: 1989 06 28 and then click command button. Then in label i see result 43. Now i need the number of 43 sum. ( 4 + 3 = 7).And if lets say number is 93 then 9 + 3 = 12 = 1 + 2 = 3.

I guess in vb6 code need write with If, Then, Else, End If arguments. For example:
If Label1 > 10 Then
Val(Text1) + Val(Text2)....
Else
EndIf
End sub

2. Re: How to calculate the total sum of the numbers

Hard to say with your sample.

#1 VB6 can calculate DATE() for you.
#2 Strings can use INSTR() to find a position from within a string

3. Re: How to calculate the total sum of the numbers

How to calculate date which could indicate your age i know that.
With Strings (INSTR) i do not unfamiliar with that. If they can solve my problem then say code.

Can you know other methods to calculate calculate the total sum of the numbers

4. Super Moderator Power Poster
Join Date
Jul 2008
Location
WV
Posts
5,255

Re: How to calculate the total sum of the numbers

So what you are trying to do is a numerology calcultaion?

5. Re: How to calculate the total sum of the numbers

Hmmm... This calculation looks somewhat familiar...

Here's a VBA function (used as a worksheet function) from one of my older Excel files, somewhat related to tarot cards:

Code:
```Function rqs(x, maxqs)
Dim TempX As Integer, TempQS As Integer

TempX = x
Do
TempQS = 0
Do
TempQS = TempQS + TempX Mod 10
TempX = Int(TempX / 10)
Loop While TempX > 0
TempX = TempQS
Loop While TempQS > maxqs
rqs = TempQS
End Function```
The "r" in the function name means "recursive" and the "qs" means "Quersumme", the german translation of "digit sum". As the first parameter, the function takes the number of which to calculate the digit sum. In your example that would be 19890628 (as a number!). The second parameter determines the termination condition: The function keeps calculating the digit sum of the result it gets until that result is no bigger than the second parameter. In your example you'd pass 9 for that. (Warning: Passing a value smaller than 9 for that parameter is likely to result in an endless loop.)

This rather aged example of my VBA programming skills probably has some room for improvement. I posted it as-is, just to give you something to play with...

6. Re: How to calculate the total sum of the numbers

Originally Posted by DataMiser
So what you are trying to do is a numerology calcultaion?
Yeah, looks like that...

7. Re: How to calculate the total sum of the numbers

Yes i trying to do numerology calculation.

This code is not working.

Code:
``` Function rqs(x, maxqs)
Dim TempX As Integer, TempQS As Integer

TempX = x
Do
TempQS = 0
Do
TempQS = TempQS + TempX Mod 10
TempX = Int(TempX / 10)
Loop While TempX > 0
TempX = TempQS
Loop While TempQS > maxqs
rqs = TempQS
End Function```
You say that "Here's a VBA function (used as a worksheet function) from one of my older Excel files" I need for visual basic, not for excel.

I put two pictures.
In first pic. you see 1 label,1 command button and 8 TextBox.
In second pic. you see my code.

So can you do write code according to my example involving those files (label, command button and textbox)

8. Re: How to calculate the total sum of the numbers

+ is the wrong way to CONCATENATE a string anyways. USE '&' instead!

Try making 3 Temporary labels first!

9. Re: How to calculate the total sum of the numbers

Why i need 3 labels? Identify all 3 labels function code and how i must use them.

10. Super Moderator Power Poster
Join Date
Jul 2008
Location
WV
Posts
5,255

Re: How to calculate the total sum of the numbers

I would try to help but I know little about numerology and can't quite make out what you are trying to do based on what is in your post. If you provide more detail then perhaps I could help.

11. Senior Member
Join Date
Jul 2005
Posts
1,083

Re: How to calculate the total sum of the numbers

Code:
```    Dim intTotal1 As Integer
Dim strTotal1 As String
Dim intTotal2 As Integer

intTotal1 = Val(Text1.Text) + Val(Text2.Text) + Val(Text3.Text) + Val(Text4.Text) + Val(Text5.Text) + Val(Text6.Text) + Val(Text7.Text) + Val(Text8.Text)

strTotal1 = Format(intTotal1)
intTotal2 = Val(Left(strTotal1, 1)) + Val(Right(strTotal1, 1))
Label1.Caption = Format(intTotal2)```
It would be better if you validate that every textbox has an integer number inside (0 to 9)
Also validate that the first result (intTotal1 in this example) has 2 places

12. Re: How to calculate the total sum of the numbers

In 8 TextBox for example i write date of birth: 1989 06 28 and then click command button. Then in label i see result 43. Now i need the number of 43 sum. ( 4 + 3 = 7).And if lets say number is 93 then 9 + 3 = 12 = 1 + 2 = 3.
Finally. You added all the numbers from all text boxes to get 43!

Really starting to sound like HOMEWORK to me...

::calc to the rescue::

13. Re: [RESOLVED] How to calculate the total sum of the numbers

jggtz you are awesome! Thanks you, your code is works fine.
I really am begginer so can you tell my how "validate that every textbox has an integer number inside" ?

14. Senior Member
Join Date
Jul 2005
Posts
1,083

Re: [RESOLVED] How to calculate the total sum of the numbers

Just following the example... isn't the best approach

First, in design time, to each textbox, set it's MaxLength property = 1

Code:
```    Dim intTotal1 As Integer
Dim strTotal1 As String
Dim intTotal2 As Integer

If IsNumeric(Text1.Text) And _
IsNumeric(Text2.Text) And _
IsNumeric(Text3.Text) And _
IsNumeric(Text4.Text) And _
IsNumeric(Text5.Text) And _
IsNumeric(Text6.Text) And _
IsNumeric(Text7.Text) And _
IsNumeric(Text8.Text) Then
intTotal1 = Val(Text1.Text) + Val(Text2.Text) + Val(Text3.Text) + Val(Text4.Text) + Val(Text5.Text) + Val(Text6.Text) + Val(Text7.Text) + Val(Text8.Text)
strTotal1 = Format(intTotal1, "00")
intTotal2 = Val(Left(strTotal1, 1)) + Val(Right(strTotal1, 1))
Label1.Caption = Format(intTotal2)
Else
MsgBox "Some of the textboxes has not a number"
Endif```

15. Re: [RESOLVED] How to calculate the total sum of the numbers

I did it, i set eatch textbox MaxLength property = 1.
How i continue write this code from And_
Code:
`If IsNumeric(Text1.Text) And _`

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•