471,355 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Close form problem

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

Similar topics

2 posts views Thread by chanchito_cojones | last post: by
1 post views Thread by Martin Douglas | last post: by
6 posts views Thread by Ashish | last post: by
7 posts views Thread by Alice | last post: by
19 posts views Thread by Nathan | last post: by
4 posts views Thread by erick-flores | last post: by
4 posts views Thread by Jason Huang | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.