How to place exact score on my message box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Valco
    New Member
    • Jul 2012
    • 1

    How to place exact score on my message box

    Private Sub cmd1back_Click( )
    fmeMain.Visible = True
    fmeMain.ZOrder
    fmeQ1.Visible = False
    End Sub

    Private Sub cmd2back_Click( )
    fmeQ2.Visible = False
    fmeQ1.ZOrder
    fmeQ1.Visible = True
    End Sub

    Private Sub cmd2next_Click( )
    Dim q2 As Integer
    Select Case True
    Case q2 = 0
    If opt2a.Value = True Then
    q2 = 1
    fmeQ2.Visible = False
    fmeQ3.ZOrder
    fmeQ3.Visible = True
    Else
    fmeQ2.Visible = False
    fmeQ3.ZOrder
    fmeQ3.Visible = True
    End If
    Case q2 = 1
    fmeQ2.Visible = False
    fmeQ3.ZOrder
    fmeQ3.Visible = True
    End Select
    End Sub

    Private Sub cmd3back_Click( )
    fmeQ3.Visible = False
    fmeQ2.ZOrder
    fmeQ2.Visible = True
    End Sub

    Private Sub cmd3finish_Clic k()
    Dim q3 As Integer
    If opt3c.Value = True Then
    q3 = 1
    MsgBox ("Your score is " & q1 + q2 + q3), vbExclamation + vbOKOnly, "Congrats"
    End
    Else
    MsgBox ("Your score is " & q1 + q2 + q3), vbExclamation + vbOKOnly, "Congrats"
    End
    End If
    End Sub

    Private Sub cmdStart_Click( )
    fmeMain.Visible = False
    fmeQ1.ZOrder
    fmeQ1.Visible = True
    End Sub

    Private Sub cmd1next_Click( )
    Dim q1 As Integer
    Select Case True
    Case q1 = 0
    If opt1b.Value = True Then
    q1 = 1
    fmeQ1.Visible = False
    fmeQ2.ZOrder
    fmeQ2.Visible = True
    Else
    fmeQ1.Visible = False
    fmeQ2.ZOrder
    fmeQ2.Visible = True
    End If
    Case q1 = 1
    fmeQ1.Visible = False
    fmeQ2.ZOrder
    fmeQ2.Visible = True
    End Select
    End Sub


    this is my entire code for the program.. this is an mock exam i made in vb6. as the title says i want it to place the exact score in the title box. however it only tells the score in the last frame
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    Is it possible to attach your vb codes in bytes? (go to "adavced" * "additional options" - "manage attachments", ...)

    there are some irregularities:
    - select case: ???
    Code:
    Select Case True 
    Case q2 = 0
    Must be:
    Code:
    Select Case q2
    Case 0
    - End in If Then ???
    Code:
    MsgBox ("Your score is " & q1 + q2 + q3), vbExclamation + vbOKOnly, "Congrats"
    End
    Else
    PS:
    Always use indents :
    - easier to discover errors.
    - Reads faster.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      Declare a Form Level Variable, say Dim MyScore As Integer.
      Adda Textbox/Label Control on the top of the form...(Not inside frame)
      @ the beginning, initialize it with Zero.
      Keep adding score, in Next button, and deisplay in the Text/label...

      Regards
      Veena

      Comment

      • kiseitai2
        New Member
        • Jul 2007
        • 93

        #4
        Do what QVeen72 said, which is an easy and reusable way of coding, or try changing your messagebox statement to
        Code:
        MsgBox ("Your score is " & (q1 + q2 + q3))
        .
        I get the feeling that when you invoke the message box object, it joins (&) the text with q1 before adding and joining q2 and q3. Do you get a partial score that is equal to score- q1? Experiement around with some easy numbers and check your result with the expected value to see if this is the case. Good luck!:)

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          There are various issues, as others have pointed out.

          However, I believe your fundamental problem is that the variables q1, q2 and q3 are defined as local to the routines where they're used. That means they only exist within that routine. For example, q1 exists only within the method cmd1next_Click. That means that when you display your message box, no variables called q1 or q2 exist. Because you have your VB options set to allow this, VB helpfully creates them for you - but obviously these new variables don't contain any values. So what you're effectively calculating there is "q3 + 0 + 0".
          There are two things you should do.
          1. To correct the immediate problem, define q1, q2 and q3 in the declarations section of the form (and remove them from the individual methods).
          2. To prevent this sort of problem and save yourself a lot of headaches in the future, go to Tools | Options | Editor and turn on the option "Require Variable Declaration". This will prevent you from using variables which don't exist, by adding the line "Option Explicit" at the start of each new form and module. Since that applies when forms/modules are created, you should manually enter it at the top of the declarations section of this current form.

          Hope this helps.

          P.S. I think the "q1 + q2 + q3" is actually working alright, kiseitai2 (once we allow for the variable-scope issue described above). But adding the parentheses certainly won't hurt, and does make it a bit more obvious what's intended.

          Comment

          • kiseitai2
            New Member
            • Jul 2007
            • 93

            #6
            Haha! I totally missed the scope issue. How noobish of me! I guess it's my coding style, but I usually use a minimal number of redundant parentheses to make sure the statement is explicit. After all, a compiler follows logic and an ambiguous statement (an statement with more than one way to interpret) can be dangerous since you can't predict how the compiler will handle it (unless you have already tested the code and confirmed the result). Also, it helps human readers.

            Comment

            • Acedew
              New Member
              • Apr 2015
              • 1

              #7
              that single line of code u posted kiseitia2 saved me a tone of time on my work

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                That's the great thing about these archives, of course. Here it is almost three years later, and this answer is still helping people.

                Comment

                • kiseitai2
                  New Member
                  • Jul 2007
                  • 93

                  #9
                  Nice! I never thought that the few times I have been able to contribute helped others this much!

                  Comment

                  Working...