Referencing a field programmatically

  • Thread starter Thread starter Raf
  • Start date Start date
R

Raf

I have 12 textboxes in a Report depending on witch month the BackColor Change
I’m using now a Select Case with 12 cases
I'm hoping I can use a loop my dilemma is how to reference the field

I want some thing like this
m being the month
for i= m to 12
Field i .BackColor = 16777215 ---> in the report I have fields 1 to 12
next i

any help will be appreciated
 
Assumption:
You have twelve CONTROLS named Calendar1 to Calendar12

For i = 1 to 12
Me.Controls("Calendar" & i).BackColor = 16777215
Next i

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
Raf said:
I have 12 textboxes in a Report depending on witch month the BackColor Change
I’m using now a Select Case with 12 cases
I'm hoping I can use a loop my dilemma is how to reference the field

I want some thing like this
m being the month
for i= m to 12
Field i .BackColor = 16777215 ---> in the report I have fields 1 to 12
next i

any help will be appreciated

I'm confused. Do you want all of the 12 controls to have the same
background color and this color changes if the month is different? Or, does
each control represent a month and you want each month (control) to have a
different background color?

John's answer assumes: (a) you want all the controls to have the same color
and (b) the color won't change (i.e. he's hard coded the color value). If
you want the color to change every month, you're stuck using a SELECT/CASE
scenario with each CASE having the color value for that month. For instance,
something like this:

' Change the color depending on the month for today
Select Case month(Now)
Case 1 ' January
lngColorVal = 16777215
Case 2 ' February
lngColorValue = (put another color value here)
Case (all the other months)
lngColorValue = (put another color value here)
End Case

Then you loop through the controls like this:

For i = 1 to 12
Me.Controls("Calendar" & i).BackColor = lngColorValue
Next i

Is *that* what you're trying to do? If each control represents a month and
you want each control to have a different background color, that could be
done in design mode without any coding.

Regards, Chris
 
Back
Top