Form Open Passing the Open Args Parameter

The Form Open event property triggers whenever a form is opened. Each form is only opened once, so this event should theoretically trigger only one time. ("Theoretically" is used here because there are cases when events should fire only once, but they end up firing more than once. Don't take this warning too seriously, but if you suddenly have trouble with a procedure, trace it and see if this is happening.)

In the OpenForm method of the DoCmd object, there is a parameter called OpenArgs that will allow you to pass a string to the called Form. There is no structure to the parameter value, other than it needs to be a string.

For example, the following will open the form called Orders and pass it the string value.

Docmd.OpenForm "Orders", OpenArgs:="CallingProc | MainMenu"

When the form opens, you can examine the OpenArgs property using the Me object. See the earlier section You Talking to Me? for more information. The following example will examine the OpenArgs property of the form, and react to it.

Private Sub Form_Open(Cancel As Integer) If Len( Me.OpenArgs) > 0 Then

'Do Something Else

Cancel = True End If End Sub

In the previous code example, the application checks to see if a value was provided for the OpenArgs property. This is accomplished by checking the length of the value of the property. The Len() function will return the length of any string.

So, if the length of the OpenArgs property is more than 0, then a value has been supplied. If this is true then some processing will occur, otherwise the Cancel variable will be set to True.

Previously during the discussion on properties, it was mentioned that some event procedures could be cancelled. The Open event procedure is one of them. When the Cancel value is set to True, the event property will not complete the operation. In this example, the Open property will not complete, meaning that the Form will not finish opening. So, if the OpenArgs value is not provided, then the form will not open. This is a sneaky way of preventing users from opening a form on their own from the Database window.

Was this article helpful?

0 0

Post a comment