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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HighLight Label by checking checkbox VB code for public

Status
Not open for further replies.

JOD59

Technical User
Dec 17, 2003
39
US
Hi, I need help writing code to highlight a label when a checkbox is checked. I want o be able to call this procedure so I don't have to code it a 100 times. Here’s what I have so far but I know its wrong. Any help will be greatly appreciated. Thanks


Option Compare Database
Option Explicit
Const conHiOn As Long = 65535
Const conHiOff As Long = -2147483633

Public Sub Cmd_HighLight()

Dim ctl As Control
For Each ctl In Controls
If Me.ctl.ControlType = acCheckBox = True Then
If Me.ctl.ControlType = acLabel Then
ctl.BackColor = conHiOn
Else
ctl.BackColor = conHiOff

End If
End If
Next ctl
End Sub
 
Something like this ?
For Each ctl In Controls
If Me.ctl.ControlType = acLabel Then
If Me![checkbox name] = True Then
ctl.BackColor = conHiOn
Else
ctl.BackColor = conHiOff
End If
End If
Next ctl

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank PH, This is sort of what I'm looking to do, However I would like to have the command cycle through all of the check boxes and for each one that test true change the label backcolor to 65535, I think I also need to put error handling to bypass the labels that are not attached to the checkboxes. I also get this error when I run your code and I think it might be because of not having error handling. "Member or data method not found" Any thoughts? Thanks
 
If the labels are attached to the checkboxes, you could try something like this:

[tt]dim ctl as control
for each ctl in me.controls
if ctl.controltype=accheckbox then
if ctl.value then
ctl.controls(0).BackColor = conHiOn
else
ctl.controls(0).BackColor = conHiOff
end if
end if
next ctl[/tt]

If they are not attached, I'd think using the closest thing to control arrays you've got in Access, naming the controls chk1, chk2, ... chkN and lbl1, lbl2, ... lblN

Then perhaps something like this

[tt]dim ctl as control
for each ctl in me.controls
if ctl.controltype=accheckbox then
if ctl.value then
me("lbl" & right$(ctl.name,1)).BackColor = conHiOn
else
me("lbl" & right$(ctl.name,1)).BackColor = conHiOff
end if
end if
next ctl[/tt]

- typed not tested...

Roy-Vidar
 
OK, I understand now.
For Each ctl In Controls
If ctl.ControlType = acCheckBox Then
If ctl.Value = True Then
ctl.Controls(0).BackColor = conHiOn
Else
ctl.Controls(0).BackColor = conHiOff
End If
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Roy, IT Works great; seeing I'm new to this world of VB I have a couple of questions. I assume that the default after
"if ctl.controltype=accheckbox then" tests True

And the same is probably true for

if ctl.value then
ctl.controls(0).BackColor = conHiOn

If value tests to true what is being tested? and
I'm not sure what ctl.controls(0) is doing?

Sorry for my ignorance and thanks for your quick help.


 
It's kind of usual to drop further testing on boolenas. Since they can be either true or false, the test

[tt]If ctl.Value = True Then[/tt]

means either

[tt]If True = True Then ' or
If False = True Then[/tt]

I always do that on booleans. Some say it reduces readability, then I'd say Read More;-)

For instance, let's say you wan't to toggle the enabled property of a control based on whether or not another control was checked:

[tt]me!txtSomeControl.enabled = me!chkCheckbox.value

vs

if me!chkCheckbox.value = true then
me!txtSomeControl.enabled = true
else
me!txtSomeControl.enabled = false
end if[/tt]

First one executes faster, is faster to write, and after some time will become easier to read (well, in my opinion)

The .Value property of a control, is one of several properties, it is also the default property, so lot of developers don't use it when refereing to the value currently in the control.

On controls having an attached label, that control is the parent control of the label, which means you can reach it thru me!txtSomeControl.controls(0). There are some more samples at the bottom of faq702-5010.

Roy-Vidar
 
A variation on Boolean:
For Each ctl In Me.Controls
If ctl.ControlType = acCheckBox Then
ctl.Controls(0).BackColor = IIf(ctl.Value, conHiOn, conHiOff)
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Roy Thanks, great answer I think I have it now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top