Need to close pop up form and stop code

Frank123

Registered User.
Local time
Today, 10:47
Joined
Jun 29, 2016
Messages
31
Hi, I'm a novice with VBA. This seems to be simple, but I cannot find an answer. I have a subform with a button that runs code after the "on click" event. While the code is running, a pop up form is triggered that pauses the code until the user makes a selection on the pop up form. Once the selection is made, the code than continues to run to its end. What I need is a "close" or "cancel" button on the pop up form that when selected completely exits the opertion without continuing to run the remaining part of the code. I tried:
  • The "X" button at the top of the pop up form
  • DoCmd.Close
  • DoCmd.Close acForm, "FormName", acSaveNo
  • DoCmd.Quit
However, none of these completely exited out of the operation. The code continued to run after the line that opened the pop up form. Any help would be greatly appreciated.
 
The way I do this is don't let the user actually close the popup, but just hide it instead. This lets the code keep running, back to the calling object, which is then able to read data off that hidden form. So say I have this code on the popup...

Code:
private m_cancelled as boolean[COLOR="Green"] 'defaults to false[/COLOR]

public property get Cancelled as boolean
   Cancelled = m_cancelled
end property

private sub cmdCancel_Click()
   m_cancelled = true  [COLOR="Green"]'value exposed by the public property[/COLOR]
   me.visible = false[COLOR="Green"]  'hides the popup, and code execution continues[/COLOR]
end sub

Now, when code returns to the calling object, you can read the custom Cancelled property of the popup, and deal with it...

Code:
private sub cmdOpenPopup_Click
   const fn as string = "mypop"
   dim cancelled as boolean

   docmd.openform fn, , , , , acDialog [COLOR="Green"]'execution pauses while popup is open[/COLOR]
   cancelled = forms(fn).cancelled     [COLOR="Green"]'retain the form's cancelled property[/COLOR]
   docmd.close acform, fn            [COLOR="Green"]  'close the form - important[/COLOR]
   if cancelled then
[COLOR="Green"]      'handle the cancelled case
[/COLOR]   else
[COLOR="Green"]      'handle the uncancelled case[/COLOR]
   end if
end sub
See how that all works? So don't let the user close the popup. Set the CloseButton property to false, and then in response to user activity on that form, you only hide it, leaving all its data available to be read by calling code.

hth
Mark
 
Thanks Mark for your fast reply. I never thought of this. I'll give it a try.
 
Yeah, you bet. It's a good pattern for a wizard too, since the calling code can read the data off the form.
 

Users who are viewing this thread

Back
Top Bottom