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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to unload form while in Form_Load procedure 1

Status
Not open for further replies.
Oct 5, 1999
105
GB
Is there any way of cancelling the loading of a form from its Form_Load procedure. Doing so seems to always give an error 364 "Object was unloaded".

Part of my program prints various information as requested.

My problem is that in the Form_Load procedure of Form2 (which does the printing) I am checking various print layout parameters, and if they are not defined, then calling another form (Form3) to define them. If on return from Form3 the parameters are still not defined, because the user has pressed a Cancel button, then I want to exit from Form2 as printing is not possible.

I have tried just using a
Unload Me
but it gives the avove error.

So how should I do this?
 
>I am checking various print layout parameters, and if they are not defined...

Why not do that before Form2 is loaded?
 
Yes that could be possible if there is no other way, but it will mean duplicating code and moving variables as they are all defined at module level within Form2 currently.

Presumably there is a good reason for VB to disallow this sort of thing, it just seemed the logical place to do it.

 
I would personally prefer to do all the conditional stuff in a sub main. Simply change the name of Form_Load in Form2 to some public function name ("PrintOK" or something) that returns a boolean value. Call this proc from Sub Main, and then show Form2 if it returns true and unload it if it returns false. (You'll have loaded the form by calling the public function.)

Of course, I would really migrate all the module level variables to the standard module, and the function too. That way I wouldn't have to unload the form if things didn't go the way I wanted them to, because I wouldn't have loaded it in the first place.

HTH

Bob
 
Along the lines of what BobRodes suggested: Just create a proceedure in that form called something like OpenForm.
Move your code from Form_Load to this proceedure.
Call the proceedure and if every thing is correct, then continue.

Public Function OpenForm() As Boolean
If TestsPassed Then
OpenForm = True
Else
OpenForm = False
End if
End Function


Externally:
If myForm.OpenForm=True then myForm.Show

You could even have a Me.Show at the end of that OpenForm proceedure which is called if all is well, and not worry about even calling myForm.Show externally:

Private mFormWasLoaded as boolean
Public Function OpenForm() As Boolean
Dim TestsPassed As Boolean

If TestsPassed Then
OpenForm = True
Me.Show
Else
OpenForm = False
'Only needed if the form was actually loaded, explicitly, or by referencing an object on the form.
If mFormWasLoaded then Unload Me
End if
End Function

Private Sub Form_Load()
mFormWasLoaded =true
End Sub


Then called Externally:

Dim myForm as Form1
Set myForm = New Form1

myForm.OpenForm


>(You'll have loaded the form by calling the public function.)

No, not really. Normally, only if done explicitly or if an object on the form was referenced




 
Thanks for that SB, that's just the idea I needed.

I hadn't thought of the fact that of course you can effectively call a form without first using a Show.

In fact presumably, I can accomplish the same thing using the Load statement if I just modify the Form_Load procedure of Form2 to set a global variable, e.g. blnCannotPrint, instead of trying to unload itself, and then change the calling sequence to:

Load Form2
If blnCannotPrint then
Msgbox "Cannot print!"
Unload Form2
Else
Form2.Show vbModal
End If

Thanka again
 
>to set a global variable

I wouldn't do that. Instead, use a public variable in the Form2 (Form2.blnCannotPrint)

However, if you went that route, I would consider BobRodes idea of using a public function in Form2 which gets checked prior to loading.
 
I agree with SB, wouldn't use a global variable, and wouldn't use a public variable either. In fact, as I look at it again, you don't even need to use a function, you can use a sub.

Here's a stripped down demo. Create a project with 3 forms. Put a command button on form1, and a check box on form3. Form3 represents the printer function, and the check box represents whether the print function worked ok or not. In Form2, put this code:
Code:
Option Explicit

Public Sub LoadConditional() 'This sub loads Form2 only if the check box in Form3 is checked
If Form3.Check1 <> vbChecked Then
    Unload Me
Else
    Me.Show
End If
End Sub
And, in Form1:
Code:
Option Explicit

Private Sub Command1_Click()
Form2.LoadConditional
End Sub

Private Sub Form_Load()
Form3.Show
End Sub
Run the program. You'll see that if you check the box on Form3, form2 will show, and if you don't it won't.

HTH

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top