How to grey out a other input fields if a checkbox is true?

  • Thread starter Thread starter Roadwarrior1
  • Start date Start date
R

Roadwarrior1

I am working on a project that I want only fields with information to print
out for each contact. So if I answer yes, true in a checkbox other fields
will grey out and not print because there are no input strings. Can anyone
help?
 
You don't say how you are printing the contract. Are you printing the form
or are you printing a report? The latter is far preferable, and if you are
not using a report I'd urge you to do so, but I'll deal with the former
first, just in case:

1. In the check box's AfterUpdate event procedure change the ForeColor and
BackColor properties of the relevant controls like so:

Const conGREY = 12632256

If Me.chkYourCheckBox Then
Me.SomeControl.ForeColor = conGREY
Me.SomeControl.BackColor = conGREY
Me.SomeControl.Enabled = False
Me.SomeOtherControl.ForeColor = conGREY
Me.SomeOtherControl.BackColor = conGREY
Me.SomeOtherControl.Enabled = False
' and so on
Else
Me.SomeControl.ForeColor = vbBlack
Me.SomeControl.BackColor = vbWhite
Me.SomeControl.Enabled = True
Me.SomeOtherControl.ForeColor = vbBlack
Me.SomeOtherControl.BackColor = vbWhite
Me.SomeControl.Enabled = True
' and so on
End If

Note that there is no built in constant for grey, so you have to declare one.

You haven't said whether the check box is bound to a column (field) in the
underlying table or is an unbound control. If it is a bound control also put
the same code in the form's Current event procedure:

2. If it’s a report that's being printed then if the checkbox is a bound
control put the following slightly shortened version of the code as above in
the report's detail section's Format event procedure. You'll need to include
the checkbox control in the detail section, but if you don't want it to show
then set its Visible property to False:

Const conGREY = 12632256

If Me.chkYourCheckBox Then
Me.SomeControl.ForeColor = conGREY
Me.SomeControl.BackColor = conGREY
Me.SomeOtherControl.ForeColor = conGREY
Me.SomeOtherControl.BackColor = conGREY
' and so on
Else
Me.SomeControl.ForeColor = vbBlack
Me.SomeControl.BackColor = vbWhite
Me.SomeOtherControl.ForeColor = vbBlack
Me.SomeOtherControl.BackColor = vbWhite
' and so on
End If

If the checkbox is an unbound control on an open form then you'd need to
reference the control in the code for the report's detail event procedure:

Const conGREY = 12632256

If Forms("YourFormNameGoesHere").chkYourCheckBox Then
Me.SomeControl.ForeColor = conGREY
Me.SomeControl.BackColor = conGREY
Me.SomeOtherControl.ForeColor = conGREY
Me.SomeOtherControl.BackColor = conGREY
' and so on
Else
Me.SomeControl.ForeColor = vbBlack
Me.SomeControl.BackColor = vbWhite
Me.SomeOtherControl.ForeColor = vbBlack
Me.SomeOtherControl.BackColor = vbWhite
' and so on
End If

This would then grey out the controls for every record returned by the
report of course. For selectively greying out or showing controls record by
record you'd need a Boolean (Yes/No) column in the underlying table, to which
the check box would be bound.

On the basis of the limited information you've given its difficult to be
categorical about this, but you might not need the checkbox or a Boolean
column in the table at all. If its purely the presence or absence of a value
in a control which determines whether you want to show it or grey it out then
in a report's Format event procedure you can do so with:

Const conGREY = 12632256

If IsNull(Me.SomeControl) Then
Me.SomeControl.BackColor = conGREY
Else
Me.SomeControl.BackColor = vbWhite
End If

If IsNull(Me.SomeOtherControl) Then
Me.SomeOtherControl.BackColor = conGREY
Else
Me.SomeOtherControl.BackColor = vbWhite
End If
' and so on

Note that you don't need to bother about the ForeColor property for this as
there'll be no value to show when the control is Null.

If you are not familiar with using event procedures this is how its done:

Select the form, control or report section as appropriate in form or report
design view and open its properties sheet if its not already open. Then
select the relevant event property in the properties sheet. Click on the
'build' button; that's the one on the right with 3 dots. Select 'Code
Builder' in the dialogue, and click OK. The VBA window will open at the
event procedure with the first and last lines already in place. Enter the
lines of code between these two existing lines.

Ken Sheridan
Stafford, England
 
Yes, it is a bound field. I is a input field so if the checkbox is selected
then other fields will grey out.
 
You should be able to handle that scenario with what I said in my original
reply. Post back if you need any more help.

Ken Sheridan
Stafford, England
 
You need to tackle this at two stages; firstly when the user first navigates
to each record on the form, secondly when they update the control bound to
the 'Yes or No' field. It also makes a difference whether the user moves to
an empty new record or to an existing one.

To cater for when the user moves to a record put code in the form's Current
event procedure. For this example I'll assume the 'Yes or No' field is
called MyYesNo and the field in which data is to be entered if Yes is
answered is called MyYesText, and if No is answered is called MyNoText. So
the Current event procedure's code would go like this:

' first determine if the user has moved to a
' new or existing record
If Me.NewRecord Then
' in the case of a new record disable both text boxes
' until user selects 'yes' or 'no'
Me.[MyYesText].Enabled = False
Me.[MyNoText].Enabled = False
Else
' if its an existing record disable the relevant
' control(s) on the basis of the value of MyYesNo
Me.[MyYesText].Enabled = (Nz([MyYesNo],"No") = "Yes")
Me.[MyNoText].Enabled = (Nz([MyYesNo],"Yes") = "No")
EndIf

The code in the AfterUpdate event procedure of the MyYesNo control is the
same as that in the last part of the above, but you also have to cater for a
user changing their mind, e.g. answering 'yes' and entering something in
MyYesText, then going back and answering 'no', or vice versa, so you need to
delete (set to Null) what they might have already entered in either. You
also need to allow for the possibility, albeit a remote one, of a user
setting the combo box to Null, in which case both text boxes should be
disabled until they select 'yes' or 'no'. You can use the Nz function to
return "Yes" or "No" if the combo box is Null, thus forcing the expression to
evaluate to False and disable the text box. So the code would be:

Me.[MyYesText] = Null
Me.[MyNoText] = Null
Me.[MyYesText].Enabled = (Nz([MyYesNo],"No") = "Yes")
Me.[MyNoText].Enabled = (Nz([MyYesNo],"Yes") = "No")

Ken Sheridan
Stafford, England
 
Back
Top