Solved

Close active form from a function

Posted on 2008-10-16
11
529 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

743 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

12 Experts available now in Live!

Get 1:1 Help Now