Test for Parent or Subform without error trapping

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    Test for Parent or Subform without error trapping

    Is there a property or method that can be used to determine if a form has a parent or not? If I make any reference to frm.Parent it triggers an error if the form does not have a parent.

    I'd like to make my subforms have a different background color than my forms (dynamically with code, not by setting properties). But I prefer to do it without triggering an error.

    Thanks,

    Jim
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Sorry, the only way I've found to do this is to error trap.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32653

      #3
      Sorry Jim. The best I have is to encapsulate it in a procedure :
      Code:
      'OnSubform() returns True if frmVar is open within a subform on another form.
      Public Function OnSubform(frmVar As Form) As Boolean
          OnSubform = True
          On Error Resume Next    'Then statement is executed on error
          If Not TypeOf frmVar.Parent Is Form Then OnSubform = False
      End Function

      Comment

      • jimatqsi
        Moderator Top Contributor
        • Oct 2006
        • 1293

        #4
        Thanks to both of you. I have reluctantly accepted that as the solution, but I remain surprised and disappointed that there is not another way.

        Jim

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          OK,

          This is a very common method to determine if a form is loaded:
          Code:
          If CurrentProject.AllForms("FormName").IsLoaded Then
          	' DO SOMETHING
          End If
          I do know that if the form is a subform, and not loaded as a standalone, and the parent form is opened with the subform loaded then CurrentProject. AllForms("SubFo rmName").IsLoad ed = FALSE.

          Now I use this in my subforms to check to see if the parent is opened and I cancel the open if the parent is NOT opened to keep the subforms from erroring

          SO, if the code, ran as subform on_open, returns false, then we're 95% assured that the form has been opened as a subform and easy to confirm with Neopa's function or any numerous methods.

          HOWEVER,

          if the result is true then we know that, there is at lest one form with that name loaded as a standalone and it could be the form calling the code or one that was already open as standalone. This is the logical rub...
          Last edited by zmbd; May 8 '14, 06:03 PM.

          Comment

          Working...