Damon's code has the right idea, but it requires you set the enabled/disabled property for each of your many textboxes, one at a time. It also won't set the enabled/disabled property correctly as you move from record to record. This needs to be done in the Form_Current event.
This may sound and look complicated at first, but it really isn't, especially compared with the alternative, which is to deal with the status of each and every one of your 30 textboxes separately! In order to do this you need to use the Tag Property of your textboxes to tell Access which ones to make enabled /greyed out depending on the status of Condition1.
Note: You have referred to your checkbox as Condition1 in one of your posts and Condition
al1 in another! I went with
Condition1 for this code; just make sure the the actual name of the checkbox matches the references in the code.
To set the Tag property of a textboxes:
Select all the textboxes you want to be affected by holding down <
Shift> and clicking on each one.
Click on Properties
Click on Other
In the Tag Property box enter
Cond1 (no quotation marks!)
While you’ve got them all selected, you can also set Enabled to No for all of them, so when the form opens they’ll start out disabled.
Now what this routine does, basically, is check to see whether Condition1 is checked or not, loop thru your textboxes, seeing if they have the Tag Property set to
Cond1 and setting them to Enabled = True or Enabled = False, dependng on the status of the checkbox. If the user makes a mistake and has to change Condition1 , the textboxes will change accordingly.
Select the Condition1 checkbox
Right click and click on
Properties
Click on
Events
Click to the right of the box for the
OnClick Property
Click on the ellipsis (
...)
Click on
Code Builder
You’re now in the VBA Editor! You should see this sub:
Private Sub Condition1_Click()
End Sub
Copy and paste this code between these two lines:
'This sets the textboxes to Enabled or not Enabled immediately
- Dim ctrl As Control
-
-
If Condition1 Then 'If the box is checked
-
For Each ctrl In Me.Controls
-
If ctrl.ControlType = acTextBox Then
-
If ctrl.Tag = "Cond1" Then
-
ctrl.Enabled = True
-
End If
-
End If
-
Next
-
-
Else 'If the box is not checked
-
For Each ctrl In Me.Controls
-
If ctrl.ControlType = acTextBox Then
-
If ctrl.Tag = "Cond1" Then
-
ctrl.Enabled = False
-
End If
-
End If
-
Next
-
End If
-
While you’re in the VBA Editor, copy and paste this code also. It does the same check as the previous code, but it does it each time you move from one record to another, so that the Enabled status of each textbox is set properly.
- Private Sub Form_Current()
-
Dim ctrl As Control
-
-
If Condition1 Then
-
For Each ctrl In Me.Controls
-
If ctrl.ControlType = acTextBox Then
-
If ctrl.Tag = "Cond1" Then
-
ctrl.Enabled = True
-
End If
-
End If
-
Next
-
-
Else
-
For Each ctrl In Me.Controls
-
If ctrl.ControlType = acTextBox Then
-
If ctrl.Tag = "Cond1" Then
-
ctrl.Enabled = False
-
End If
-
End If
-
Next
-
End If
-
End Sub
-
Click on the “Save” button and exit the Editor. Run the form and try the checkbox out.
Note: If the lines for the code appear when you copy and paste this into the code editor, be sure to delete them, along with the following period.
Good Luck!
Linq
;0)>