• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4140
  • Last Modified:

VB6 - Error 364: Object was unloaded in...

Hi Experts


I have been staring at this code for a while now and I can't figure out what I'm doing wrong.  I include the key parts of the code below (minus comments).

Basically if any forms are open apart from the two required, the code needs to exit and clear frmUpdateNI.

when the "Unload Me" is called from frmUpdateNI.Form_load it calls Form_unload (which works fine) but then on returning to Form_load and doing the exit sub, it generates the error.

The error is not generated in any other way.



Here's the code - here's hoping there some bright spark out there! (well brighter than me and my 4 months of VB coding experience)


***************************************
mdiMain
***************************************

Private Sub UpdateNITable()
'***************************************************************************
'Purpose:     Displays the Update NI form to enable updating of the NI table.
'Parameters:  None.
'Returns:     None.
'***************************************************************************
On Error GoTo ErrorHandler

Dim conUpdate As ADODB.Connection
Dim rs As ADODB.Recordset
Dim blnUpdateExists As Boolean
Dim strSQL As String

  ' Check if update database is present
  blnUpdateExists = OpenAccessDatabase(conUpdate, UPDATE_NI_DSN, NI_UPDATE_DATABASE)

  If blnUpdateExists Then
 
    strSQL = "SELECT * FROM national_insurance"
   
    Set rs = New ADODB.Recordset
    rs.Open strSQL, conUpdate

    ' Invalid NI data in the database
    If rs.EOF Then
        Err.Raise INVALID_NI_DATA
    End If
   
    rs.Close
    Set rs = Nothing
    conUpdate.Close
    Set conUpdate = Nothing
     
    Load frmUpdateNI
   
     
  Else
    ' Raf - database failed to open
    ' Remove UpdateNI DSN
    CreateAccessDSN UPDATE_NI_DSN, mstrDatabasePath & NI_UPDATE_DATABASE, ODBC_REMOVE_DSN
 
    Err.Raise NI_DATABASE_NOT_PRESENT
     
  End If

Procedure_Exit:
  Exit Sub

ErrorHandler:
  Select Case Err.Number

    Case NI_DATABASE_NOT_PRESENT
      If IN_DEVELOPMENT Then
        GlobalErr Err.Number, DEVELOPMENT_DIRECTORY, "mdiMain - UpdateNITable"
      Else
        GlobalErr Err.Number, CurDir & "\Databases\", "mdiMain - UpdateNITable"
      End If
      Resume Procedure_Exit
      Resume
               
    Case INVALID_NI_DATA
      GlobalErr Err.Number, Err.Description, "mdiMain - UpdateNITable"
      Resume Procedure_Exit
      Resume

    Case Else
      GlobalErr Err.Number, Err.Description, "mdiMain - UpdateNITable"
      Resume Procedure_Exit
      Resume

  End Select

End Sub


******************************************************************
frmUpdateNI

******************************************************************

Private Sub Form_Load()
'***************************************************************************
'Purpose:     Form load event.  Executed when the form is loaded.
'             Sets the form size and populates the form.
'Parameters:  None.
'Returns:     None.
'***************************************************************************
On Error GoTo ErrorHandler
Dim frm As Form
Dim blnChildFormIsOpen

  mdiMain.mnuUpdateNI.Enabled = False
  blnNIDatabaseOpen = False
  blnChildFormIsOpen = False
 
  Me.Height = 4575
  Me.Width = 9795
  Me.Top = (mdiMain.ScaleHeight - Me.Height) / 2
  Me.Left = (mdiMain.ScaleWidth - Me.Width) / 2

 
  ' Check all child forms are closed
 
  For Each frm In Forms
    If (frm.Name <> mdiMain.Name) And (frm.Name <> frmUpdateNI.Name) Then
     
       blnChildFormIsOpen = True

    End If
  Next frm
 
       
  If blnChildFormIsOpen Then
 
    MsgBox "Application Has Open Forms." & vbCrLf & vbCrLf & _
           "The Update NI routine cannot be implemented whilst the application has forms open." _
           & vbCrLf & vbCrLf & "Close any open forms before attempting to update NI.", vbInformation, "Update NI"
         
      Unload Me
  Else
      PopulateForm
  End If


Procedure_Exit:
  Exit Sub

ErrorHandler:
  Select Case Err.Number

    Case Else
      GlobalErr Err.Number, Err.Description, "frmUpdateNI - Form_Load"
      Resume Procedure_Exit
      Resume

  End Select

End Sub


*********************************************

Private Sub Form_Unload(Cancel As Integer)
'***************************************************************************
'Purpose:     Form Unload event.  Executed when the form is unloaded.
'             Closes the recordset and connection objects.
'Parameters:  Cancel - Set to non zero to cancel the unload event.
'Returns:     None.
'***************************************************************************
On Error GoTo ErrorHandler

  mdiMain.mnuUpdateNI.Enabled = True

    If blnNIDatabaseOpen Then
       conUpdate.Close
       Set conUpdate = Nothing
       ' Remove UpdateNI DSN
       CreateAccessDSN UPDATE_NI_DSN, mstrDatabasePath & NI_UPDATE_DATABASE, ODBC_REMOVE_DSN
  End If
 
  Set frmUpdateNI = Nothing

Procedure_Exit:
  Exit Sub

ErrorHandler:
  Select Case Err.Number

    Case Else
      GlobalErr Err.Number, Err.Description, "frmUpdateNI - Form_Unload"
      Resume Procedure_Exit
      Resume

  End Select

End Sub



Anything stand out as blatantly wrong?

Raf
0
rafsalman
Asked:
rafsalman
  • 2
1 Solution
 
appariCommented:
you cannot use unload me in form load event.  change your code to form activate and do it something like this.

Dim blnUnLoadMe as boolean

sub form_load()

if somecondition then
   blnUnLoadMe=true
else
   blnUnLoadMe=false
   'do otherthings you want to do
endif
end sub

sub form_activate()
    if blnUnLoadMe=true then
        Unload me
    endif
end sub
0
 
rafsalmanAuthor Commented:
sounds plausible !

Will try it and report back.

Thanks for the quick answer.

Raf
0
 
rafsalmanAuthor Commented:
ok couldn't get the activate event to work out right (never triggered) but you were right - it was the unload me in the load event that did it.

In the end i used this in mdiMain - UpdateNITable, just after the Load frmUpdateNI

 If frmUpdateNI.blnUnloadMe Then
        Unload frmUpdateNI
 End If



thanks appari.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now