Solved

Run time error '364' - Object was unloaded

Posted on 2000-02-24
6
6,071 Views
Last Modified: 2010-07-27
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.
0
Comment
Question by:jasonboetcher
6 Comments
 
LVL 20

Expert Comment

by:hes
ID: 2555736
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
 
LVL 10

Accepted Solution

by:
caraf_g earned 25 total points
ID: 2555743
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
 

Author Comment

by:jasonboetcher
ID: 2555840
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:caraf_g
ID: 2557061
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2557062
BTW: Thanks for the points! ;-)
0
 

Expert Comment

by:email_rajeevshah
ID: 25662265
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

761 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