• Use of variables in MsgBoxes (VBA (Access etc.))

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Use of variables in MsgBoxes (VBA (Access etc.))

    Author
    Topic
    #441729

    Just out of curiousity…

    When making a MsgBox, I usually construct the strings all on the same line. I’ve noticed that many people declare variables even for the elements that don’t change. For example, the two Subs below perform the same task. I’d tend to use the latter because I find it easier to write and read. Is there ANY advantage to the former method?

    Sub PieEating1()
        Dim strTitle As String
        Dim strMessage1 As String
        Dim strMessage2 As String
        Dim strMessage As String
        Dim intPieCount As Integer
        Dim intDialog As Integer
        
        strTitle = "How many pies have you eaten?"
        strMessage1 = "You have eaten "
        strMessage2 = " pies."
        intPieCount = 3
        strMessage = strMessage1 & CStr(intPieCount) & strMessage2
        intDialog = vbExclamation
        MsgBox strMessage, intDialog, strTitle
    End Sub
    Sub PieEating2()
        Dim intPieCount As Integer
        
        intPieCount = 3
        MsgBox "You have eaten " & CStr(intPieCount) & " pies.", _
            vbExclamation, _
            "How many pies have you eaten?"
    End Sub
    

    Hope someone can shed some light on this. Thanks!

    Mark

    Viewing 2 reply threads
    Author
    Replies
    • #1061430

      I think it’s just a matter of personal preference. I tend to use the latter method too, being lazy. I’d even omit the CStr function:

      MsgBox “You have eaten ” & intPieCount & ” pies.”, _

      will work just as well.

      • #1061435

        Thanks, Hans, that’s reassuring.

        One of my university tutors always advocated laziness (at least in the sense of taking the most efficient route to a good end result), so I can now happily continue to follow his advice.

        • #1061459

          The only advantage of the first one is if you use similar messages elsewhere in the routine – then you can reuse the variables. Otherwise it’s just a question of which you find clearer – in programming terms I generally advocate clarity over brevity and/or efficiency, unless you can be certain it’s a one-off piece of code. (and generally when I’m certain, I’m wrong … grin)

          • #1061490

            Hi Rory

            In the past, when developing code for colleagues, I always asked “Is this a one-off activity; or will you be using this code again?”. Invariably the answer was “One-off.”. Invariably they lied. Invariably I had to back engineer my code.

            I now try to keep reminding myself rantonSpend the time and effort to provide clarity. “. rantoff

    • #1061575

      I tend to use a third approach, I define CONSTants to hold the strings. This is more efficient than using variables for values that don’t change while the code is running, and it keeps all my text in a single place.

      Not only does this make it easier to change things, but it also makes translation much easier as all user displayable text is in a single place.

      StuartR

    • #1061586

      > I find it easier to write and read.
      My primary qualification is always: “How easy will this be for me or someone else to maintain or adapt six months from now?”

      • #1061589

        Thanks for your responses everyone. Very interesting!

        I think everyone learns the importance of “ease of maintenance” the hard way, by getting horribly burned when an old project rears its ugly head. And it’s the same with those supposedly “one-off” projects.

        To go back to my original questions, I think I’ll continue to take the second option because it’s easier to write and read. However, I think constants are more appropriate for text that can be re-used elsewhere.

        Regards,

        Mark

    Viewing 2 reply threads
    Reply To: Use of variables in MsgBoxes (VBA (Access etc.))

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: