Solved

Close active form from a function

Posted on 2008-10-16
11
533 Views
Last Modified: 2013-11-28
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
Comment
Question by:ClaudeWalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 26

Expert Comment

by:dannywareham
ID: 22736431
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
 

Author Comment

by:ClaudeWalker
ID: 22736457
How am I supposed to get the form name from the autokey macro?
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 22736471
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
Technology Partners: 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!

 

Author Comment

by:ClaudeWalker
ID: 22736502
Thats too risky.  This has to be air tight.  
0
 

Author Comment

by:ClaudeWalker
ID: 22736520
Plus the form is in modal and popup so it doesn't lose focus when the other form is opened.
0
 

Author Comment

by:ClaudeWalker
ID: 22736522
Thanks for your input though.  I appreciate your time and effort,
0
 
LVL 26

Accepted Solution

by:
dannywareham earned 500 total points
ID: 22736533
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
 

Author Comment

by:ClaudeWalker
ID: 22736606
What if I have 40 forms?
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 22736624
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
 

Author Comment

by:ClaudeWalker
ID: 22736648
I got it!

This works well.  Thanks for your help,

JOe K.

0
 
LVL 26

Expert Comment

by:dannywareham
ID: 22736660
No worries.
Glad to help
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question