vb code to enable combo box on selection of checkbox

david.paton

Registered User.
Local time
Today, 09:31
Joined
Jun 26, 2013
Messages
338
I want to enable a combo box upon selection of a checkbox. It is stored in the form called tblChild subform2. The name of the combo box is cboRelationship and the check box is chkRelated.

I am a novice at coding so I looked on the net for an answer and here is what I found and tried to adapt. Problem is that it didn't work either.



Private Sub chkRelated_CheckedChanged(sender As Object, e As EventArgs)
If chkRelated.Enabled = True Then
cboRelationship.Enabled = False
Else
cboRelationship.Enabled = True
End If

End Sub


Could someone help me work out this simple problem please?
 
Code:
Private Sub chkRelated_AfterUpdate()
     Me.cboRelationship.Enabled = Me.chkRelated
End Sub
 
Thanks for that Paul but I am a little unsure as to what I want. Here is my database I am developing. On my tblChild subform2, there is a checkbox to record whether children are related. I originally wanted to have to combo box enable when the checkbox is selected but do you have any suggestions as to what would be a good solution?

If the text box goes from visible to invisible, won't the data still be recorded and won't that be a problem in terms of data redundancy?
 

Attachments

Code:
Private Sub chkRelated_AfterUpdate()
     Me.cboRelationship.Enabled = Me.chkRelated
End Sub

Thanks, this seems to work. The only problem is that when you open the form, all the combo boxes are enabled, regardless of status.
 
You also put in the form's current event:

Private sub form_current()
On error resume next
If not me.newrecord then
Me.cbo.enabled=me.ckbox
Else
Me.cbo.enabled=true
End if
End sub
 
Thanks, this seems to work. The only problem is that when you open the form, all the combo boxes are enabled, regardless of status.

You could save the Form with the combo disabled but it sounds like you might be using Continuous Forms. This strategy won't work in that case because the combos are all instances of the same object and all affected by the code.

The alternative is to use ConditionalFormatting on the combo. (Right click and select ConditionalFormatting.) Change the type to Expression Is and enter the formula.

Code:
chkRelated = True

Enable is one of the things that can be done when the expression is true.
 
I put in this code to the after update event:

If Me.chkRelated = "True" Then
Me.cboRelationship.Visible = True
Else
Me.cboRelationship.Visible = False
End If

With that, they all appear, even with one box selected then if I try and select one box, they all disappear. I have attached my db.

Thanks
 

Attachments

You also put in the form's current event:

Private sub form_current()
On error resume next
If not me.newrecord then
Me.cbo.enabled=me.ckbox
Else
Me.cbo.enabled=true
End if
End sub


Thanks for that. It slightly works. If I select a checkbox, the others get grayed out and you can only select that combo box , none of the others but if I have just all the check boxes unselected, it allows me to select them all.
 
ThaIf I select a checkbox, the others get grayed out and you can only select that combo box , none of the others but if I have just all the check boxes unselected, it allows me to select them all.

You need to use ConditionalFormatting as I explained in my previous post.
 
I tried using conditional formatting and all it seems to have done is that the boxes are now blue but they can still be selected, even when the checkbox is not selected. Maybe I just need to add an option to tblRelated of Not Related?
 
I tried using conditional formatting and all it seems to have done is that the boxes are now blue but they can still be selected, even when the checkbox is not selected. Maybe I just need to add an option to tblRelated of Not Related?

Change the Enabled Property of the combo to No in the Design View. (Data tab of Properties).

In the ConditionalFormatting rule there is a small rectangle icon just to the right of the FontColor selector. Click it so it is highlighted. This will Enable the combo when the rule is True.
 

Users who are viewing this thread

Back
Top Bottom