Standards & Practices - Better use of MsgBox

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15640
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Standards & Practices - Better use of MsgBox

Post by ChrisGreaves »

I must be fairly new to this VBA stuff; don't know why I didn't adopt this practice before ...

I am wading thorough acres hectares of code replacing
MsgBox "You don't appear to have any words interesting enough to index!"
with
MsgBox "IndexActiveDocument- You don't appear to have any words interesting enough to index!"

That is, including the name of the procedure with every MsgBox (or my custom logging procedure) call.

I am motivated by an error report from a user who says "I got just an empty pop-up message box".
I don't know whether the MsgBox was mine or not, but from now on, I'll know if it WAS one of mine; also I'll get some idea of what the user might have been attempting.
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Standards & Practices - Better use of MsgBox

Post by agibsonsw »

You could add the name of the procedure to the Title - this might be less confusing for the user.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15640
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Standards & Practices - Better use of MsgBox

Post by ChrisGreaves »

agibsonsw wrote:You could add the name of the procedure to the Title - this might be less confusing for the user.
Thanks Andy!

Like I said I must be fairly new to this VBA stuff; :doh:
He who plants a seed, plants life.

User avatar
StuartR
Administrator
Posts: 12612
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Standards & Practices - Better use of MsgBox

Post by StuartR »

I have a variable strMsgTitle that I set to the name of the procedure at the start of each Sub or Function and use as the title of all message boxes.
StuartR


User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Standards & Practices - Better use of MsgBox

Post by agibsonsw »

I understand there is no straight-forward way to get the name of the currently running procedure. This is a shame as it would make the coding of the MsgBox, for every procedure, easier.

This page http://www.cpearson.com/excel/InsertProcedureNames.aspx provides code to achieve this, but it's probably only worth using if there are lots of procedures involved. Still, an interesting piece of code :smile:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Standards & Practices - Better use of MsgBox

Post by JohnH »

When I add an error handler using MZ Tools it adds the name of the procedure and the current form name automatically.

Code: Select all

 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure sbtest of VBA Document Form_frmMainMenu" 
But when I then copy and paste the procedure into a different context I have to remember to change it.
ErrorHandler.gif
You do not have the required permissions to view the files attached to this post.
Regards

John

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15640
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Standards & Practices - Better use of MsgBox

Post by ChrisGreaves »

agibsonsw wrote:This page http://www.cpearson.com/excel/InsertProcedureNames.aspx provides code to achieve this,
And a beautiful piece of code it is, too.
I wonder hy he disallows lower-case letters in identifiers.

Code: Select all

strcProcName
He who plants a seed, plants life.

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Standards & Practices - Better use of MsgBox

Post by HansV »

ChrisGreaves wrote:I wonder hy he disallows lower-case letters in identifiers.

Code: Select all

strcProcName
Sorry, I don't understand your remark.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15640
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Standards & Practices - Better use of MsgBox

Post by ChrisGreaves »

HansV wrote:"strcProcName" Sorry, I don't understand your remark.
Hi Hans.
I emailed Chip and he promptly (no kidding!) emailed back.
There is an "Option Compare Text" directive which I've not used before.
I am currently essaying in Word2003 & Excel2003 to see why my system still baulks on lower-case letters.
He who plants a seed, plants life.