(Msg. 1) Posted: Mon Nov 10, 2008 9:07 pm
Post subject: Use "X" on UserForm Archived from groups: microsoft>public>word>vba>userforms (more info?)
I have a simple userform with two buttons "OK" and "Cancel"
If the user clicks OK I want to process additional code in the calling
procedure. If the user clicks Cancel or "X" I want the calling procedure to
end.
I know how to intercept the "X" button so that it does nothing and I know
how to disable the "X" button.
In this case I want the "X" button to mimic the Cancel button. I have
found to possible solutions. One uses error handling and one uses a public
variable. I was wondering if there are pitfalls to using either method or
if a better method exists:
Project code for first form:
Sub ProcessXOnUserFormWithErrHandler()
Dim oFrm As frmTest3
Set oFrm = New frmTest3
oFrm.Show
On Error GoTo Err_Handler
If oFrm.Tag = "Canceled" Then
Unload oFrm
Set oFrm = Nothing
Exit Sub
End If
MsgBox "Do something"
Unload oFrm
Set oFrm = Nothing
Err_ReEntry:
Exit Sub
Err_Handler:
Debug.Print Err.Number & " "; Err.Description
Set oFrm = Nothing
Resume Err_ReEntry
End Sub
Userform code:
Private Sub cmdCancel_Click()
Me.Tag = "Canceled"
Me.Hide
End Sub
Private Sub cmdOK_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Me.Tag = "Canceled"
End Sub
Project code for second form:
Public boFrmCancel as Boolean
Sub ProcessXOnUserFormWithLogic()
Dim oFrm As frmTest4
Set oFrm = New frmTest4
oFrm.Show
If boFrmCancel Then
Unload oFrm
Set oFrm = Nothing
Exit Sub
End If
MsgBox "Do something"
Unload oFrm
Set oFrm = Nothing
End Sub
UserForm code:
Private Sub cmdCancel_Click()
boFrmCancel = True
Me.Hide
End Sub
Private Sub cmdOK_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
boFrmCancel = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Select Case CloseMode
Case 1
'Do Nothing
Case Else
boFrmCancel = True
End Select
End Sub
(Msg. 2) Posted: Sat Dec 20, 2008 4:52 pm
Post subject: RE: Use "X" on UserForm [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Hi Greg,
Both methods work fine for me. As a variation you could use a public
variable. For example:
-----------------------------------------------------------------------------------------------
Sub TestFrmTest5()
Dim oForm As UserForm
Load FrmTest5
Set oForm = UserForms(UserForms.Count - 1)
FrmTest5.Show
If bCancel Then
Unload oForm
MsgBox "Do nothing"
Else
Unload oForm
MsgBox "Do something"
End If
Set oForm = Nothing
End Sub
-----------------------------------------------------------------------------------------------
In the form:
Option Explicit
Private Sub CmdCancel_Click()
bCancel = True
Me.Hide
End Sub
Private Sub CmdOk_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
bCancel = False
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then bCancel = True
End Sub
-----------------------------------------------------------------------------------------------
Kind regards,
Astrid
"Greg Maxey" wrote:
> I have a simple userform with two buttons "OK" and "Cancel"
>
> If the user clicks OK I want to process additional code in the calling
> procedure. If the user clicks Cancel or "X" I want the calling procedure to
> end.
>
> I know how to intercept the "X" button so that it does nothing and I know
> how to disable the "X" button.
>
> In this case I want the "X" button to mimic the Cancel button. I have
> found to possible solutions. One uses error handling and one uses a public
> variable. I was wondering if there are pitfalls to using either method or
> if a better method exists:
>
> Project code for first form:
>
> Sub ProcessXOnUserFormWithErrHandler()
> Dim oFrm As frmTest3
> Set oFrm = New frmTest3
> oFrm.Show
> On Error GoTo Err_Handler
> If oFrm.Tag = "Canceled" Then
> Unload oFrm
> Set oFrm = Nothing
> Exit Sub
> End If
> MsgBox "Do something"
> Unload oFrm
> Set oFrm = Nothing
> Err_ReEntry:
> Exit Sub
> Err_Handler:
> Debug.Print Err.Number & " "; Err.Description
> Set oFrm = Nothing
> Resume Err_ReEntry
> End Sub
>
> Userform code:
>
> Private Sub cmdCancel_Click()
> Me.Tag = "Canceled"
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> Me.Hide
> End Sub
> Private Sub UserForm_Initialize()
> End Sub
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> Me.Tag = "Canceled"
> End Sub
>
>
> Project code for second form:
>
> Public boFrmCancel as Boolean
>
> Sub ProcessXOnUserFormWithLogic()
> Dim oFrm As frmTest4
> Set oFrm = New frmTest4
> oFrm.Show
> If boFrmCancel Then
> Unload oFrm
> Set oFrm = Nothing
> Exit Sub
> End If
> MsgBox "Do something"
> Unload oFrm
> Set oFrm = Nothing
> End Sub
>
> UserForm code:
>
> Private Sub cmdCancel_Click()
> boFrmCancel = True
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> Me.Hide
> End Sub
> Private Sub UserForm_Initialize()
> boFrmCancel = False
> End Sub
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
> Select Case CloseMode
> Case 1
> 'Do Nothing
> Case Else
> boFrmCancel = True
> End Select
> End Sub
>
>
> Thanks.
>
> --
> Greg Maxey - Word MVP
>
> My web site http://gregmaxey.mvps.org > Word MVP web site http://word.mvps.org >
>
>
>
(Msg. 3) Posted: Sun Dec 21, 2008 8:48 pm
Post subject: Re: Use "X" on UserForm [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
Thanks Astrid.
Astrid wrote:
> Hi Greg,
>
> Both methods work fine for me. As a variation you could use a public
> variable. For example:
>
> -----------------------------------------------------------------------------------------------
> Sub TestFrmTest5()
> Dim oForm As UserForm
>
> Load FrmTest5
> Set oForm = UserForms(UserForms.Count - 1)
> FrmTest5.Show
>
> If bCancel Then
> Unload oForm
> MsgBox "Do nothing"
> Else
> Unload oForm
> MsgBox "Do something"
> End If
>
> Set oForm = Nothing
>
> End Sub
> -----------------------------------------------------------------------------------------------
> In the form:
>
> Option Explicit
> Private Sub CmdCancel_Click()
> bCancel = True
> Me.Hide
> End Sub
>
> Private Sub CmdOk_Click()
> Me.Hide
> End Sub
>
> Private Sub UserForm_Initialize()
> bCancel = False
> End Sub
>
> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
> Integer) If CloseMode = 0 Then bCancel = True
> End Sub
> -----------------------------------------------------------------------------------------------
>
> Kind regards,
> Astrid
>
> "Greg Maxey" wrote:
>
>> I have a simple userform with two buttons "OK" and "Cancel"
>>
>> If the user clicks OK I want to process additional code in the
>> calling procedure. If the user clicks Cancel or "X" I want the
>> calling procedure to end.
>>
>> I know how to intercept the "X" button so that it does nothing and I
>> know how to disable the "X" button.
>>
>> In this case I want the "X" button to mimic the Cancel button. I
>> have found to possible solutions. One uses error handling and one
>> uses a public variable. I was wondering if there are pitfalls to
>> using either method or if a better method exists:
>>
>> Project code for first form:
>>
>> Sub ProcessXOnUserFormWithErrHandler()
>> Dim oFrm As frmTest3
>> Set oFrm = New frmTest3
>> oFrm.Show
>> On Error GoTo Err_Handler
>> If oFrm.Tag = "Canceled" Then
>> Unload oFrm
>> Set oFrm = Nothing
>> Exit Sub
>> End If
>> MsgBox "Do something"
>> Unload oFrm
>> Set oFrm = Nothing
>> Err_ReEntry:
>> Exit Sub
>> Err_Handler:
>> Debug.Print Err.Number & " "; Err.Description
>> Set oFrm = Nothing
>> Resume Err_ReEntry
>> End Sub
>>
>> Userform code:
>>
>> Private Sub cmdCancel_Click()
>> Me.Tag = "Canceled"
>> Me.Hide
>> End Sub
>> Private Sub cmdOK_Click()
>> Me.Hide
>> End Sub
>> Private Sub UserForm_Initialize()
>> End Sub
>> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
>> Integer) Me.Tag = "Canceled"
>> End Sub
>>
>>
>> Project code for second form:
>>
>> Public boFrmCancel as Boolean
>>
>> Sub ProcessXOnUserFormWithLogic()
>> Dim oFrm As frmTest4
>> Set oFrm = New frmTest4
>> oFrm.Show
>> If boFrmCancel Then
>> Unload oFrm
>> Set oFrm = Nothing
>> Exit Sub
>> End If
>> MsgBox "Do something"
>> Unload oFrm
>> Set oFrm = Nothing
>> End Sub
>>
>> UserForm code:
>>
>> Private Sub cmdCancel_Click()
>> boFrmCancel = True
>> Me.Hide
>> End Sub
>> Private Sub cmdOK_Click()
>> Me.Hide
>> End Sub
>> Private Sub UserForm_Initialize()
>> boFrmCancel = False
>> End Sub
>> Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
>> Integer) Select Case CloseMode
>> Case 1
>> 'Do Nothing
>> Case Else
>> boFrmCancel = True
>> End Select
>> End Sub
>>
>>
>> Thanks.
>>
>> --
>> Greg Maxey - Word MVP
>>
>> My web site http://gregmaxey.mvps.org >> Word MVP web site http://word.mvps.org
All times are: Eastern Time (US & Canada) (change)
Page 1 of 1
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum