• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

Close active form from a function

I have an autokey that opens up a form.  When that form opens I want to close whatever active form is open.  Is there a way I can close an unknown form from a function in a module?

Thanks,
JOe K.
0
ClaudeWalker
Asked:
ClaudeWalker
  • 6
  • 5
1 Solution
 
dannywarehamCommented:
Sure.
Simplest way is to use the lost focus event, IMO.

Call it with this: (untested)

CloseLostForm (me.form)
Public Function CloseLostForm (myForm)
docmd.close acform, myForm
End Function

Open in new window

0
 
ClaudeWalkerAuthor Commented:
How am I supposed to get the form name from the autokey macro?
0
 
dannywarehamCommented:
Autokey macro?
You don't need to.

Go to each form and on teh Lost_focus() event (in VBA code), simply add:

CloseLostForm (me.form)



Then create a module and put in teh function i gave you.

when yuo form loses focus (ie, you click on another form), the old form will close.

You will need some sort of trap incase your user clicks off Access or onto the background.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ClaudeWalkerAuthor Commented:
Thats too risky.  This has to be air tight.  
0
 
ClaudeWalkerAuthor Commented:
Plus the form is in modal and popup so it doesn't lose focus when the other form is opened.
0
 
ClaudeWalkerAuthor Commented:
Thanks for your input though.  I appreciate your time and effort,
0
 
dannywarehamCommented:
Right.
In that case a change of tact.
On the On_open of each form, we close all others except theone you just opened:


Call with:


 CloseForms me.form




Public Function CloseForms(myForm as Form)

dim frm as form  
   for each frm in forms      
     if frm<>myForm then
        docmd.close acform, frm
     end if
   next
End Function
0
 
ClaudeWalkerAuthor Commented:
What if I have 40 forms?
0
 
dannywarehamCommented:
It'll close any that are open.
If they're not open it does nothing to them.

Your question said that you wanted to close the form that you just left.
If that's the case, you would never have 20 forms open, because, as you open the next one, the previous would close...

0
 
ClaudeWalkerAuthor Commented:
I got it!

This works well.  Thanks for your help,

JOe K.

0
 
dannywarehamCommented:
No worries.
Glad to help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now