Solved

Change backcolor of label on access form and make checkboxes visible

Posted on 2014-09-08
13
435 Views
Last Modified: 2014-09-09
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.
0
Comment
Question by:Lawrence Salvucci
  • 7
  • 6
13 Comments
 
LVL 9

Expert Comment

by:macarrillo1
Comment Utility
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.
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
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.
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
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

0
 
LVL 9

Expert Comment

by:macarrillo1
Comment Utility
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?
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:macarrillo1
Comment Utility
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
Yes, exactly what I'm looking for.
0
 
LVL 9

Accepted Solution

by:
macarrillo1 earned 500 total points
Comment Utility
Code should look something like:

Private Sub Label1_Click()
    [Label1].BackColor = vbYellow
    CkBox1a.Visible = True
    CkBox1b.Visible = True
  
End Sub

Private Sub Label1_DblClick(Cancel As Integer)
    [Label1].BackColor = vbWhite
    CkBox1a.Value = False
    CkBox1b.Value = False
    CkBox1a.Visible = False
    CkBox1b.Visible = False

End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:macarrillo1
Comment Utility
You will have to do this for each label and the Checkboxes start off being hidden.
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
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.
0
 
LVL 9

Expert Comment

by:macarrillo1
Comment Utility
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?
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
Comment Utility
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.
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
Comment Utility
Thank you very much for your help. I greatly appreciate it!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now