passing controlname as openargs

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.
 
D

Dirk Goldgar

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 -----
 
K

Ken Snell [MVP]

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.
 
G

Gerald Stanley

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
 
J

Jesper F

Great! I was working along the idea of passing a komma
delimited string so your advice nailed it for me. Thanks!
 
P

Pete

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top