You stated: You could your code in a sub or function to allow you to set
properties by
making a single call. How would I do that?
First of all, I meant to say the "You could place your code ..." (apologies for
the typo).
Anyway, here is a sample function that demonstrates what I was referring to - I
have placed code to Lock/Unlock specific types of controls in a subroutine that
can be called to perform the locking/unlocking with one line of code. If you
want to try it on more than one form, copy and paste it to a standard module (a
"global" module, not a module behind a form or report):
'***EXAMPLE START
Public Sub subCtlsLockOrNot(frm As Form, bLockState As Boolean)
On Error GoTo subCtlsLockOrNot_ERR
'Make sure that form is not "dirty" or the
' "locking/unlocking" code will fail
If frm.Dirty = True Then
If MsgBox("This form has unsaved data. OK to save?", _
vbYesNo + vbQuestion, "Unsaved Data") = vbYes Then
frm.Dirty = False
Else
MsgBox "The Locked property will not be set.", _
vbOKOnly + vbInformation, "Property Not Set"
Exit Sub
End If
End If
'Declare object variable
Dim ctl As Control
'Cycle through controls
For Each ctl In frm.Controls
Select Case ctl.ControlType 'Find relevant controls
Case acTextBox, acComboBox, acCheckBox
ctl.Locked = bLockState 'Set a property
End Select
Next
subCtlsLockOrNot_EXIT:
Exit Sub
subCtlsLockOrNot_ERR:
MsgBox "Error " & Err.Number & _
" occurred in subCtlsLockOrNot: " & Err.Description
Resume subCtlsLockOrNot_EXIT
End Sub
'***EXAMPLE END
The sub can be called to lock controls of the specified types as follows:
subCtlsLockOrNot Me, True 'from within a form
subCtlsLockOrNot Forms("Form1"), True 'from anywhere
Of course, to unlock the controls, you simple pass a False instead of True as
the second argument.