Unanswered: Check box status defines contents of label
I've tried to search the forums for check box related threads but I can't pinpoint what I'm looking for.
This is probably very simple, but as a beginner, I'm struggling to work out how to make a label (or any kind of text box for that matter) display the label titles of check boxes that have been ticked. These labels are derived from field titles in a table.
Basically, each database entry will have several boxes ticked, but at a glance you can't see them all clearly. For this reason, i'd like a label underneath all the tick boxes that automatically updates with the text label of a check box when you tick it, and removes the text label when you untick it. So in effect, i'll have a summary below of all the tick boxes that have been checked.
Hope this makes sense!!
Edit: I'm thinking something along the lines of a macro like this in a memo box...
Private Sub UPDATED_NOTES_Enter()
If CHECKBOX1= -1 Then displaytext "CHECKBOX1, "
If CHECKBOX2= -1 Then displaytext "CHECKBOX2, "
It needs to show EVERYTHING that is ticked in one field...be really grateful if someone can point me in the right direction...
I would keep an array of strings laying around to store all of the desired checkboxes. Then, create a function/sub that will add or remove a given checkbox name from that array whenever a control is checked. Finally, this same function/sub can be responsible for rifling through the array and building a concatenated string from the values contained therein.
My VBA is a little rusty, but I can probably mock up some psuedo code or something if that doesn't make sense...
When you create your checkbox name it accordingly, e.g. “Tickbox1” , then name the label associated with the checkbox e.g. “LabelForTickBox1”, in the properties of the checkbox select the field “OnClick”, a drop down will appear and next to that … button select tah and select code builder, paste in the code below
Try this, basically when you tick the check box, the label associated with that checkbox appears, when you uncheck it, it dissapears
Private Sub TickBox1_Click()
'If True then Label becomes visible, else label is invisible
If Me.TickBox1 = True Then
Me.LabelForTickBox1.Visible = True
If Me.TickBox1 = False Then
Me.LabelForTickBox1.Visible = False
Thanks for your quick reply, but i'm still quite unsure how to implement your suggestion. I wouldn't know how to even start with keeping an array of strings! I really am a n00b at this stuff and coupled with the fact I haven't touched databases for months hasn't helped!
I've attached a picture of the database as it stands.
I'd like all the ticked check box titles to appear in the updated notes memo box automatically if possible. It doesn't have to be a memo box, just something that will store enough text.
Anything example text you could provide when you get time would be great to start me off.
I almost thought your interpretation was a better idea than I originally had for a moment. It wouldn't work though would it because if the tick box didn't have a label, I wouldn't be able to see what it was in the first place to be able to tick it off.
It's inspired me with an idea to maybe make the label text change colour when the check box is selected though?
Say, turn it from white to red. Can your text be edited a bit to make that happen?
The idea here is to append or delete the checkbox's name to a target textbox (which I'll call txtCheckedControls) "Text" property:
' TargetString will be the name of the control we want to add to the existing string.
' The AddString boolean tells us if we're appending (true) or removing (false)
Sub BuildCheckedControlsString(ByVal TargetString As String, ByVal AddString As Boolean)
If (AddString = true)
txtCheckedControls.Text = txtCheckedControls.Text + ", " + TargetString
' This is slightly tricky. We're finding our target text and replacing it with a zero-length string, effectively "deleting" TargetString from txtCheckedControls.Text
txtCheckedControls.Text = Replace(txtCheckedControls.Text, ", " + TargetString, "")
You could do the label thing by using the Controls collection of your current form:
Sub ChangeLabelText(ByVal TargetLabelName As String, byVal Color As Long)
Me.Controls(TargetLabelName).BackColor = Color
*** Note I did not test any of this. It only serves as psuedo code...
Thank you both for your suggestions. Teddy, I'm really struggling to get your code to work, i've been tinkering with it for quite a while now, and I don't really know what I'm supposed to do with it.
But to be honest, I'm preferring this idea of the label text changing colour as you click the check box. It's instant and it's clear to see what you've checked at a glance.
However, from Mark's code it only works to a degree. It changes the label to either red or white, but only upon each click. For instance, if I click it off so it goes white, when I go to the next record, even if this record has the checkbox checked, it isn't red, it is white. It only changes to red again when I click it off, and then on and then every record is red.
Obviously, this is because the event only occurs upon a click on the checkbox, but I'd like it so that every record you see constantly has either red or white labels depending on whether the box for that record is checked or not.
Is that confusing or does it make sense?
Really appreciate the work here guys, I wouldn't have a clue otherwise!!