Solved

Close active form from a function

Posted on 2008-10-16
11
530 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
 

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
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.

 

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 When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now