?
Solved

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

Posted on 2003-03-27
3
Medium Priority
?
3,993 Views
Last Modified: 2007-12-19
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
Comment
Question by:rafsalman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 160 total points
ID: 8217890
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
 
LVL 1

Author Comment

by:rafsalman
ID: 8218013
sounds plausible !

Will try it and report back.

Thanks for the quick answer.

Raf
0
 
LVL 1

Author Comment

by:rafsalman
ID: 8218825
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question