Check box to control editability of certain fields in a form

A

Allen Browne

Use the AfterUpdate event procedure of the text box to set the Locked
property of the other controls:
Private Sub chk1_AfterUpdate
Dim bLock as Boolean
bLock = Nz(Me.chk1, False)
Me.[Text0].Locked = bLock
Me.[Text3].Locked = bLock
'etc.
End Sub

In the Current event procedure of the form, call the same code so it is
applied when you move record:
Private Sub Form_Current()
Call chk1_AfterUpdate
End Sub

This assumes the check box is bound to a yes/no field in your table.
 
J

John W. Vinson

Is this possible? How can I create such a check box?

Yes. Use the checkbox tool on the toolbox to add an unbound checkbox control
to the form.

View the checkbox's Properties (I'll call it chkEditable) and find the
AfterUpdate property. Click the ... icon and select Code Builder. Edit the
code to

Private Sub chkEditable_AfterUpdate() ... Access gives you this line free
Me!txtThis.Enabled = Me!chkEditable
Me!cboThat.Enabled = Me!chkEditable
<and so on>
End Sub

to set the Enabled property of the control to the selected value of the
checkbox.

You may want to put a yes/no Editable field in the table, and put similar code
in the form's Current event, if you want the editability to persist with the
stored record.

John W. Vinson [MVP]
 
N

Nosne

I'm a beginner so would you mind explaining in detail the parts of the code.
I do have a yes/no field on the table. I want this to control the
editability of 9 other fields on the table. The table is about clients and
checking this box will allow me to put information into these other fields
when the answer to the paticular check box question is yes.

thank you in advance for your helpful reply.

Allen Browne said:
Use the AfterUpdate event procedure of the text box to set the Locked
property of the other controls:
Private Sub chk1_AfterUpdate
Dim bLock as Boolean
bLock = Nz(Me.chk1, False)
Me.[Text0].Locked = bLock
Me.[Text3].Locked = bLock
'etc.
End Sub

In the Current event procedure of the form, call the same code so it is
applied when you move record:
Private Sub Form_Current()
Call chk1_AfterUpdate
End Sub

This assumes the check box is bound to a yes/no field in your table.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Nosne said:
Is this possible? How can I create such a check box?
 
A

Allen Browne

Set the On Click event procedure of your check box to:
[Event Procedure]

Click the Build button (...) beside this.
Access opens the code window, and gives you the first and last lines
("Private Sub..." and "End Sub".)

Paste the code in between.

Replace "chk1" with the name of your check box.

Replace "Text0", "Text3", etc with the name of your 9 controls (repeating,
so you will have 9 lines.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Nosne said:
I'm a beginner so would you mind explaining in detail the parts of the
code.
I do have a yes/no field on the table. I want this to control the
editability of 9 other fields on the table. The table is about clients
and
checking this box will allow me to put information into these other fields
when the answer to the paticular check box question is yes.

thank you in advance for your helpful reply.

Allen Browne said:
Use the AfterUpdate event procedure of the text box to set the Locked
property of the other controls:
Private Sub chk1_AfterUpdate
Dim bLock as Boolean
bLock = Nz(Me.chk1, False)
Me.[Text0].Locked = bLock
Me.[Text3].Locked = bLock
'etc.
End Sub

In the Current event procedure of the form, call the same code so it is
applied when you move record:
Private Sub Form_Current()
Call chk1_AfterUpdate
End Sub

This assumes the check box is bound to a yes/no field in your table.

Nosne said:
Is this possible? How can I create such a check box?
 
N

Nosne

I have one more problem...It seems to be working opposite the way i want it
to. I want it to be blocked when the box is unchecked.

thank you in advance for your help

Allen Browne said:
Set the On Click event procedure of your check box to:
[Event Procedure]

Click the Build button (...) beside this.
Access opens the code window, and gives you the first and last lines
("Private Sub..." and "End Sub".)

Paste the code in between.

Replace "chk1" with the name of your check box.

Replace "Text0", "Text3", etc with the name of your 9 controls (repeating,
so you will have 9 lines.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Nosne said:
I'm a beginner so would you mind explaining in detail the parts of the
code.
I do have a yes/no field on the table. I want this to control the
editability of 9 other fields on the table. The table is about clients
and
checking this box will allow me to put information into these other fields
when the answer to the paticular check box question is yes.

thank you in advance for your helpful reply.

Allen Browne said:
Use the AfterUpdate event procedure of the text box to set the Locked
property of the other controls:
Private Sub chk1_AfterUpdate
Dim bLock as Boolean
bLock = Nz(Me.chk1, False)
Me.[Text0].Locked = bLock
Me.[Text3].Locked = bLock
'etc.
End Sub

In the Current event procedure of the form, call the same code so it is
applied when you move record:
Private Sub Form_Current()
Call chk1_AfterUpdate
End Sub

This assumes the check box is bound to a yes/no field in your table.

Is this possible? How can I create such a check box?
 
N

Nosne

What is the code to make the labels and the fields disappear from the form
when the yes/no check box is unchecked? Will the disappearance be immediate
or do I have to refresh?

Thank you for your previous replies which have been of great help to me.
 
A

Allen Browne

Change the Visible property of the controls instead of the Locked property.

That will trigger an error if you try to hide the control that has the
focus, so you will need to use error handling.
 
N

Nosne

How can I cause the field and the associated label to turn grey when the box
is unchecked? I want a way to visually indicate that the particular fields
do not apply to records with the said box unchecked.
 
J

John W. Vinson

How can I cause the field and the associated label to turn grey when the box
is unchecked?

The Enabled property is what you want for that.

John W. Vinson [MVP]
 
J

John W. Vinson

what changes do I make to the codes?

Instead of Me.controlname.Locked = <something> use Me.controlname.Enabled =
<something>.

For more specific help please post your current code.

John W. Vinson [MVP]
 
N

Nosne

Private Sub PRO_Click()
Dim block As Boolean
block = Not Nz(Me.PRO, False)
Me.INT.Locked = block
Me.INT_EXP.Locked = block
Me.PS.Locked = block
Me.PS_EXP.Locked = block
Me.Ctl5TH_AD_START.Locked = block
Me.Ctl5TH_AD_END.Locked = block
Me.Ctl5TH_AD_TYPE.Locked = block
Me.Ctl6TH_AD.Locked = block
Me.Ctl6TH_AD_EXP.Locked = block
Me.Ctl6TH_AD_TYPE.Locked = block
End Sub

this is my current setup. what changes do i need to get the greyd out effect?
 
D

Douglas J. Steele

I thought John was pretty explicit when he said "Instead of
Me.controlname.Locked = <something> use Me.controlname.Enabled =
<something>." The only thing he didn't point out was that if you're setting
Locked to True, you'd want to set Enabled to False.

In other words,

Private Sub PRO_Click()
Dim block As Boolean
block = Nz(Me.PRO, False)
Me.INT.Enabled = block
Me.INT_EXP.Enabled = block
Me.PS.Enabled = block
Me.PS_EXP.Enabled = block
Me.Ctl5TH_AD_START.Enabled = block
Me.Ctl5TH_AD_END.Enabled = block
Me.Ctl5TH_AD_TYPE.Enabled = block
Me.Ctl6TH_AD.Enabled = block
Me.Ctl6TH_AD_EXP.Enabled = block
Me.Ctl6TH_AD_TYPE.Enabled = block
End Sub

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)
 
Top