Require selection from combo on checkbox?

S

Steve B

Hi - is it possible to set a form to require a selection from a combo if a
check box is set to true?

I've tried using the validation rule set to the check box name, but it
doesn't work...
 
J

Jeff Boyce

Steve

Since we're not there, we don't have any way to know HOW you "tried using
the validation rule ..."

If this were mine, I'd add a validation check in the form's BeforeUpdate
event that ensured that there was a selection made in the combobox if the
checkbox were true (and "Cancel"ed the update if there wasn't).

By the way, you can make it easier for your users to understand that they
need to do something with the combobox if you will add something like the
following to the AfterUpdate event of the checkbox:

Me!YourComboboxName.Enabled = (Me!YourCheckbox = True)

This will enable/disable the combobox, depending on the status of the
checkbox.

You could even use:
Me!YourComboboxName.SetFocus
if the checkbox were true, to remind the user that they need to make a
selection.

Good Luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
S

Steve B

Hi Jeff,

Appolgies for the lack of info, I should no better as a whine to staff for
lack of detail!

I've added what I want for the time being by hiding the combo box by using
the following in the form On current event

Private Sub Form_Current()
Me!part1.Visible = Me!Parts_requested
End Sub

and then on the check box After update event

Private Sub Parts_requested_AfterUpdate()
Me!part1.Visible = Me!Parts_requested
End Sub

But will also investigate your helpful suggestions

Thank you for your time

Steve
 
J

John W. Vinson

Hi - is it possible to set a form to require a selection from a combo if a
check box is set to true?

I've tried using the validation rule set to the check box name, but it
doesn't work...

Are you using a Form (good) or a table (much less good)? You can do the check
in a Form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!checkboxname = True Then
If IsNull(Me!comboboxname) Then
Msgbox "If the checkbox is checked you must select an item", vbOKOnly
Cancel = True
Me!comboboxname.SetFocus
End If
End If
End Sub

If you're using a table... well, don't; but if you are, you'll need to use the
Table Validation rule, not field validation (which cannot refer to other
fields). E.g.

([checkboxname] = True AND [combobox] IS NOT NULL) OR ([checkboxname] = False)
 
S

Steve B

Cheers John & Jeff,

Used both your solutions and everything is now perfect!

Steve

John W. Vinson said:
Hi - is it possible to set a form to require a selection from a combo if a
check box is set to true?

I've tried using the validation rule set to the check box name, but it
doesn't work...

Are you using a Form (good) or a table (much less good)? You can do the
check
in a Form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!checkboxname = True Then
If IsNull(Me!comboboxname) Then
Msgbox "If the checkbox is checked you must select an item", vbOKOnly
Cancel = True
Me!comboboxname.SetFocus
End If
End If
End Sub

If you're using a table... well, don't; but if you are, you'll need to use
the
Table Validation rule, not field validation (which cannot refer to other
fields). E.g.

([checkboxname] = True AND [combobox] IS NOT NULL) OR ([checkboxname] =
False)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top