Passing a control name to a function

M

Mark

Is it possible to pass a control name to a function on another form,
that function then changes the value of the control specified.
 
D

Douglas J. Steele

Yes, it is, but it's much easier if the function is in a stand-alone module,
rather than a module associated with a form.

Your declaration needs to be something like:

Function MyFunction(SomeControl As Control) As Long

and you'd invoke it as:

value = MyFunction(Me.MyControl)

or

value = MyFunction(Forms!MyForm!MyControl)

Within the function, you'd refer to SomeControl wherever you wanted to refer
to the control on the form.
 
M

Mark

Thanks for the quick response. Your suggestion does work, but it is
not exactly what I needed. My situation is I have a form (form 1) with
a field (a date field) and a button. When you click the button it
brings up a form with a calendar (form 2) on it. I would like it so
when the user selects a date on the calendar it returns the value to
the date field on form 1. I was hoping I could pass the control name
of the date field to the calendar function which when the user selects
a date the value of the date field (form 1) is updated within the
calendar function on form 2. My thinking is that I can reuse the
calendar function with the many other date fields I have within the
database.

I hope this makes sense.
 
D

Douglas J. Steele

Pass the name of the calling form, and the name of the control as the
OpenArgs in your OpenForm call:

DoCmd.OpenForm "CalendarForm", OpenArgs:="Form=MyForm;Control=MyTextBox"

(replace MyForm and MyTextBox with the correct values)

In the calendar form, check whether OpenArgs is Null or not. If it isn't,
parse the value of OpenArgs to determine the name of the calling form, and
the name of the calling control. Once you know them, populate that field.

Here's a snippet that shows how I do it:

Dim lngLoop As Long
Dim strControl As String
Dim strForm As String
Dim varCurrArg As Variant
Dim varArgs As Variant

If Len(Trim$(Me.OpenArgs & vbNullString)) > 0 Then
varArgs = Split(Me.OpenArgs, ";")
For lngLoop = LBound(varArgs) To UBound(varArgs)
varCurrArg = Split(varArgs(lngLoop), "=")
If UBound(varCurrArg) > 0 Then
Select Case varCurrArg(0)
Case "Form"
strForm = varCurrArg(1)
Case "Control"
strControl = varCurrArg(1)
Case Else
End Select
End If
Next lngLoop
If Len(strForm) > 0 And Len(strControl) > 0 Then
Forms(strForm).Controls(strControl) = MyValue
End If
End If

(where MyValue is whatever you're trying to pass back to the calling form.)
 
M

Mark

Thanks, that worked a treat.
Pass the name of the calling form, and the name of the control as the
OpenArgs in your OpenForm call:

DoCmd.OpenForm "CalendarForm", OpenArgs:="Form=MyForm;Control=MyTextBox"

(replace MyForm and MyTextBox with the correct values)

In the calendar form, check whether OpenArgs is Null or not. If it isn't,
parse the value of OpenArgs to determine the name of the calling form, and
the name of the calling control. Once you know them, populate that field.

Here's a snippet that shows how I do it:

Dim lngLoop As Long
Dim strControl As String
Dim strForm As String
Dim varCurrArg As Variant
Dim varArgs As Variant

If Len(Trim$(Me.OpenArgs & vbNullString)) > 0 Then
varArgs = Split(Me.OpenArgs, ";")
For lngLoop = LBound(varArgs) To UBound(varArgs)
varCurrArg = Split(varArgs(lngLoop), "=")
If UBound(varCurrArg) > 0 Then
Select Case varCurrArg(0)
Case "Form"
strForm = varCurrArg(1)
Case "Control"
strControl = varCurrArg(1)
Case Else
End Select
End If
Next lngLoop
If Len(strForm) > 0 And Len(strControl) > 0 Then
Forms(strForm).Controls(strControl) = MyValue
End If
End If

(where MyValue is whatever you're trying to pass back to the calling form.)
 
Top