Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form_Load executes again after Form_Unload completes

Status
Not open for further replies.

sgursahaney

Programmer
Jun 11, 2001
57
US
I have a problem with my VB app where my main form attempts to re-execute Form_Load while I am executing Form_Unload. During my load procedure, I have a dialog box that appears that request the user username and password. This dialog box seems to re-appear when I am shutting down my application. It seems as if the Form_load is being triggered automatically during shutdown. Has anybody seen this strange behavior before?

Suresh
 
Lets see your code in your unload. And the code around your unload command to unload that form.
 
Break the code execution and step through.. there shud be one line of code referencing the same form, which is causing the problem..

also try
set frmName = Nothing

in the Form Unload
 
can you show the code in the form_unload event?
 
How do you declare your forms?
i.e.
Code:
dim frm as new Form1
frm.Caption = "My Form Caption"
or
Code:
dim frm as Form1
.
.
.
set frm = new Form1
frm.Caption = "My Form Caption"
or
Code:
Form1.Caption = "My Form Caption"

These examples have subtle differing effects of object lifetime (instancing & destroying)....

In common with SemperFiDownUnda, vbSun and teresachristy , I would like to see some code please especially the unload events and the form declarations!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Here's my code. Note that I added the l_shutdown flag as a workaround to handle this program. I also have a single line in the Form_Paint routine that may be contributing to this problem. I have provided it at the end.

Private Sub Form_Load()
Dim rc As Long
Dim IniFileName As String

On Error GoTo LoadErr

'If we are in shutdown mode, just exit
If l_shutdown = True Then Exit Sub

'Get Last window position from INI
objFrmSetup.GetSettings Me

'Create INI filename if INI not passed in as command line parameter
If FileSize(Command()) > 0 Then
IniFileName = Command()
Else
IniFileName = App.Path & "\" & App.EXEName & ".ini"
End If

'Retrieve configuration values from INI file
Call ReadINI(IniFileName)

'Load wrap code combo box from INI file
Call LoadWrapCodes(IniFileName)

'Enable the CC timer to connect to CallCenter and begin receiving events
Call CCInit
tmrCC.Enabled = True

'Show the login window
frmLogin.Show 1

'If user decided cancelled out of Login form, then immediately quit
If g_LoginSucceeded = False Then
Unload Me
Exit Sub
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo UnloadErr

'Set Shutdown flag
l_shutdown = True

'Shutdown Internet Explorer Window
Call TermWeb

'Delete the agent monitor
Call CCDestroyAgent

'Stop the event timer and disconnect from CallCenter
tmrCC.Enabled = False
Call CCDisconnect

'Save position if requested
If mnuSettings.Checked = True Then objFrmSetup.SaveSettings
UnloadErr:
End Sub

Private Sub Form_Paint()

'Align Statusbar with bottom of window
Me.StatusBar1.Align = vbAlignBottom

End Sub
 
Your problem is almost certainly with the line
Code:
If mnuSettings.Checked = True Then objFrmSetup.SaveSettings

I guess that the objFrmSetup has been unloaded from memory. However because you are referencing the objFrmSetup at this point, it is being re-loaded (hence the form laod event being fired)

This is to do with the way that you are instancing forms. Personally, I think it is best to use the following sort of code..
Code:
dim frm as objFrmSetup
.
.
.
'Code  as  needed
.
.
.
Set frm = new objFrmSetup  'Create new instance of form HERE
'do work with form
.
.
.
'now finished with form
unload frm
Set frm = nothing
'any reference to frm after here will cause a RT error
'rather than a subtler logic error as you have!!

Hope that shine some light on your problem

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Actually, the objFrmSetup is a class that I use to retrieve the last position and size of the form from the INI file. During unload, if the mnuSettings option is selected, the object is used to save the current size and position of the main form. As you surmised, it may be causing the main form to reload when I invoke the SaveSettings Method from the object on the Form_Unload. Maybe I should move that code to the Query_Unload routine instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top