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.
jasonboetcherAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
caraf_gConnect With a Mentor Commented:
Add a .bas module to your project and code a Sub Main. Set your project properties - Startup Object is Sub Main.

StatusMsg "Loading application..."
  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
    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.", vbInformation, "Registry Update Successful"
  End If

'You don't need to close and restart!
 
    '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
    StatusMsg "Ready"

    Unload frmSplash

    frmMain.Show


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

Is all you need
0
 
hesCommented:
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
0
 
jasonboetcherAuthor Commented:
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!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
caraf_gCommented:
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.
0
 
caraf_gCommented:
BTW: Thanks for the points! ;-)
0
 
email_rajeevshahCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.