Results 1 to 8 of 8
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automation error on modeless form (97 2000 2002)

    Hi all,

    I have a strange problem.

    I show a userform using this code:

    Public frmFlexifind As ufFlexiFind

    Sub ShowIt()
    If frmFlexifind Is Nothing Then
    Set frmFlexifind = New ufFlexiFind
    frmFlexifind.FormInitialize
    End If
    If CInt(Left(Application.Version, 2)) = 8 Then
    frmFlexifind.Show
    Else
    ShowModeless
    End If
    End Sub

    Sub ShowModeless()
    frmFlexifind.Show vbModeless
    End Sub

    The close button on the form has this code:

    Private Sub btClose_Click()
    Me.Hide
    End Sub

    And the Queryclose:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode < 2 Then
    Me.Hide
    End If
    End Sub

    When I close the userform using the close button (the X) and then run ShowIt again, I get an "Automation Error" on this line:

    frmFlexifind.FormInitialize

    However, when I change the queryclose routine to:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode < 2 Then
    Me.Hide
    Cancel = True
    End If
    End Sub

    All is well.

    Can anyone explain this please?
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation error on modeless form (97 2000 2002)

    The QueryClose event occurs when you try to close the form. In your case, you don't really want to close it, only to hide it. Therefore, you should cancel the closing of the form. If you don't, the form will close completely. Now the variable frmFlexifind refers to a form that no longer exists. But is has not been set to Nothing, so next time you run ShowIt, the instruction Set frmFlexifind = New ufFlexifind is skipped. Trying to run frmFlexifind.FormInitialize fails because the variable doesn't refer to an existing form.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation error on modeless form (97 2000 2002)

    Crystal-clear Hans, thanks.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Automation error on modeless form (97 2000 2002)

    Hans,
    If the Set frmFlexifind = New ufFlexifind line is skipped then wouldn't the frmFlexifind.FormInitialize also be skipped since it's part of the same If...End If block?
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation error on modeless form (97 2000 2002)

    Hmm, yes. Actually, I get an Automation error on the line where the Show method is invoked. Perhaps Jan Karel can comment on this.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation error on modeless form (97 2000 2002)

    <hr>wouldn't the frmFlexifind.FormInitialize also be skipped since it's part of the same If...End If block?
    <hr>

    True. I copied the code and then left out som parts of it, screwqing up the original order of things...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Automation error on modeless form (97 2000 2002)

    Someone once posted some code to figure out which UserForms remained loaded but hidden. I guess this is a slightly different problem because in addition to the UserForm object, we have a pointer to the UserForm object. Hmmm...

  8. #8
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation error on modeless form (97 2000 2002)

    <hr>Someone once posted some code to figure out which UserForms remained loaded but hidden. <hr>

    That may well have been me.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •