Lock form fields

  • Thread starter Thread starter Howard
  • Start date Start date
H

Howard

I have a subform which allows the user to browse through
labour records for a given task (parent form). Regarding
the subform alone, is it possible to lock the fields
within the subform based on the value of a field within
the subform. For example, I have a status field on the
labour subform. If the status is set to "Work Order
Issued", that record should then be locked so the user can
no longer make changes. While scrolling through, the user
can make changes to other records with different status
values. Please let me know. Thanks!
-Howard
 
There are a variety of ways to do this in a form - each one using the
Current Event of the form to check a status field or whatever field you have
that indicates whether the record is to be locked. You also need to execute
this code in the AfterUpdate event of the form to ensure that the record is
locked after the status changes. I've shown this code as event code but in
reality I would put it in separate procedures within the form's class module
and then call that procedure form both the Current Event and the AfterUpdate
event.

The easiest (though it has drawbacks) is to toggle the AllowEdits property
of the form. In the following, the record will be 'locked' when the
laborStatus="Work Order Issued". The drawback to using the AllowEdits
property of the form is that it also locks unbound controls when it is
false. This means that an unbound control used for locating a record is
locked when the rest of the form is locked. Not ideal in many cases.

Private Sub Form_Current()
Me.AllowEdits = Me.laborStatus="Work Order Issued"
End Sub

Note that the above is a shortcut for the following

Private Sub Form_Current()
if Me.laborStatus="Work Order Issued" then
me.allowedits=true
else
me.allowedits=false
end if
End Sub

The other way is to toggle the Locked property of individual controls based
on the value of InvoiceStatus. Put some keyword in the Tag property of each
control that should be conditionally locked then loop through all the
controls and toggle the locked property of the relevant controls. For
example - using the keyword "ToggleLock"

Private Sub Form_Current()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "ToggleLock" Then
ctl.Locked = Me.laborStatus="Work Order Issued"
End If
Next ctl
Set ctl = Nothing
End Sub
 
Thanks! In my case, the subform does not contain any
unbound controls so I think I'll utilize the first method
to toggle the AllowEdits property. If I have problems I'll
reply again. Thanks for everything!
-Howard
-----Original Message-----
There are a variety of ways to do this in a form - each one using the
Current Event of the form to check a status field or whatever field you have
that indicates whether the record is to be locked. You also need to execute
this code in the AfterUpdate event of the form to ensure that the record is
locked after the status changes. I've shown this code as event code but in
reality I would put it in separate procedures within the form's class module
and then call that procedure form both the Current Event and the AfterUpdate
event.

The easiest (though it has drawbacks) is to toggle the AllowEdits property
of the form. In the following, the record will be 'locked' when the
laborStatus="Work Order Issued". The drawback to using the AllowEdits
property of the form is that it also locks unbound controls when it is
false. This means that an unbound control used for locating a record is
locked when the rest of the form is locked. Not ideal in many cases.

Private Sub Form_Current()
Me.AllowEdits = Me.laborStatus="Work Order Issued"
End Sub

Note that the above is a shortcut for the following

Private Sub Form_Current()
if Me.laborStatus="Work Order Issued" then
me.allowedits=true
else
me.allowedits=false
end if
End Sub

The other way is to toggle the Locked property of individual controls based
on the value of InvoiceStatus. Put some keyword in the Tag property of each
control that should be conditionally locked then loop through all the
controls and toggle the locked property of the relevant controls. For
example - using the keyword "ToggleLock"

Private Sub Form_Current()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "ToggleLock" Then
ctl.Locked = Me.laborStatus="Work Order Issued"
End If
Next ctl
Set ctl = Nothing
End Sub


--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

I have a subform which allows the user to browse through
labour records for a given task (parent form). Regarding
the subform alone, is it possible to lock the fields
within the subform based on the value of a field within
the subform. For example, I have a status field on the
labour subform. If the status is set to "Work Order
Issued", that record should then be locked so the user can
no longer make changes. While scrolling through, the user
can make changes to other records with different status
values. Please let me know. Thanks!
-Howard


.
 
Back
Top