PDA

View Full Version : Disable 'On Load' Event



CreganTur
05-30-2008, 12:11 PM
I've got a simple macro that only uses 2 forms. There is an On Load Event for both forms.

Here's my question:

The user can move from Form2 to Form1 only by clicking a Close button. This button closes Form2 and opens Form1. I want to disable form1's On Load Event only when moving from Form2 to Form1(when the Close button is clicked). Is this possible?

Oorang
05-30-2008, 01:27 PM
The simplest way I know to do that is to conditionally execute the procedure in the On Load event based on an open argument.

Option Explicit

Private m_strOpenArgs As String

Private Sub Form_Open(Cancel As Integer)
m_strOpenArgs = Nz(Me.OpenArgs, vbNullString)
End Sub

Private Sub Form_Load()
If m_strOpenArgs = "DoOpen" Then
'Do your thing here.
End If
End Sub

DarkSprout
06-02-2008, 07:14 AM
Or Use a Global-Variable

'// in a Module
Public gvBoolOpenForms As Boolean

Private Sub Form_Load()
If gvBoolOpenForms = True Then
'Do your thing here.
End If
End Sub

Oorang
06-02-2008, 07:51 AM
Hi Dark Sprout,
A global variable will work, but it is considered bad practice. You should never give a variable more scope than is necessary for it to perform it's function. At minimum it should be public not global. But really if you have an interface to pass locals it should be used, otherwise you then have to maintain methods to clear you public/global it at the right time as it will not auto clear when the form closes. In which case you might as well build a custom property or class, at which point it should occur to you there is already one built :)

Some people will tell you to "never use Globals" but that isn't strictly true. You should give preference to smallest scope. So if a local will do use a local. If you have to expose part of a class use a friend property not a public variable. If you have a property in a module then use a public property first. The only time you should ever go global is if you want to use a var to communicate between projects. And for an interface you are better giving preference to properties then public vars as you can then tinker with the internals as needed without breaking the interface, as well as giving yourself access to the Friend scope.

As a side note, unless you wanted to use Form_Load for a specific reason, you can put this whole thing in just Form_Open:

Option Explicit

Private Sub Form_Open(Cancel As Integer)
If Nz(Me.OpenArgs, vbNullString) = "DoOpen" Then
'Do your thing here.
End If
End Sub

DarkSprout
06-05-2008, 06:26 AM
I've slapped my wrists!

OTWarrior
06-05-2008, 07:12 AM
assuming your navigation system will mean you initialy go from form1 to form2, and in your case go back to form1, couldn't you just make form1 not visible when loading form2.

Then when you close form2, form1 is made visible again (and you could even refresh the form is it needed to update the forms data) and wouldn't fire the load event as it isn't being loaded again?

....Or do you HAVE to open and close each form?