A simple one for the logic gurus...

Fornatian

Dim Person
Local time
Today, 15:57
Joined
Sep 1, 2000
Messages
1,396
For simplicity I have five forms named Form1,Form2 etc..each representing data related to a central record

What I have is a toolbar where users can click to open the various forms.

What I want to do is:
When a user opens Form1 close forms 2,3,4 and 5
When a user opens Form2 close forms 1,3,4 and 5

I can do this using form code but it seems a bit inefficient to copy the code making slight variations to which forms to close.

In english what I want is:

Close Form1,Form2,Form3,Form4,Form5 unless they are the active form>
I know I need a function and I know I need to pass in the activeform name for exclusion purposes but can't quite sort the logic.

Over to you..

Ian
 
Apparently, I am a logic guru!!!
Sussed it myself.
Thanks to all who read.
The answer was(for those interested):

'Closes all the chosen forms except the one with the focus
Dim strMe As String
strMe = CodeContextObject.Name
If strMe <> "Previous Site Investigation" Then
DoCmd.Close acForm, "Previous Site Investigation", acSaveYes
End If
If strMe <> "Historical Archive Information" Then
DoCmd.Close acForm, "Historical Archive Information", acSaveYes
End If
If strMe <> "ProjectDiary" Then
DoCmd.Close acForm, "ProjectDiary", acSaveYes
End If
If strMe <> "Maps Information" Then
DoCmd.Close acForm, "Maps Information", acSaveYes
End If
If strMe <> "Soil Main Form" Then
DoCmd.Close acForm, "Soil Main Form", acSaveYes
End If
If strMe <> "StatAuthQry" Then
DoCmd.Close acForm, "StatAuthQry", acSaveYes
End If
If strMe <> "Miscellaneous Activities Form" Then
DoCmd.Close acForm, "Miscellaneous Activities Form", acSaveYes
End If
If strMe <> "frmBuildAReport" Then
DoCmd.Close acForm, "frmBuildAReport", acSaveYes
End If
If strMe <> "Mining Records" Then
DoCmd.Close acForm, "Mining Records", acSaveYes
End If

O Happy Day!
 
How about this, You make use of the tag field of the form. For each form that you are considering in your solution, put a value of True in the tag field.

Then you list out each form object and if the tag propoerty is true you test if the name matches your active form name. If it does not you close the form.

The same principle just lest code and more efficient if you intend to add more forms to your list later on.

Dim frmObject as Form
Dim dbs as Database
Dim strMe as String ' your active form name

Set dbs = currentdb
for each frmObject in dbs.forms
if frmObject.tag = true then
if frmObject.name <> strMe then
docmd.close acform, frmobject.name, acSaveYes
end if
end if
next frmObject

dbs.close
set dbs = nothing

You can also add code to test whether the form is actually open before trying to close it to make your code more robust.

In programming iot is always best to hardcode as little information in your procedures as possible. It makes your code more versatile.

ntp
 
ntp,

thanks for the tip, i knew there was a way with the looping mechanism, just wouldn't come to me - you know how it is!

thanks for the good advice.

ian
 

Users who are viewing this thread

Back
Top Bottom