change label format based on control it is attached to

N

night_writer

I hve a form that has checkboxes for all 50 states. Because individual
checks are hard for the user to see, I color the label of the field
when the box is checked. When the form is open and a new a box is
checked or unchecked, I run the following code for each checkbox on
the AfterUpdate event:

Private Sub chkAK_AfterUpdate()

If Me.chkAK.Value = True Then
With Me.chkAK_Label
.BackStyle = 1
.BackColor = 16762052
End With
Else
With Me.chkAK_Label
.BackStyle = 0
.BackColor = -2147483633
End With
End If
End Sub

However, I also want to check the condition of each box and color the
label appropriately when the OnCurrent event occurs for the form
itself. Right now, I am actually doing a "call" for every individual
checkbox AfterUpdate event and it gets the job done, but seems
excessively heavy handed.

I am thinking that some kind of For / Next for each control would be
much better, but I can't figure out how to go from testing the
checkbox to identifying the label that is attached to it. Because all
of my controls are named similarly, I could actually create the label
name from the checkbox name ("chk" & right(checkbox.Name,2) &
"_Label"), but that's a string. I don't know how to translate that to
refer to the label control so I can change the formatting on the
label.

Maybe I'm trying to make this too difficult. Would appreciate any
creative solutions anyone cares to offer!

Alice
 
N

night_writer

Me.Controls("chk" & Right(checkbox.Name,2) & "_Label")

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)












- Show quoted text -

Thank you very much. This did the trick!

Here is my final code, just in case anyone else would find it useful.
(I tagged my state checkboxes "state" so I wouldn't be checking any
other checkboxes on the form):

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "State" Then
Select Case ctl.ControlType
Case acCheckBox
If ctl.Value = -1 Then
With Me.Controls("chk" & Right(ctl.Name, 2) &
"_Label")
.BackStyle = 1
.BackColor = 16762052
End With
Else
With Me.Controls("chk" & Right(ctl.Name, 2) &
"_Label")
.BackStyle = 0
.BackColor = -2147483633
End With
End If
End Select
End If
Next ctl



Alice
 

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