openargs

  • Thread starter Thread starter dave
  • Start date Start date
OpenArgs is a generic place to stuff some value you want to pass to a form.

Example use:
In the NotInList event of a combo box, you want to open a form where the
user can supply the appropriate fields for the lookup tables. You must open
the form in Dialog mode to pause the combo's NotInList event until the data
is added. You also need to pass the value to the new form.

DoCmd.OpenForm "MyForm", DataMode:=acFormAdd, WindowMode:=acDialog,
OpenArgs:=NewData

Then in the Load event of the dialog, you can drop the passed value into the
appropriate field.
 
OpenArgs holds a string value. OpenArgs is the last parameter in the OpenForm
method. You set the OpenArgs value with an expression like DoCmd.OpenForm
"MyForm",,,,,,"<<Some String Value>>". (Other parameters can be set where I show
commas!) When you open a form with this expression, the OpenArgs value of the
form has the value you entered in the OpenForm expression.

One way I have used OpenArgs is to set the caption of a form when it opens. I
had a generic form with a calendar that was used in several routines in the
application. So rather than creating a static caption for the calendar form, I
did the following:

DoCmd.OpenForm "CalendarForm",,,,,,"Click On The Date Of Service"

Then in the Open event of the calendar form I had:
Me.Caption = Me.OpenArgs

I used the OpenArgs everywhere I opened the calendar form to make the caption
fit what was needed at that point. For example, at another point in the
application I had the following statement:
DoCmd.OpenForm "CalendarForm",,,,,,"Click On The Starting Date"
 
dave said:
Can someone give me a bref explanation of how openargs
works and possible uses?


You can pass any string to a form (and in A2K+, a report)
when you open it:

DoCmd.OpenForm "someform", _
OpenArgs:= "anystring"

Then someform can retrieve the string by refering to its
OpenArgs property. One common use is to have a form opened
and position it at a specified record.

If Not IsNull(Me.OpenArgs) Then
With Me.RecordsetClone
.FindFirst "keyfield = " & Me.OpenArgs
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With
End If
 
Thanks Allen... so if I was to create a number of forms
and one standard dialog form which could be used to maybe
list choices for a selection (or whatever) I could use
the open args statement to pass the selection back to the
main form, to a specific text box which may have
different names depending on the main form it opened from?
 
what I would like to be able to do... I want to be able
to dblclick on a field on a main form to open a dialog,
make a selection on the dialog form and pass the value
back to the field I double clicked from on the main form
(which will always be only one field but the name will
vary depending on the main form I open the dialog from).
At the moment I have (on the dialog) Forms![frmStockItem]!
[txtStockCode]=me.txtStkCode It would be great to be
able to pass the Forms![frmStockItem]![txtStockCode] in
an open args statement so I could use multiple main forms
with different field names to pass the value back to. At
the moment I need multiple dialog forms which are all
exactly the same but have slightly different code behind
them to pass the value back to different main forms.
 
Dave said:
what I would like to be able to do... I want to be able
to dblclick on a field on a main form to open a dialog,
make a selection on the dialog form and pass the value
back to the field I double clicked from on the main form
(which will always be only one field but the name will
vary depending on the main form I open the dialog from).
At the moment I have (on the dialog) Forms![frmStockItem]!
[txtStockCode]=me.txtStkCode It would be great to be
able to pass the Forms![frmStockItem]![txtStockCode] in
an open args statement so I could use multiple main forms
with different field names to pass the value back to. At
the moment I need multiple dialog forms which are all
exactly the same but have slightly different code behind
them to pass the value back to different main forms.


Open the dialog form in dialog mode so the code in the main
form pauses until the dialog form is closed. For syntax
reasons, you need to pass both the name of the calling form
and the text box where you want the result placed.

DoCmd.OpenForm "MyDialog", _
OpenArgs:= Me.Name & ";" & Me.ActiveControl.Name

Then the dialog form can separate the two names and store
the selected value back to the calling form:

Dim intPos As Integer
intPos = InStr(Me.OpenArgs, ";")
If intPos > 1 And intPos < Len(Me.OpenArgs) Then
Forms(Left(Me.OpenArgs, intPos - 1) _
.Controls(Mid(Me.OpenArgs, intPos + 1) _
= selectedvalue
Else
' Invalid OpenArgs
STOP
End If
 
I use three methods to pass values between form.

The simplest but least elegant is to use a global variable.

The second is using openargs. Split() can be used to
separate multiple arguments passed via openargs. These
can then be referenced as Argv(0), Argv(1) etc. and
converted to the appropriate data type in the Form_Open
event.

The third is a stack. You can push values onto a stack of
variants and pop them off again after returning from the
dialog. This is my favoured way of retrieving data from a
dialog. You have to be prepared if you cancel the dialog
that you don't pop the stack inappropriately. A stack
class can be created to make stack handling easy.

Rod Scoullar
 
Back
Top