vanhunk
07-22-2015, 07:10 AM
Passing values of variables from a userform to the main calling code in a normal module based on which command button on the form was clicked:
I have a userform that has a message and 2 buttons, an OK button and a Cancel button.
How it is supposed to work is as follows:
When the main code in a normal module is executed, the form must open. If the OK button is clicked, the action must be referred back to the main code and some further action performed, based on the fact that the OK button was clicked. If the Cancel button was clicked the action must again be referred back to the main code and some other action performed, again based on the fact that the Cancel button was clicked.
I would for example thus like to have the values of bOK and bCancel (as below) sent to the main code, but without declaring them as public variables (because I would like to use the same variables for other forms and not cause confusion)
The form also has a timer (as below) and when the allowed interaction period has expired the code must behave exactly the same as if bOK has been clicked. In other words it has to revert back to the main code and perform some action as if the OK button has been clicked.
I have the following code in the userform module "frmEmailAlert" as attached:
Option Explicit
Private bOK As Boolean
Private bCancel As Boolean
Private Sub cmdCancel_Click()
bCancel = True
End Sub
Private Sub UserForm_Activate()
Dim dTimer As Double
dTimer = Timer
Do While Timer < dTimer + 5 And Not bOK And Not bCancel
DoEvents
Loop
Unload Me
End Sub
Private Sub cmdOK_Click()
bOK = True
End Sub
The following code is in a regular module "MainCodeModule" as attached:
Option Explicit
Sub MainCode()
frmEmailAlert.Show vbModeless
End Sub
Regards,
vanhunk
I have a userform that has a message and 2 buttons, an OK button and a Cancel button.
How it is supposed to work is as follows:
When the main code in a normal module is executed, the form must open. If the OK button is clicked, the action must be referred back to the main code and some further action performed, based on the fact that the OK button was clicked. If the Cancel button was clicked the action must again be referred back to the main code and some other action performed, again based on the fact that the Cancel button was clicked.
I would for example thus like to have the values of bOK and bCancel (as below) sent to the main code, but without declaring them as public variables (because I would like to use the same variables for other forms and not cause confusion)
The form also has a timer (as below) and when the allowed interaction period has expired the code must behave exactly the same as if bOK has been clicked. In other words it has to revert back to the main code and perform some action as if the OK button has been clicked.
I have the following code in the userform module "frmEmailAlert" as attached:
Option Explicit
Private bOK As Boolean
Private bCancel As Boolean
Private Sub cmdCancel_Click()
bCancel = True
End Sub
Private Sub UserForm_Activate()
Dim dTimer As Double
dTimer = Timer
Do While Timer < dTimer + 5 And Not bOK And Not bCancel
DoEvents
Loop
Unload Me
End Sub
Private Sub cmdOK_Click()
bOK = True
End Sub
The following code is in a regular module "MainCodeModule" as attached:
Option Explicit
Sub MainCode()
frmEmailAlert.Show vbModeless
End Sub
Regards,
vanhunk