Use a variable as a control name

DataMiner

Registered User.
Local time
Today, 23:56
Joined
Jul 26, 2001
Messages
336
Why doesnt this work:
dim ctl as Control
strfromdate=forms("OTDSwitchBoard")("FromDate")
set ctl=strfromdate

But this does:
dim ctl as Control
set ctl=forms("OTDSwitchBoard")("FromDate")


I need the value of ctl to be a variable that I read from the OpenArgs string of the form. How do I make this work??

More details:
Form1 has a field for StartDate and StopDate. I want to fill these in with the beginning and ending dates of our "manufacturing month" (which doesn't correspond exactly to the calendar month)

When user clicks the MfgMonth button on Form1, Form2 opens. The OpenArgs for Form2 contains names of the StartDate and StopDate controsl on Form1. User will click on the appropriate "month" button on Form2. Form2 calculates the beginning and ending dates for the month, and writes them back to the appropriate controls on Form1.

I want to use this for a bunch of different forms, and the names of the controls for StartDate and StopDate will not always be the same. So, I want this to be a variable.

Access 2002.
 
strfromdate=forms("OTDSwitchBoard")("FromDate")

Above you are setting the variable "strfromdate" to the value in the "FromDate" control on the "OTBSwitchBoard" form. Whatever value is in the control is now in the variable.

set ctl=forms("OTDSwitchBoard")("FromDate")

and here you are setting ctl to an object reference of the control. You use the former to get the contents of the control into a program variable and the latter to get to the properties of a control, like:

Code:
dim ctl as Control
set ctl=forms("OTDSwitchBoard")("FromDate")
ctl.Visible = True    'make control visible
ctl.SpecialEffect = 4   'shadowed

I need the value of ctl to be a variable that I read from the OpenArgs string of the form. How do I make this work??

I'm not 100% sure on this, but I don't think you can set the OpenArgs to anything except what evaluates to a string expression, and you're only allowed one string expression.

You need 3 things: the form name that form2 is opened from; the StartDate control name on form1; and the StopDate control name on form1.

So each and every form that can open form2 is going to need code like:

Code:
Dim frmStrArg as String
' comma delimits required
frmStrArg = Me.Name & ",StartDateControlName,StopDateControlName"
DoCmd.OpenForm "form2", , , , , , frmStrArg

Then, in either your form2 command button on click events or a called function, you'll need to parse the names back out of the form2 OpenArgs. If you set a function up in your class module for form2 that's called by every applicable command button on form2, you can use something like this:

Code:
Function fncWriteBack()
Dim strTemp as String
Dim strFrmName as String
Dim strCtlName1 as String
Dim strCtlName2 as String
Dim CalcDate1, CalcDate2 as Date

'figure calculated dates
CalcDate1 = ......
CalcDate2 = ......

strTemp = Me.OpenArgs

'parse form name to write back too...
strFrmName = Left(strTemp, InStr(1,strTemp,",")-1)
strTemp = Mid(strTemp,InStr(1,strTemp,",")+1)

'parse StartDate control name to write back too...
strCtlName1 = Left(strTemp, InStr(1,strTemp,",")-1)
strTemp = Mid(strTemp,InStr(1,strTemp,",")+1)

'parse StopDate control name to write back too...
strCtlName2 = strTemp

'write back to form that opened form2...
Forms(strFrmName)(strCtlName1) = CalcDate1
Forms(strFrmName)(strCtlName2) = CalcDate2

End Function

hth,

(Edit: rats, caught some syntax errors...)
 
Last edited:
Thanks a million! Works great now.
 

Users who are viewing this thread

Back
Top Bottom