BaldyWeb

Open a second form using OpenArgs to Conditionally Populate Controls

Sometimes you might want to pass values from one form to another when opening the second form.  You can use OpenArgs for this, and the value will be available in the second form as long as it stays open, without the first form having to stay open.  Using the Split() function and a delimiter, you can even pass more than one value.  The code to open the form and pass 2 values would look like:

DoCmd.OpenForm "frmOpenArgs", , , , , , Me.EmpID & ";" & Me.txtEmpLName

Where frmOpenArgs is the name of the form being opened, Me.EmpID & Me.txtEmpLName contain the 2 values to be passed.  The code in the second form to handle the OpenArgs would look like this, which includes code to handle the possibility of them not being present:

Dim strOpenArgs() As String

If Not IsNull(Me.OpenArgs) Then
  strOpenArgs = Split(Me.OpenArgs, ";")
  Me.txtEmpID = strOpenArgs(0)
  Me.txtEmpLName = strOpenArgs(1)
Else
  Me.txtOtherInfo = "Unknown"
End If

Here's a sample demonstrating the technique:

OpenArgs sample db

Home