?
Solved

Close active form from a function

Posted on 2008-10-16
11
Medium Priority
?
540 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
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 2000 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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