Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Nov 2002
    Location
    Farnborough, Hampshire, England
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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?

    <pre>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</pre>

    <pre>Sub PieEating2()
    Dim intPieCount As Integer

    intPieCount = 3
    MsgBox "You have eaten " & CStr(intPieCount) & " pies.", _
    vbExclamation, _
    "How many pies have you eaten?"
    End Sub
    </pre>



    Hope someone can shed some light on this. Thanks!

    Mark

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.))

    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.

  3. #3
    Lounger
    Join Date
    Nov 2002
    Location
    Farnborough, Hampshire, England
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.))

    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.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.))

    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 ... <img src=/S/grin.gif border=0 alt=grin width=15 height=15>)
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Use of variables in MsgBoxes (VBA (Access etc.))

    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 <img src=/S/ranton.gif border=0 alt=ranton width=66 height=37> " Spend the time and effort to provide clarity. ". <img src=/S/rantoff.gif border=0 alt=rantoff width=66 height=37>
    Regards
    Don

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.))

    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

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.

    > 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?"

  8. #8
    Lounger
    Join Date
    Nov 2002
    Location
    Farnborough, Hampshire, England
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Use of variables in MsgBoxes (VBA (Access etc.

    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

Posting Permissions

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