View Full Version : Solved: Check to see if Msgbox is invoked in the userform
I use the Msgbox to output error messages in my userform. I'd like to know when the msgbox is not invoked so i could enable a command button to move to the next screeen. How can i do a check to see how many times the msgbox subroutine has been called from my userform?.
Bob Phillips
11-28-2006, 08:58 AM
I use the Msgbox to output error messages in my userform. I'd like to know when the msgbox is not invoked so i could enable a command button to move to the next screeen. How can i do a check to see how many times the msgbox subroutine has been called from my userform?.
You would have to setup a counter in your code.
Len Piwowar
11-28-2006, 11:22 AM
You could use the Tag property of the userform to store your msgbox counts, In the msgbox subroutine you add the code:
MsgCnt = userform.tag + 1
I use the Msgbox to output error messages in my userform. I'd like to know when the msgbox is not invoked so i could enable a command button to move to the next screeen. How can i do a check to see how many times the msgbox subroutine has been called from my userform?.as others have suggested, you will need a counter. To solve this for just one userform, you probably just want to increment a counter (could be a global or something private to the userform depending on what you want to do with it) for each call to MsgBox. If this is a more general problem, you may wish to write a procedure that "looks" like MsgBox, but does the counting for you. Let's call it MsgBoxC (for counter). MsgBoxC has the same basic arguements as MsgBox and (1) calls MsgBox and (2) increments the counter (which is a global or something easily zeroed and evaluated).
Bob Phillips
11-29-2006, 01:31 AM
as others have suggested, you will need a counter. To solve this for just one userform, you probably just want to increment a counter (could be a global or something private to the userform depending on what you want to do with it) for each call to MsgBox. If this is a more general problem, you may wish to write a procedure that "looks" like MsgBox, but does the counting for you. Let's call it MsgBoxC (for counter). MsgBoxC has the same basic arguements as MsgBox and (1) calls MsgBox and (2) increments the counter (which is a global or something easily zeroed and evaluated).
A MsgBox class :)
asingh
11-29-2006, 02:25 AM
Is there something like a msgbox class......?
Bob Phillips
11-29-2006, 02:50 AM
Is there something like a msgbox class......?
No, you write one.
It would have (at least) one method which would be ShowMessage, which would have all the same arguments as MsgBox, but that method would also do other things, in this instance it could add to a counter. There would then be properties to look at attributes of such a class, such as DisplayCount.
Here is a rough and ready example
Option Explicit
Const mClassTitle = "Show Message Class"
Private mCount As Long
Property Get DisplayCount() As Long
DisplayCount = mCount
End Property
Property Let DisplayCount(Count As Long)
mCount = Count
End Property
Public Function ShowMessage(ByVal Prompt As String, _
Optional ByVal buttons As VbMsgBoxStyle, _
Optional ByVal title As String, _
Optional ByVal helpfile As String, _
Optional ByVal context As Double) As VbMsgBoxStyle
Dim ans As VbMsgBoxStyle
mCount = mCount + 1
If title = "" Then title = mClassTitle
ans = MsgBox(Prompt, buttons, title, helpfile, context)
End Function
and here is an example of usage
Sub TestMsgBoxClass()
Dim MyMessages As clsMsgBox
Set MyMessages = New clsMsgBox
MyMessages.ShowMessage ("hello")
MsgBox MyMessages.DisplayCount
MyMessages.ShowMessage "custom buttons", vbOKCancel + vbInformation
MsgBox MyMessages.DisplayCount
MyMessages.ShowMessage "custom title", , "custom title"
MsgBox MyMessages.DisplayCount
MyMessages.DisplayCount = 0
MyMessages.ShowMessage "reset the count"
MsgBox MyMessages.DisplayCount
Set MyMessages = Nothing
End Function
I just use a flag variable everytime i use msgbox. So if the flag is true, i know there have been messages displayed
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.