Solved

Close active form from a function

Posted on 2008-10-16
11
531 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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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