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

Exit program in Form_Load() Sub

Status
Not open for further replies.

Tomeczek

Programmer
Oct 25, 2012
40
US
Need haelp.
In my Form_load() subroutine I check for certain things and want to start the GUI part (the form) only if the conditions met.
If the condition is not met, I issue the MsgBox and try to exit, something like this:
Code:
If <condition> then
   MsgBox "..."
   Unload Me
End If

This doesn't work. After "Unload Me", the Form_Load() starts again, and again, and...

Any suggestions? Thanks in advance!
 
Add a standart Module to your Project, create a [tt]Sub Main[/tt] in the module:

Code:
Option Explicit

Sub Main()

If <condition> then
   MsgBox "..."
Else
   frmMyFirstForm.Show
End If 

End Sub

Go to Project - Project Properties...
General tab - Startup Object drop down combo and select Sub Main

Powodzenia :)

Have fun.

---- Andy
 
Thanks Andrzej!
This is some idea! Will try it.
 
>This is some idea!

Frankly, it is the best practice way to start a VB project. The ability to directly start up a VB project from a form is a hangover from the earliest versions of VB (when it was essentially a simple form designer), and Microsoft should probably have made Sub Main the default startup object rather than Form1 by the time we reached VB6
 
As far as I know this works in Form_Load without incident:
Code:
If <condition> then
   MsgBox "..."
   Unload Me
   Exit Sub
End If

I'm guessing that the rest of that procedure was touching controls on the Form or something, triggering reload.

Have you tried it?
 
If the form is reloading it is probably because it is being unwittingly referred to somewhere else in the program.
Even checking if it is visible or it's size will Load the form again.
You don't really ever have to use form.load or form.show unless you only want it to do these actions and nothing else. (Except it may be good programming practice for clarity of programming flow)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top