Open Args

What if you want to check for conditions or set certain properties when a form is opening? You can use the OpenArgs property of the form to pass parameters to the form when you open it. The OpenForm method's seventh (last) parameter supplies the value for OpenArgs, as shown in the following statement:

DoCmd.OpenForm "FormName", , , , , , "OpenArgs value"

In this case, the value of the OpenArgs property would be "OpenArgs value".

Because the Open event triggers whenever a form opens, you can include the OpenArgs parameter on the OpenForm method to provide information that can be used even before the user sees the form.

An excellent application of this is to ensure that the form is opened by code and only under specific conditions. For example, you may have a form that should be opened only by certain users. Because users can open the form from the Navigation pane, it's possible for any user to open a form — unless you have implemented programmatic restriction. One approach is to use the following code in the form's OnOpen event to prevent it from being opened from the Navigation pane.

Private Sub Form_Open(Cancel As Integer)

If Me.OpenArgs() <> "Valid User" Then

MsgBox "You are not authorized to use this form!"

vbExclamation + vbOKOnly, "Invalid Access"

Cancel = True

End If

End Sub

If a user opens this form from the Navigation pane, she will see the message, You are not authorized to use this form! The Cancel = True statement prevents the form from opening.

For a user to gain access to this form you must have code in your application that, after the user has been approved, allows the form to open. For example, a form named frmRestricted would need your code to execute the following statement (be sure to specify other parameters as appropriate):

DoCmd.OpenForm "frmRestricted", , , , , , "Valid User"

Another time to use OpenArgs might be when you automatically populate information in the form you're opening. Suppose you have a combo box that requires the user to select a valid value. If the user enters an undefined value, you may want to pop up a form to collect information about the value just entered — you might want to pass the text that the user entered to the form that you're opening so it is populated in the appropriate field on the pop-up form. Clearly you can utilize the OpenArgs property for a variety of purposes.

You may prefer the new List Items Edit Form property, discussed earlier in the chapter, over writing your own code to pop up a form to collect a new value for the combo box.

Be aware that once the form is opened, the value of OpenArgs does not change on subsequent executions of the OpenForm method. For that reason, you may want to check to see if the form is open before you execute OpenForm. The form's IsLoaded property can tell you if a form is loaded.

0 0


  • max eisenhower
    Can openargs pass control names?
    6 years ago

Post a comment