Close form problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jpr

    #1

    Close form problem

    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 "lstpreintervie w" on a form named eforms.

    Public Function Openmyform()
    If IsNull(Forms!ef orms!lstPreInte rview) 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!ef orms!lstPreInte rview) 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.

  • Phil Stanton

    #2
    Re: Close form problem

    option compare database
    option explicit

    Function CloseAllForms()

    Dim i As Integer
    Dim Frm As Form

    For i = 0 To (Application.Cu rrentProject.Al lForms.Count - 1)
    If IsLoaded(Applic ation.CurrentPr oject.AllForms( i).Name) Then
    Set Frm = Forms(Applicati on.CurrentProje ct.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 conObjStateClos ed = 0
    Const conDesignView = 0

    If SysCmd(acSysCmd GetObjectState, acForm, strFormName) <>
    conObjStateClos ed Then
    If Forms(strFormNa me).CurrentView <conDesignVie w 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" <jprma@tin.itwr ote in message
    news:1171370191 .682946.176990@ j27g2000cwj.goo glegroups.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 "lstpreintervie w" on a form named eforms.
    >
    Public Function Openmyform()
    If IsNull(Forms!ef orms!lstPreInte rview) 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!ef orms!lstPreInte rview) 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.
    >

    Comment

    • storrboy

      #3
      Re: Close form problem

      On Feb 13, 7:36 am, "jpr" <j...@tin.itwro te:
      >
      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("eform s").lstPreInter view,"")
      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(frmNa me)
      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.

      Comment

      Working...