none
Closing a form in the GUI does not remove it from the forms collection in vba.

    Frage

  • Error 2450: access 2010 vba:

    E.g. Form_frmAbteilungen.Requery will create it and put it in the forms collection.

    But trying to reference it with Forms(OpenFormName) generates an Error 2450 because it is not visible

    although the OpenFormName is listed in the Forms collection.

    docmd.open acform, OpenFormName  will make the Form available again.

    This is really basic stuff and yet it doesn't work. Why?

    Regards Richard

    '***** Direct Window Test ******
    ? Form_frmAbteilungen.Visible          'Form is NOT visible but is now in the Forms collection.
    Forms("frmAbteilungen").Visible        'But this will raise an Error 2450
    '***** Direct Window Test ******

    Montag, 6. März 2017 14:08

Alle Antworten

  • Hi,

    As I never experienced anything like you're describing I tried to repro your example in Access 2010, no success. The first line in direct window fails already with error 424: Object required.

    Can you show the declarations and potential variable assignments?

    Rgds - Peter

    Freitag, 31. März 2017 12:07
    Moderator
  • The first line works only if the form has a module. "Forms_frmAbteilungen" really is a module object.

    I can see that in fact, after the first line, frmAbteilungen is in the Forms collection, but the form is not open. So this is a strange situation. My recommendation is: Don't do this :-)

    First open the form with DoCmd.OpenForm, and only after this, use references to the forms module.

    Matthias Kläy, Kläy Computing AG

    Freitag, 31. März 2017 13:39
  • Hi,

    The first line works only if the form has a module.

    Beginners mistake, thanks for correction. ;-)

    I can see that in fact, after the first line, frmAbteilungen is in the Forms collection, but the form is not open. So this is a strange situation. My recommendation is: Don't do this

    Agreed, don't do it. The explanation for the situation is probably simple:

    It is an implicit object instance, triggered by the check. We know it from object declarations with keyword New. Example:

    Dim obj As New Excel.Application
    '... do something
    obj.Quit
    Set obj = Nothing

    If obj Is Nothing Then 'Now check the task manager for Excel applications ;-)

    While form and code-behind-form are 2 different objects and only the CBF got loaded it is fairly obvious that the form itself has not been loaded, thus won't be accessible prior to "OpenForm".

    And no, ? Forms("formname").Visible or ? AllForms("formname").Visible won't perform implicit instanciation. ;-)

    Rgds - Peter

    Samstag, 1. April 2017 10:14
    Moderator
  • I made some more tests:

    Dim frm As Form
    
    Debug.Print "Form visible = " & Form_frmAbteilungen.Visible
    ' prints False
    Debug.Print "IsLoaded = " & CurrentProject.AllForms("frmAbteilungen").IsLoaded
    ' prints True
    
    For Each frm In Forms
        Debug.Print frm.Name
    Next
    ' prints the name of the form, frmAbteilungen
    
    Set frm = Forms("frmAbteilungen")
    ' Error 2450, Microsoft Access cannot find the referenced form 'frmAbteilungen'
    

    Interestingly, when the form module is referenced the first time (Form_frmAbteilungen.Visible), then the code in the Form_Open and Form_Load events is run!

    But the last line shows that there is still no valid Form object

    Matthias Kläy, Kläy Computing AG

    Sonntag, 2. April 2017 14:05
  • Hi, I now use the following code to prevent the errors.

    Public Function IsFormOpen(FormName As String) As Boolean
    '****************************************************************************************
    'Zweck:    Checks if a Form is open
    'N.B.      Just accessing a Form object will create it. So, you must use the forms collection
    'Geändert: Richard Bates 07.03.2017
    '****************************************************************************************
    On Error GoTo ErrorHandler
    Dim ThisForm As Form

      IsFormOpen = False
     
      For Each ThisForm In Forms
        If TestValidForm(ThisForm.Name) Then
          If ThisForm.Name = FormName Then
            IsFormOpen = True
            Exit Function
          End If
        End If
      Next
     
    ExitPoint:
      Exit Function

    ErrorHandler:
      Select Case Err
        Case Else:  MsgBox "Error: " & Err & ": " & Err.Description, vbCritical, "IsFormOpen"
      End Select
      Resume ExitPoint
    End Function

    Public Static Function TestValidForm(FormName As String) As Boolean
    '****************************************************************************************
    'Zweck:     Tests if a form is valid
    'Geändert:  Richard Bates 07.03.2017
    '****************************************************************************************
    On Error GoTo ErrorHandler
     
      TestValidForm = False
     
      'This will raise an error when the Form is not properly loaded (e.g. Form_frmAbteilungen.Visble)
      If Forms(FormName).Visible Then
        TestValidForm = True
      End If

    ExitPoint:
      Exit Function

    ErrorHandler:
      Select Case Err
        Case 2450:  'Form is not properly loaded (e.g. Form_frmAbteilungen.Visble)
                    On Error Resume Next
                    DoCmd.Close acForm, FormName, acSaveNo    'Remove the invalid Form.
                    GoTo ExitPoint
        Case Else:  MsgBox "Error: " & Err & ": " & Err.Description, vbCritical, "TestValidForm"
      End Select
      Resume ExitPoint
    End Function

    Regard Richard

    Montag, 3. April 2017 20:38
  • Just noticed that I need to add OR Not visible in the TestValidForm function!
    Montag, 3. April 2017 20:52
  • To test if a form is open, I propose the following function:

    Public Function IsFormOpen(FormName As String) As Boolean
    Dim frm As Form
    On Error Resume Next
    Set frm = Forms(FormName)
    IsFormOpen = Not (frm Is Nothing)
    End Function

    or even shorter

    If CurrentProject.AllForms("frmAbteilungen").IsLoaded Then
       ' Form is open
    End If

    Matthias Kläy, Kläy Computing AG


    • Bearbeitet mklaey Dienstag, 4. April 2017 12:28
    Dienstag, 4. April 2017 10:25