Okay, if that's what you are after, one of the problems is that setting the
AllowEdits affects the unbound controls as well as the bound ones.
That usually works out to be a problem for me, so I use the function below
with a command button that toggles between Lock and Unlock the form. To lock
the form, call:
Call LockBoundControls(Me, True)
and to unlock it, use False for the last argument.
The code loops through all controls on the form, and identifies those bound
to a field (not unbound, nor bound to an expression, nor without a Control
Source), and it toggles the Locked property of those controls. If it finds a
subform, the function calls itself recursively so that it handles any level
of subforms as well.
If there are some controls that should not be locked, you can pass their
names as optional arguments, e.g.:
Call LockBoundControls(Me, True, "Text1", "Sub1")
If the exception is a subform, then no processing occurs for lower levels of
that subform either.
Re your question about AllowEdits, a Form_AfterUpdate won't occur when there
are no updates, *unless* the form was already dirty at the time when you set
AllowEdits to No. In that case, MS decided it was best to let you finish
editing and save before applying the property--probably a good choice. The
code below therefore explicitly saves before allowing the toggle to
AllowEdits, to avoid that confusion.
As a visual clue for the user, I also place thin-boundary red rectangle
around the edge of the form, and set its Visible property to True when the
form is locked. I've found this helps the user remember why "the stupid
respond won't respond" (i.e. it's currently locked.)
Hope it helps. Watch the line-wraps.
----------------------------code starts------------------
Public Function LockBoundControls(frm As Form, _
bLock As Boolean, ParamArray avarExceptionList())
On Error GoTo Err_Handler
'Purpose: Lock the bound controls and prevent deletes on the form any
its subforms.
'Arguments frm = the form to be locked
' bLock = True to lock, False to unlock.
' avarExceptionList: Names of the controls NOT to lock
(variant array of strings).
'Usage: Call LockBoundControls(Me. True)
Dim ctl As Control 'Each control on the form
Dim lngI As Long 'Loop controller.
Dim bSkip As Boolean
'Save any edits.
If frm.Dirty Then
frm.Dirty = False
End If
'Block deletions.
frm.AllowDeletions = Not bLock
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acOptionGroup, _
acCheckBox, acOptionButton, acToggleButton
'Lock/unlock these controls if bound to fields.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If HasProperty(ctl, "ControlSource") Then
If Len(ctl.ControlSource) > 0& And Not ctl.ControlSource
Like "=*" Then
If ctl.Locked <> bLock Then
ctl.Locked = bLock
End If
End If
End If
End If
Case acSubform
'Recursive call to handle all subforms.
bSkip = False
For lngI = LBound(avarExceptionList) To
UBound(avarExceptionList)
If avarExceptionList(lngI) = ctl.Name Then
bSkip = True
Exit For
End If
Next
If Not bSkip Then
If Len(Nz(ctl.SourceObject, vbNullString)) > 0& Then
ctl.Form.AllowDeletions = Not bLock
ctl.Form.AllowAdditions = Not bLock
Call LockBoundControls(ctl.Form, bLock)
End If
End If
Case acLabel, acLine, acRectangle, acCommandButton, _
acTabCtl, acPage, acPageBreak, acImage, acObjectFrame
'Do nothing
Case Else
'Includes acBoundObjectFrame, acCustomControl
Debug.Print ctl.Name & " not handled on " & conMod & " at " &
Now()
End Select
Next
Exit_Handler:
Set ctl = Nothing
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume Exit_Handler
End Function
----------------------------code starts------------------