By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,483 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,483 IT Pros & Developers. It's quick & easy.

Close form problem

P: n/a
jpr
I am using moduled to open forms. I run them from a menu that I have
created on the toolbar. The source of the forms are queries all with
record source to "lstpreinterview" on a form named eforms.

Public Function Openmyform()
If IsNull(Forms!eforms!lstPreInterview) Then
MsgBox "Sorry. You need to select a record!"
Exit Function

Else
'If the record has been selected, well then it can open the macro
Openemyform
If Not IsNull(Forms!eforms!lstPreInterview) Then
DoCmd.OpenForm ("Myform")
End If
End If

End Function

I would like to be able to scroll from one form to the other so that
users just select the required form and can open it. What happens, is
that when a form is opened, the active one should close or I can have
up to 20 forms opened at the same time. Is there a way I can do this
withouth having to provide the form name? And where should I place the
code?
Thanks.

Feb 13 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
option compare database
option explicit

Function CloseAllForms()

Dim i As Integer
Dim Frm As Form

For i = 0 To (Application.CurrentProject.AllForms.Count - 1)
If IsLoaded(Application.CurrentProject.AllForms(i).Na me) Then
Set Frm = Forms(Application.CurrentProject.AllForms(i).Name)
DoCmd.Close acForm, Frm.Name
End If
Next i

End Function

Function IsLoaded(ByVal strFormName As String) As Integer
' Returns True if the specified form is open in Form view or Datasheet
view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <conDesignView Then
IsLoaded = True
End If
End If

End Function

Isloaded is a useful function to open supporting forms from a major form,
for example if you have a telephone directory, you might have a table (and
form) of local towns. You can check if it is already open.

Run the CloseAllForms Function befor opening the next form you want to open

HTH

Phil
"jpr" <jp***@tin.itwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>I am using moduled to open forms. I run them from a menu that I have
created on the toolbar. The source of the forms are queries all with
record source to "lstpreinterview" on a form named eforms.

Public Function Openmyform()
If IsNull(Forms!eforms!lstPreInterview) Then
MsgBox "Sorry. You need to select a record!"
Exit Function

Else
'If the record has been selected, well then it can open the macro
Openemyform
If Not IsNull(Forms!eforms!lstPreInterview) Then
DoCmd.OpenForm ("Myform")
End If
End If

End Function

I would like to be able to scroll from one form to the other so that
users just select the required form and can open it. What happens, is
that when a form is opened, the active one should close or I can have
up to 20 forms opened at the same time. Is there a way I can do this
withouth having to provide the form name? And where should I place the
code?
Thanks.

Feb 14 '07 #2

P: n/a
On Feb 13, 7:36 am, "jpr" <j...@tin.itwrote:
>
I would like to be able to scroll from one form to the other so that
users just select the required form and can open it. What happens, is
that when a form is opened, the active one should close or I can have
up to 20 forms opened at the same time. Is there a way I can do this
withouth having to provide the form name? And where should I place the
code?
Thanks.

Let's see if I understand this..

You have a listbox on a form called lstPreInterview?
And the value selected in it is the name of a form you want to open?
If so, what will happen if someone selects a form that is already
open? Does it just receive focus and move to the foreground?

If this is correct I would write the following careful of line wraps..
Provide additional error trapping as you see fit.

Public Function Openmyform()
Dim frmName As String
Dim frm As Form

'If list is null or empty stop running
frmName= Nz(Forms("eforms").lstPreInterview,"")
If frmName="" then
MsgBox "Sorry. You need to select a record!"
Exit Function
End If

'See if form is already open
'Set focus if it is, open otherwise
On Error Resume Next
Set frm=Forms(frmName)
If Err=0 then
frm.SetFocus
Exit Function
End If

DoCmd.OpenForm frmName
Set frm=Nothing

End Function
I would like to be able to scroll from one form to the other so that
users just select the required form and can open it. What happens, is
that when a form is opened, the active one should close or I can have
up to 20 forms opened at the same time. Is there a way I can do this
withouth having to provide the form name? And where should I place the
code?
Thanks.
As to your last comments, I believe you can have as many forms on
screen as memory allows, although people might find it anoying having
to close a multitude. I would think that closing a form just because
you open a new one in code could be a problem. This introduces the
likelyhood of data corruption (users not finished what they are doing
in it) and events on that form preventing it from being closed -
thereby making new problems in keeping track of what's still open.
And no you can't open a form without knowing it's name.

Feb 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.