lock all fields

G

Guest

I want to lock all fields in a form with an if statment. tried code below
(error) and allowedits = false (did nothing) can someone help. thx.

Private Sub Form_Open(Cancel As Integer)
If (Me!Render = True And Me!DateService < Date) Then
Me!Locked = True
MsgBox "Form is Locked"
Else
Me!Locked = False
End If
End Sub
 
K

Ken Snell \(MVP\)

It's much better to use a form's Load event to manipulate controls on a
form; during the Open event, some of the controls may not be instantiated
yet and you'll get errors.

You cannot change the Locked property of every control on a form using a
single code step. You must loop through all the controls and lock each one
individually:

Private Sub Form_Load()
Dim blnLock As Boolean
Dim ctl As Control
On Error Resume Next
blnLock = (Me!Render = True And Me!DateService < Date)
For Each ctl In Me.Controls
ctl.Locked = blnLoc
Err.Clear
Next ctl
If blnLock = True Then MsgBox "Form is Locked"
Exit Sub ' to clear Err cache
End Sub
 
G

Guest

A form (Me) does not have Lock property. You have to be specific of which
field in Me you want to lock or iterate through the collection of Text boxes
(or any other controls that accept data entry) and lock them individually.
For instance

dim ctl as Control
For Each ctl In Me.Controls ' Collection of controls on a form
' Check to see if control is text box or a combo box
If (ctl.ControlType = acTextBox ) Or _
(ctl.ControlType = acComboBox) Then
If ctl.Enabled <> False Then
' Set control properties. - will toggle the accessibility
into the control
With ctl
.Locked = Not (.Locked) ' toggle with the NOT
operator
.Enabled = Not (.Enabled) ' toggle with the NOT
operator
End With
End If
Next ctl

Be sure not to be in a control when it is set to disabled (grayed mode) or
you will receive an error message. In other words, you can not disable a
control when it has a focus.
 
G

Guest

can one do the equivalent of locking all fields by putting the form's 3 key
controls to false?
AllowEdits
AllowAdditions
AllowDeletions

?? am wondering on this - it would seem significantly simpler than looping
thru all controls for individual locking yet achieve the same end.....

am also always confused on when to use the OnLoad vs OnOpen....is there a
primer guideline somewhere on this??
 
K

Ken Snell \(MVP\)

Setting these form properties to False *may* accomplish your goal, but they
also affect whether new records can be shown and whether any records are
shown (if the form is filtered and there are no records to be shown based on
that filter). You can try it, but again I would do it in the form's Load
event.

Use Open when you want to set the form's RecordSource property or
potentially want to cancel the open event based on some parameter other than
a value in a form's controls or fields. Often, you need to do "trial and
error" if you really think you need to use the Open event -- and even then,
I've had the experience of the Open event being ok one time and not the
next.

Use of these events are discussed in most books about ACCESS. The Help file
also discusses their use to some extent.
 
G

Guest

Tried the code Ken and it compiled but didn't lock the fields for some
reason. Anyway, I decided just to permanenetly lock some of the fields and
used the code below for the ones that needed to be on/off. Thanks for the
help. Ian.

Private Sub Form_Load()
If (Me!Render = True And Me!DateService < Date) Then
Me!Render.Locked = True
Me!DateService.Locked = True
MsgBox "Form is Locked. Crew Info can be edited"
Else
Me!Render.Locked = False
Me!DateService.Locked = False
End If
End Sub
 

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