For those that cannot download due to security settings at their place of employ here is the code.
First, the form, named frmMain has 3 controls:
CommandButton, named btnOk
CommandButton, named btnCancel
TextBox, named tbxMain
frmMain code --
Option Explicit
Private myExitState As Boolean
Public Property Get ExitState() As Boolean
'' Return the value hosted our private member variable.
'' Since we have no corresponding Let Property statement
'' it's ostensibly read-only.
ExitState = myExitState
End Property
Public Property Get UserText() As String
'' Return the current value of the text in the tbxMain
'' object. While the way VB[A] is structured it allows
'' access to the object directly we should not go that
'' route [IMO]. A future implementation may use a
'' different object, or otherwise employ a different
'' implementation. Such changes should not affect clients
'' or consumers of the form. Also, this gives us the
'' opportunity to perform other activities before we
'' return the value, like qualifying, logging, whatever.
UserText = tbxMain.text
End Property
Public Property Let UserText(value As String)
'' Allow the client set the value of the txbMain object,
'' BUT NOT DIRECTLY. See balance of comments in the Get
'' UserText Property.
tbxMain.text = value
End Property
Private Sub btnOk_Click()
'' Set the state and then hide the form.
IndicateAccept
Me.Hide
End Sub
Private Sub btnCancel_Click()
'' Set the state and then hide the form.
IndicateCancel
Me.Hide
End Sub
Private Sub IndicateAccept()
'' As there may be more than one way this is triggered
'' let's have one procedure have responsibility for
'' setting the state. While this seems overkill because
'' we have but one little variable representing state,
'' in a big application there could be many things that
'' have to be done aside from merely setting said variable.
'' This gives us one entry point, and thus, only one area
'' to edit when things change, and more often than not
'' they will.
myExitState = True
End Sub
Private Sub IndicateCancel()
'' See comment in Sub IndicateAccept.
myExitState = False
End Sub
Private Sub UserForm_Activate()
'' Start with state indicating cancel.
IndicateCancel
End Sub
Private Sub UserForm_Initialize()
'' Start with state indicating cancel.
IndicateCancel
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'' If the user closed the form via the control
'' box instead of the [Ok] or [Cancel] buttons ...
If CloseMode = VbQueryClose.vbFormControlMenu Then
IndicateCancel
End If
End Sub
And then the module code, in a module named mdlMain --
Option Explicit
Public Sub Main()
Dim myForm As frmMain
Set myForm = New frmMain
'' Access and use the property(s) we exposed.
myForm.UserText = "Mark's great adventure."
'' Alrighty, let's show it.
myForm.Show
'' Program execution will not end up here until the
'' user closes the form, either by clicking the [Ok]
'' or [Cancel] buttons or via the control box (the
'' [x] in the top right corner).
If Not (myForm Is Nothing) Then
'' An error may be thrown if the user closes the
'' form via the control box because it will have
'' been unloaded but not yet nothing (let's trap
'' that possibility).
On Error GoTo ErrHandler
If myForm.ExitState Then
'' access the UserText Property we exposed
MsgBox _
"User pressed [Ok]." & vbCrLf & vbCrLf & _
"UserText = <" & _
myForm.UserText & ">"
Else
MsgBox "User pressed [Cancel]."
End If
Unload myForm
Set myForm = Nothing
Exit Sub
ErrHandler:
If Err.Number = -2147418105 Then
MsgBox _
"The user closed the form via the " & _
"form's control box."
Else
'' Uhhh, what the h3ll? An error we didn't
'' anticipate. Let's display the error
'' description.
MsgBox Err.Description
End If
Err.Clear
Resume OutOfHere
End If
OutOfHere:
Set myForm = Nothing
End Sub
See a problem, see bad advice etc? Let me / us know. Thanks!