Solved

Run time error '364' - Object was unloaded

Posted on 2000-02-24
6
5,926 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now