Link to home
Start Free TrialLog in
Avatar of jasonboetcher
jasonboetcher

asked on

Run time error '364' - Object was unloaded

I have an application that first displays a splash screen.  It then checks the system registry to see if the proper keys can be located.  If they are not located, the user is notified that the registry keys are being added by a message box.  When the user clicks 'OK' the app should then unload so that the next time the app is started the registry keys are there.  Everything works fine until I try to unload the app.  That is when I get the run time error 364.  Why am I getting this error?

Here is my code from my frmMain Load event:


StatusMsg "Loading application..."
  Show
  frmSplash.Show
  DoEvents
 
  Screen.MousePointer = vbDefault

  'Retrieve database path name from system registry
  gstrDatabasePath = GetSetting("Bankruptcy", "FilePaths", "Database", _
    "No Path")

  'Check returned database path name from system registry
  'If it equals 'No Path' then add settings to the registry
  If gstrDatabasePath = "No Path" Then
    Unload frmSplash
    MsgBox "The Bankruptcy Trustee Reporting application was not able to locate the proper entries in the System Registry for the application to function properly." _
      & Chr$(13) & Chr$(13) & "Click 'OK' to allow the application to make the proper entries into the System Registry.", _
      vbCritical, "Initial System Registry Entries Needed"
    SaveSetting "Bankruptcy", "FilePaths", "Database", "C:\Bankruptcy\Bankruptcy.mdb"
    SaveSetting "Bankruptcy", "FilePaths", "Data", "C:\Bankruptcy\Brupfile.fil"
    SaveSetting "Bankruptcy", "FilePaths", "Report", "C:\Bankruptcy\Bruplist.fil"
    SaveSetting "Bankruptcy", "FilePaths", "ExpFmt1", "A:\Rconfile.txt"
    SaveSetting "Bankruptcy", "FilePaths", "ExpFmt2", "A:\Rconfile.txt"
    SaveSetting "Bankruptcy", "FilePaths", "ExpFmt3", "A:\Arpfile.txt"
    SaveSetting "Bankruptcy", "FilePaths", "ExpFmt4", "A:\Recon.txt,A:\Stmt.txt,A:\Trancode.txt"
    SaveSetting "Bankruptcy", "FilePaths", "CrystalReports", "C:\Bankruptcy"
    MsgBox "The proper Registry entries have been made.  Please close and restart the application.", vbInformation, "Registry Update Successful"
    Unload Me
  Else
    'Test database connection
    gcnn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source =" & gstrDatabasePath
    'retrieve Tran Date from BkrpList
    rst.Open "BkrpList", gcnn
    gstrProcessDate = rst("Tran Date")
    rst.Close
    gcnn.Close
    Unload frmSplash
    StatusMsg "Ready"
  End If

Here is the code from my frmSplash:


Private Sub Form_GotFocus()
    Dim intPauseTime As Long
    Dim intStart As Long

    intPauseTime = 2
    intStart = Timer
    Do While Timer < intStart + intPauseTime
      DoEvents
    Loop

    frmMain.Show
End Sub

Private Sub Form_Load()
    Screen.MousePointer = vbHourglass
    lblVersion.Caption = "Version " & App.Major & "." & App.Minor & "." & App.Revision
    lblProductName.Caption = "Bankruptcy Trustee Reporting"
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Set frmSplash = Nothing
End Sub


When I choose 'Debug' when I receive this error, it points to the following line of code in the frmSplash GotFocus event "frmMain.Show".

Any ideas you can give me will be greatly appreciated.  Also, any tips you can give to my coding in general would be a great help as I am new to VB.
Avatar of hes
hes
Flag of United States of America image

Try using this instead in a module:

Public fMainForm As frmMain


Sub Main()
    frmSplash.Show
    frmSplash.Refresh
    Set fMainForm = New frmMain
    Load fMainForm
    Unload frmSplash


    fMainForm.Show
End Sub

And have the startup object as Sub Main
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasonboetcher
jasonboetcher

ASKER

That worked great.  The only thing I had to add was another GetSetting command before I tested the connection to pick up the new registry key.  Thanks!
Tip: avoid coding too much in event procedures, especially Load, Unload and Resize procedures. You also get into trouble coding stuff in _Change and _Click procedures for data-containing controls such as text boxes, combo boxes and so on.

This leads to what I call "tortuous event coding". You'll find that you have to start making exceptions for what triggered the event, because events could happen in a different order depending on whether the user clicked on a control, tabbed into it, the contents was changed by the user typing something or by the program itself... and so on. Makes for nasty spaghetti code...

Often you can achieve the same by approaching the code slightly differently.

In this case you put code in Form_GotFocus and Form_Load procedures. Instead, I took the code out and put them in a Sub Main, stabilising the product.
BTW: Thanks for the points! ;-)
I am getting error :
Runtime error 364 object was unloaded

I have coded Form_Load event and on certain condition I want to UnLoad the current form and setfocus on MDI form's control.
What is the solution for this? or What is alternative for this?

Frontend :Visual Basic 6 EE
Backend : SQL Server

Private Sub Form_Load()
On Error GoTo errHandler: 'Added by Vishwas on 26/10/2009
  Dim rsUnlockRes As New ADODB.Recordset
  Dim STRSQL As String
  'Marked by Vishwas on 26/10/2009''SYS_RESOURCE prob
  STRSQL = "select * from sys_resource_in_process where user_name = '" & sUserName & "'  order by resource_name"
  'Added By Ashwani on 20/08/2006
  If rsUnlockRes.State = adStateOpen Then rsUnlockRes.Close
  rsUnlockRes.Open STRSQL, cn, adOpenForwardOnly, adLockReadOnly
  If Not (rsUnlockRes.EOF Or rsUnlockRes.BOF) Then
      lstUnlockRes.Clear
      rsUnlockRes.MoveFirst
      While Not (rsUnlockRes.EOF Or rsUnlockRes.BOF)
         lstUnlockRes.AddItem (rsUnlockRes.Fields("resource_name").Value)
         rsUnlockRes.MoveNext
      Wend
  End If
  If lstUnlockRes.ListCount = 0 Then
     MsgBox "No Resources are locked.", vbCritical
     'Unload Me 'Marked by Vishwas on 26/10/2009###Here error is
     Unload frmUnlockRes 'Marked by vishwas on 26/10/2009###Here error 
     frmAppMain.ucmnuMenu1.SetFocus 'Commented by Vishwas on 26/10/2009
     Exit Sub###Here errore is
'     'cmdUnlock.Enabled = False
''     cmdExit.SetFocus
  End If
  Exit Sub
errHandler:
If Err.Number <> 0 Then MsgBox "Error : " & CStr(Err.Number) & " : " & Err.Description, vbCritical, Me.Caption
End Sub

Open in new window