Form References

P

PeterM

I have a form (switchboard) and on SwitchBoard is a subform control
(subform!).
There are 10 fields on SwitchBoard entitled Program1 thru Program10. From
within subform1 I issued:

Dim i As Integer
For i = 1 To 10
Me("forms!switchboard!Program" & i).ForeColor =
DLookup("Boxclickedbordercolor", "SystemTable")
Next i

that code doesn't work. So I changed it to the literal below just for
testing and this code works.

Forms!switchboard!Program1.ForeColor = DLookup("Boxclickedbordercolor",
"SystemTable")


What is wrong for the first version?

Thanks for your help!
 
D

Dirk Goldgar

PeterM said:
I have a form (switchboard) and on SwitchBoard is a subform control
(subform!).
There are 10 fields on SwitchBoard entitled Program1 thru Program10. From
within subform1 I issued:

Dim i As Integer
For i = 1 To 10
Me("forms!switchboard!Program" & i).ForeColor =
DLookup("Boxclickedbordercolor", "SystemTable")
Next i

that code doesn't work. So I changed it to the literal below just for
testing and this code works.

Forms!switchboard!Program1.ForeColor = DLookup("Boxclickedbordercolor",
"SystemTable")


What is wrong for the first version?


"Me" refers to the module that is running the code; in this case, the
subform. Your expression:
Me("forms!switchboard!Program" & i)

.... implies that "forms!switchboard!Program1" (and its friends) are controls
on the subform, but they aren't. You should drop the "Me", and write your
loop like this:

Dim lngColor As Long
Dim i As Integer

lngColor = DLookup("Boxclickedbordercolor", "SystemTable")

For i = 1 To 10
Forms!switchboard.Controls("Program" & i).ForeColor = lngColor
Next i

That would be the general case. Since you know that this code is running on
a subform of the form "switchboard", you could simplify the form reference
to:

Me.Parent.Controls("Program" & i).ForeColor = lngColor
 
P

PeterM

Thanks Dirk... worked like a charm!

Dirk Goldgar said:
"Me" refers to the module that is running the code; in this case, the
subform. Your expression:


... implies that "forms!switchboard!Program1" (and its friends) are controls
on the subform, but they aren't. You should drop the "Me", and write your
loop like this:

Dim lngColor As Long
Dim i As Integer

lngColor = DLookup("Boxclickedbordercolor", "SystemTable")

For i = 1 To 10
Forms!switchboard.Controls("Program" & i).ForeColor = lngColor
Next i

That would be the general case. Since you know that this code is running on
a subform of the form "switchboard", you could simplify the form reference
to:

Me.Parent.Controls("Program" & i).ForeColor = lngColor



--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)
 

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