CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    59

    [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

    If you know how to calculate better, please help me.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2012
    Posts
    59

    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. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to calculate the total sum of the numbers

    So what you are trying to do is a numerology calcultaion?
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to calculate the total sum of the numbers

    Quote Originally Posted by DataMiser View Post
    So what you are trying to do is a numerology calcultaion?
    Yeah, looks like that...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Oct 2012
    Posts
    59

    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.

    Name:  vbc.jpg
Views: 2860
Size:  73.6 KB

    Name:  vb2.jpg
Views: 2536
Size:  33.8 KB


    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. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Oct 2012
    Posts
    59

    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.
    I SUGGEST FIRST YOU TRY ON YOUR VB6 AND THEN GIVE MY ADVICE. Because so far your advice is do nothing.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.
    Always use [code][/code] tags when posting code.

  11. #11
    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
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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!
    Then you add 4+3?

    Really starting to sound like HOMEWORK to me...

    ::calc to the rescue::
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Oct 2012
    Posts
    59

    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. #14
    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
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  15. #15
    Join Date
    Oct 2012
    Posts
    59

    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 _

Page 1 of 2 12 LastLast

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

Featured