passing controlname as openargs

  • Thread starter Thread starter Jesper F
  • Start date Start date
J

Jesper F

I have a small calendar-like form that the user can open
as a popup different places in the db. The value picked
here should then be placed in the control where the
user "came from".
I'm trying to pass the control name as a string in
openargs to the calendar form. My plan was to place this
value in an invisible textbox on the calendar form
to "remember" where to put whatever is picked
subsequently.
Can a controlname be passed in the openargs and how is the
value of that control then read in the calendar popup
(where it's then a string and not a control)?
Is there a better way of doing this?

If been trying

Dim ctl as control
set ctl = me.openargs

But it doesn't really work.
Thanks for any input.
 
Jesper F said:
I have a small calendar-like form that the user can open
as a popup different places in the db. The value picked
here should then be placed in the control where the
user "came from".
I'm trying to pass the control name as a string in
openargs to the calendar form. My plan was to place this
value in an invisible textbox on the calendar form
to "remember" where to put whatever is picked
subsequently.
Can a controlname be passed in the openargs and how is the
value of that control then read in the calendar popup
(where it's then a string and not a control)?
Is there a better way of doing this?

If been trying

Dim ctl as control
set ctl = me.openargs

But it doesn't really work.
Thanks for any input.

You can pass the control's name in OpenArgs, and then you could refer to
it by form and name if you knew what form it was on. But you may not
even need to do this. If you are always interested in the form and
control that was active at the time your popup form was opened, then you
can get a reference to that specific control from
Screen.ActiveControl -- provided that you interrogate that property in
the popup form's Open event. Here's an example of what you might have
in the popup form's module:

'----- start of example code for popup form's module -----
Option Compare Database
Option Explicit

Dim mctlFrom As Access.Control ' reference to "calling" control

Private Sub Form_Close()

' Note: this procedure is here just for demonstration purposes.

If Not (mctlFrom Is Nothing) Then
With mctlFrom
MsgBox "Control was " & .Name & ", Value was " & Str(.Value)
End With
End If

Set mctlFrom = Nothing

End Sub

Private Sub Form_Open(Cancel As Integer)

' Capture a reference to the control from which this form
' was opened, if there is one.

On Error Resume Next
Set mctlFrom = Screen.ActiveControl

End Sub
'----- end of example code -----
 
You can pass a control's name as an OpenArgs value this way:

DoCmd.OpenForm "PopupFormName", , , , , acDialog, "nameofcontrol"

Then you can use this name to write a value into that control so long as the
popup form knows the form's name:

Forms("CallingFormName").Controls(Me.OpenArgs).Value = MyValue

If you want, you can pass both the form's name and the control's name in the
OpenArgs string. Just delimit the names with a character (such as |) and
then use code to parse the string into the two different names and store
those names in variables, then use those variables in the above expression
in place of "CallingFormName" and Me.OpenArgs.
 
If the calendar-like form can be called from several forms, then I would have
thought that you will need to pass the Form Name as well as the Control Name
in theOpenArgs.
Rather than hold these in an invisible textbox, I would recommend that you
declare two string variables at module level in the calendar form. e.g.
Private mstrFormName As String
Private mstrControlName As String

When passing multiple parameters in OpenArgs, I always seperate them with
vbCrLf (as this would not feature in a parameter).
e.g DoCmd.OpenForm "CalendarForm",,,,,,strFormName & vbCrLf & strControlName

Then to split them in the calendar form, you can use code like

Dim strOpenArgs() As String
strOpenArgs = Split(Me.OpenArgs, vbCrLf)

Then to store the parameters in the module-level string variables declared
above,
mstrFormName = strOpenArgs(0)
mstrControlName = strOpenArgs(1)

Finally when you are ready to populate this control with the value selected
by the user, you can use code like
Forms(mstrFormName).Controls(mstrControlName).Value = strUserInput (or the
value from a control)

Hope This Helps
Gerald Stanley MCSD
 
Great! I was working along the idea of passing a komma
delimited string so your advice nailed it for me. Thanks!
 
May a suggest an even easier way. In your calendar form add the following code:

Option Compare Database
Option Explicit
Dim obj1 As Object

Private Sub cmdClose_Click()
DoCmd.Close
End Sub

Private Sub axCalendar_DblClick()
obj1 = Me.axCalendar.Value
Set obj1 = Nothing
DoCmd.Close
End Sub

Private Sub Form_Load()
'*** Set Calendar to Control's Current Value or Current Date if Blank ***
Me.axCalendar.Value = Screen.ActiveControl.Value
If IsNull(Me.axCalendar) Then Me.axCalendar = Date
Set obj1 = Screen.ActiveControl
End Sub

If you also create a Public Function in a General Module:

Public Function OpenCalendar()
DoCmd.OpenForm "frmCalendar", acNormal, , , , acDialog
End Function


Then you can add the following to the On Dbl Click Event

=OpenCalendar()

The beauty of this technique is that it works whether the control is on a
form, subform or nested subform, nothing is hardcoded into the calendar
control so you can use it unchanged in any database you develop, you can keep
your forms lightweight and as you are not passing arguments it doesn't matter
if you change the name of your forms or controls!

Pete
 
You can pass a control's name as an OpenArgs value this way:

DoCmd.OpenForm "PopupFormName", , , , , acDialog, "nameofcontrol"

Then you can use this name to write a value into that control so long as the
popup form knows the form's name:

Forms("CallingFormName").Controls(Me.OpenArgs).Value = MyValue

If you want, you can pass both the form's name and the control's name in the
OpenArgs string. Just delimit the names with a character (such as |) and
then use code to parse the string into the two different names and store
those names in variables, then use those variables in the above expression
in place of "CallingFormName" and Me.OpenArgs.


I know this post is really old, but I found it today and it works great. I'm wondering if you can help me expand it to allow the passing of a subform reference.

I have it written as this:
Forms(puForm).Controls(puControl).Value = Me.ID

puForm variable is a string that has the name of the form. It works when I just pass the name of a form, but in some instances I need to pass a form-subform, so I can set the control value on a subform. I need to pass something like "Forms!frmMainForm.Form.frmSubForm" and I've tried a bunch of variations, but it doesn't seem to allow a form-subform reference in the Forms collection. Is there a way to do this? I can evaluate that the string contains a subform, or I could specify a subform variable, for when needed and just check if it is null or has a value. I'm just not finding any good information on this type of solution.

Any help would be much appreciated!
 
Back
Top