Source Code Library
VBA Error Handling
Cloud and Azure
Visual Studio LightSwitch
Connect with Us
The DoCmd.OpenReport command has similar parameters.
The Problem: Passing More than One Value to the Form/Report
Unfortunately, it's not possible to pass multiple OpenArgs parameters to a Microsoft Access form or report, or pass an array of values. Only one OpenArgs string can be used in the OpenForm or OpenReport command.
The Solution: Concatenate Your Values
By combining your values into one string separated by a character that would not be in your string, you can overcome the OpenArgs limitation and still use DoCmd to pass them. The OpenArgs parameter is a string that the form can read once it is opened. Calling the form, add the string to the OpenArgs parameter like this:
DoCmd.OpenForm "frmName", OpenArgs:="Parameter(s)"
Similarly, for reports, you can use this:
DoCmd.OpenReport "rptName", OpenArgs:="Parameter(s)"
Passing Two Values in the OpenArgs Parameter
A real-life example would be to set default values for controls on a form being opened. Suppose that you have a form of Product Categories, and the user needs to enter a new product for the category. You could open the Product entry form and then default the category value for them. With the following parameter, you could default the Category combo box to the current value.
DoCmd.OpenForm "frmName", OpenArgs:="cboCategory|" & txtCategoryID
which may result in a value such as "cboCategory|123"
Rather than explicitly specifying the parameter, you can also pass it as the last parameter in the list:
DoCmd.OpenForm "frmName", , , , , , "cboCategory|" & txtCategoryID
Parsing the OpenArgs Parameter in the Form's Load Event
In the Load event of the Form, add some VBA code to parse the information that is being passed in OpenArgs. In this example, we are using two values, separated by the 'Pipe' (|) character. The code in the form finds the existence of the Pipe character, then extract the first and second values to make the assignment.
Private Sub Form_Load() Dim intPos As Integer Dim strControlName As String Dim strValue As String If Len(Me.OpenArgs) > 0 Then ' Position of the pipe intPos = InStr(Me.OpenArgs, "|") If intPos > 0 Then ' Retrieve Control Name from the first part of the string strControlName = Left$(Me.OpenArgs, intPos - 1) ' Retrieve Value to Assign from the end of the string strValue = Mid$(Me.OpenArgs, intPos + 1) ' Assign the value to the control Me(strControlName) = strValue End If End If End Sub