-
January 31st, 2009, 06:39 AM
#1
Easy MsgBox!
I use MsgBox a lot in development to make sure that variables have the proper values before proceeding further.
I wrote the following Sub to display up to 4 variables, and take care of bothersome keystrokes and converions!
Please review, use, and suggest some improvements!
' Easy MsgBox
Sub mb(String1 As Variant, Optional String2 As Variant, Optional String3 As Variant, Optional String4 As Variant)
Dim txt$
' String1
If IsMissing(String1) Then String1 = ""
If IsNumeric(String1) Then String1 = Str$(String1)
' String2
If IsMissing(String2) Then String2 = ""
If IsNumeric(String2) Then String2 = Str$(String2)
If String2 <> "" Then String2 = " " + String2
' String3
If IsMissing(String3) Then String3 = ""
If IsNumeric(String3) Then String3 = Str$(String3)
If String3 <> "" Then String3 = " " + String3
' String4
If IsMissing(String4) Then String4 = ""
If IsNumeric(String4) Then String4 = Str$(String4)
If String4 <> "" Then String4 = " " + String4
' display message
txt$ = Trim$(String1 & String2 & String3 & String4)
MsgBox (txt$)
End Sub
-
January 31st, 2009, 04:12 PM
#2
Re: Easy MsgBox!
On first thought, despite all rumours, optional parameters can also be other types than variant.
Then, instead of using an If IsMissing(Param), you might use the default value definition which is optional to the optional parameter. It goes like
Code:
Sub mb(String1 As Variant = "", Optional String2 As Variant = "" , Optional String3 As Variant = "" , Optional String4 As Variant[ B]= ""[/B])
This makes sure, all missing parameters are defaulted to the "" (empty string)´, without you having to do an If IsMissing() for every parameter.
-
January 31st, 2009, 04:46 PM
#3
Re: Easy MsgBox!
Not in VB6... Declare them and then assign them to nothing.
-
February 2nd, 2009, 07:35 AM
#4
Re: Easy MsgBox!
What do you mean, "not in VB6", David?
I'm sure I can have something like:
Code:
Sub MySub(Optional StrPar as String = "abc")
'gives StrPar the default value of "abc"
debug.print StrPar
End Sub
You don't need the variant here. Only an IsMissing(StrPar) would never be true, even if the parameter is really not given in a call.
-
February 2nd, 2009, 10:17 PM
#5
Re: Easy MsgBox!
I meant assigning to a variable when you declare it:
Code:
Dim StrPar as String = "abc"
is VB.Net.
Code:
Dim StrPar as String
StrPar = "abc"
Is VB6...
-
February 3rd, 2009, 09:27 AM
#6
Re: Easy MsgBox!
Oh yes. That's right. But that's not what I had written.
I did no Dim, but just declared a parameter to a sub:
Sub MySub(Optional StrPar as String = "abc")
which is perfect VB6.
-
February 9th, 2009, 10:58 AM
#7
Re: Easy MsgBox!
I found a simpler code.
Let Errors do the job!
' Easy MsgBox (up to four items of any type)
Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
Const divider = " "
Dim message As String, V As Variant
On Error GoTo ErrorHandler
V = A: message = Trim$(V) & divider
V = B: message = Trim$(message) & divider & Trim$(V)
V = C: message = Trim$(message) & divider & Trim$(V)
V = D: message = Trim$(message) & divider & Trim$(V)
MsgBox (message)
Exit Sub
ErrorHandler:
message = Trim$(message) & divider & Trim$(Str$(V))
Resume Next
End Sub
Pls understand that "rumours optional parameters" are exactly what I need! I need a message of UP TO FOUR ITEMS!
-
February 9th, 2009, 12:06 PM
#8
Re: Easy MsgBox!
I'm quite confident, the above code would run as well without any error handler.
Since you are using the default assignment as I suggested
Optional B As Variant = ""
no error whatsoever is produced within your code
V = A: message = Trim$(V) & divider
V = B: message = Trim$(message) & divider & Trim$(V)
V = C: message = Trim$(message) & divider & Trim$(V)
V = D: message = Trim$(message) & divider & Trim$(V)
MsgBox (message)
So the error handler business is obsolete, I'd say.
Also it seems somewhat strange:
In the error handler you repeat exactly the same statement of which you obviously expected to produce an error. So you are producing an error again within the error handler?
-
February 11th, 2009, 06:54 AM
#9
Re: Easy MsgBox!
Thanks for the simplification!
On second thought, I think dividers should stay, and changed to something like " | ", to show which parameters are missing.
So, here is the latest V:
' Easy MsgBox (up to four items of any type)
Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
Const divider = " | "
Dim message As String, V As Variant
message = ""
V = A: message = V
V = B: message = message & divider & V
V = C: message = message & divider & V
V = D: message = message & divider & V
MsgBox (Trim$(message))
End Sub
-
February 11th, 2009, 08:39 AM
#10
Re: Easy MsgBox!
Looks good.
Good enough to finally start using code tags, anyway.
What about forgetting V:
Code:
Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
Const divider = " | "
Dim message As String
message = Trim$(A & divider & B & divider & C & divider & D)
MsgBox message
-
February 11th, 2009, 04:29 PM
#11
-
February 11th, 2009, 08:07 PM
#12
Re: Easy MsgBox!
Still want it shorter?
Code:
Sub mb(A As Variant, Optional B As Variant = "", Optional C As Variant = "", Optional D As Variant = "")
Const divider = " | "
MsgBox Trim$(A & divider & B & divider & C & divider & D)
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|