Link to home
Start Free TrialLog in
Avatar of Lawrence Salvucci
Lawrence SalvucciFlag for United States of America

asked on

Change backcolor of label on access form and make checkboxes visible

I have a bunch of labels on my form that when a user clicks on one of them I need the backcolor of that label to change to yellow. And then if they double-click on it then it changes back to the standard white color. Is there a function that I can call from the "On Click" and "On Dbl Click" events instead of typing an IF statement for each label? I would prefer to just have a function to run these two pieces of code so I can call the code with line of code instead of an IF statement for each label. I know I've seen something like this before.

The second part of this might not be doable...With each of these labels I have 2 checkboxes (not associated with this label). When the user clicks on the label I want it to change the backcolor as outlined above, but then I need to have these 2 checkboxes to be visible (they aren't visible as the default setting). Then if they double-click on the label then these two checkboxes will be unchecked (if they were checked) and then have them disappear and no longer be visible. When they are not visible they should never be checked. They can only be checked if the user clicks on the label to make them appear. Then the user can check them if needed. But then if they double-click on the label both checkboxes are unchecked and then disappear. Like I said, not sure if this part is doable.

Just trying to simplify this code and not have a bunch of IF statements for each labels click & double-click events.
Avatar of Michael Carrillo
Michael Carrillo
Flag of United States of America image

For the first one; I would use the Form Events (on click and on Double Click).
Then you could use the tag property of each of the labels and set them to a specific value (like 'Color me'). Then loop through all the controls on the form and apply the color change.

The Check boxes can be done.  But setting the event on the form might not be good after all if you need to do both.

You might need to put your labels inside some kind of container and then use the containers event properties.

Check boxes can make use of the tag field as well. Perhaps sets its value to 'Check me' and include in loop.  

I think my concern is a single click can trigger the whole thing.  You want to carefully consider what is clicked to do this.
You could use a button or put a picture (icon) on the form that users click, instead of just any label.
Avatar of Lawrence Salvucci

ASKER

So if I use the On Click and Double-Click of the forms events then how do I write the code to have it change the color for a specific label when it's clicked or double-clicked?

What do you mean put the labels in a container? Not sure I follow you there.

A single click on a specific label will turn on (make visible) the 2 checkboxes that go with that label (although not associated) and also change the backcolor of that label to vbYellow. And a double-click will make them have false values and make them not visible. But it will also change the backcolor of that label to vbWhite.

I would prefer to find a way to do all this from the specific label instead of a button since not every label will change the backcolor. Only the label that is clicked on.
So doing a little more research on this. I found this code that does what I want as far as changing the backcolor of the label when you click and double-click it. But it does it for all the labels on my form, not just the one I click on. How can this be modified to only change the backcolor of the label I actually click or double-click?

Private Sub Form_Open(Cancel As Integer)
       For Each ctrl In Me.Controls
        If ctrl.ControlType = acLabel Then
            ctrl.BackColor = 13754083
            End If
        Next ctrl
End Sub

Sub Enter_Option()
    OptionNameStr = Me.ActiveControl.Properties("name")
    For Each ctrl In Me.Controls
       If ctrl.ControlType = acLabel Then
               LabelNameStr = ctrl.Properties("name")
               ctrl.BackColor = vbYellow
           End If
        Next ctrl
End Sub

Sub Exit_Option()
    OptionNameStr = Me.ActiveControl.Properties("name")
    For Each ctrl In Me.Controls
       If ctrl.ControlType = acLabel Then
               LabelNameStr = ctrl.Properties("name")
               ctrl.BackColor = 13754083
           End If
        Next ctrl
End Sub

Private Sub lblExcellenceSub1_Click()
Call Enter_Option
End Sub

Private Sub lblExcellenceSub1_DblClick(Cancel As Integer)
Call Exit_Option
End Sub

Open in new window

Ok, It looks like I misunderstood what you were trying to do. So to make sure I understand:

CheckBox1
CheckBox2

Label1
Label2
Label3
Label4
Label5

If any label (1-5) is clicked it should turn colors and checkboxes are visible
If no labels are colored then Checkboxes are unchecked and not visible.

If this is correct then:

Each label has on click event to turn on color background and set Property Tag to 'Color' for label.
Each label has on double click  event to turn off color and set Property Tag to 'No Color' for label.
Default value for Property Tag is 'No Color' for each label.

On form Click event:
Loop through all controls with Tag values.

If count of TagsHaveColor > 0 then
CheckBox1 and CheckBox2 are visible.
Else
CheckBox1 unchecked
CheckBox2 unchecked
CheckBox1 and CheckBox2 hidden.

Does this describe what you are trying to do?
Yes it does, but not completely. Each label has it's own 2 checkboxes. So if I have 5 labels on my form then I have 10 checkboxes. Each label has 2 checkboxes that are made visible when the label is clicked and then invisible when the label is double-clicked. And along with that when the label is clicked once the backcolor of the label will turn to vbYellow. And when it's double-clicked it turns back to vbWhite.
Ok, So the Scenerio is:

Label1     Checkbox1a     Checkbox1b
Label2     Checkbox2a     Checkbox2b
Label3     Checkbox3a     Checkbox3b
Label4     Checkbox4a     Checkbox4b
Label5     Checkbox5a     Checkbox5b

So Label1 on Click event will:
-Change Label1 backcolor to vbYellow
-Checkbox1a will be visable
-checkbox1b will be visable

And Label1 Double Click event will:
-Change Label1 backcolor to vbWhite
-Set Checkbox1a value to false
-Set Checkbox1b value to false
-Checkbox1a hidden
-checkbox1b hidden


Does this describe what you are wanting to do?
Yes, exactly what I'm looking for.
ASKER CERTIFIED SOLUTION
Avatar of Michael Carrillo
Michael Carrillo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will have to do this for each label and the Checkboxes start off being hidden.
Thank you for the code. I appreciate it. Is there an easier way to write this as a function instead of doing it for each label? I was hoping to have just one function that I could call from each label with one line of code instead of creating this code for each label. For example the code I had posted that I found is 2 functions that are called from the click and dbl-click events of each label but that code does it for all the labels and not just the one I clicked on. So instead of having 5 lines of code for each label I was hoping to just call the function in one line of code for each label. Just wanted to simplify it. If not, no big deal. I can run with it like this.
Yes, but I am not sure you would want to go down that path.

You could use the Tag property to describe a group of controls. For example

Tag 'Gp1' would be associated with Label1, Checkbox1a and Checkbox1b
Tag 'Gp2' would be associated with Label2, Checkbox2a and Checkbox2b

Then the on click would pull the Tag for the clicked property and change the properties for all the controls with that same tag. This would require a loop to go through all controls with that Tag. You would also have to configure the all the Tags of both labels and Checkboxes.

So either you write the code for each set of controls or you manage the properties (Tags) of each of the controls and create a function to retrieve the control Tag values.

How many control sets (labels) do you have?
There are 53 labels and 106 checkboxes. I think going this path makes more sense than using the tag property. It sounds like much more work to do it that way. I'll keep things simple like this, even if it's more lines of code.
Thank you very much for your help. I greatly appreciate it!