Passing more than one parameter to a form

The OpenArgs property of an Access form lets us pass a parameter into that form but it will only pass a single parameter. Sometimes we need to pass some more complex information across. The solution is to concatenate the two values into a single argument. It's a kludge but it works.

Passing the parameters

The first step is to construct a single string as the argument. In this example I'm wanting a form named EditPerson to show the details of a person working on a client's site and I need the ID code of the client as well as the code for the person. Before calling the EditPerson form I concatenate the two arguments into a single string with a delimiter between them:

'-- OpenArgs will only accept a single parameter so we need to merge the two ID
'-- codes into a single string.

strTarget = txtClientid.Value & CH_DELIMITER & txtPersonID.Value
DoCmd.OpenForm FormName:="EditPerson", OpenArgs:=strTarget

Receiving the parameters

The form that receives this composite argument has to break it down into two separate strings. The split function introduced in Access 2000 makes this very easy:

'-- Break the argument into a two-member array
astrID = Split (Expression:=OpenArgs, Delimiter:=CH_DELIMITER, Limit:=2)
'-- Put the array members into variables for clarity later on
strClientID = astrID(0)
strPersonID = astrID(1)

The split function takes a string and breaks it into an array of shorter strings based on the delimiter that you specify. There's no real need to assign the values from the array into separate variables, I've just added this step because I know that the code elsewhere in the form will be easier to debug if I read strClientID rather than astrID(0).

Comments

Note that I'm using a delimiter between the two ID codes because I'm going to need to separate the two strings in the next form. The delimiter must be a character that isn't going to appear in either of the other strings so as a starter I decided to use the pipe symbol ("|"). I might have to use the same technique elsewhere in this database so I've defined this delimiter as a constant earlier in the program.

'-- Delimiter for multiple parameters
Public Const CH_DELIMITER As String = "|"

This example has been a simple one because both arguments were strings. If I had needed to pass a pair of numeric values then I'd have had to use Str$ to convert the numbers into strings before concatenating them and Val to convert the two array members back into numeric values.