Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I calculate a control name and test it?

Status
Not open for further replies.

mystuff

Programmer
Apr 30, 2004
86
0
0
US
I don't know if this is possible. I want to use a "Do" loop to run thru a series of controls on my form. The controls are named ctrl1, ctrl2, ctrl3 ... ctrl20 etc. The controls are check boxes. So I am trying to loop thru to determine which ones are "True". I want to use a DO loop to calculate the name of the control, and then look at the value. So far, this is what I have:


Counter = 1
Do While counter < 21
fnCtrl = "ctrl" & Counter ' the 1st time, the control name will be ctrl1
If fnCtrl = True Then
MsgBox("True")
Else
MsgBox("False)
Endif
Counter = Counter + 1
Loop

I want the 1st line after the Do to use the value of fnCtrl as the fieldname to be compared. I want it to be executed as
If ctrl1 = True

But what happens, it is evaluated as
fnCtrl = "ctrl1"

Does anyone have any idea if this can be done.

 
mystuff,
You were almost there.
[tt]If Me.Controls(fnCtrl) = True[/tt]

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Minor point. If something evaluates to true then you do not have to say "True = True". So the faster, and cleaner way is simply

If Me.Controls(fnCtrl) Then
do something
end if

 
...and a tad more succinct,

For x = 1 to 20
If Me("ctrl" & x) Then
MsgBox "False"
Else
MsgBox "True"
End If
Next x

and finally,

For x = 1 to 20
If Me("ctrl" & x) Then MsgBox "False" Else MsgBox "True"
Next x
 
Maybe:
Code:
For X = 1 To 20
    MsgBox Format(Me("ctrl" & X), "True/False")
Next X
[dazed]


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top