alex said:
Can anyone tell me how to refer to a group of controls once, e.g.,
give them a name; and then only have to refer to that name in the
future?
I've done this myself a number of times in the past. You want to use the Tag
property of a control. Every control that should be locked will have a value in
there such a "Lock"
Then in the Open event you pass in the OpenArgs from the calling form telling the
form if this is a new record.
If Me.OpenArgs = "New" Then
Call EnOrDis_AbleControlsOnForms(Me, tglLocked, True)
Else
Call EnOrDis_AbleControlsOnForms(Me, tglLocked, False)
End If
I also added a Lock/Unlock command button which has the following code
Call EnOrDis_AbleControlsOnForms(Me, tglLocked, tglLocked.Value)
This routine is in turn in a public module. It's a generic routine that will work
on any form.
Public Sub EnOrDis_AbleControlsOnForms(frm As Form, tgl As Control, _
Optional Override As Boolean)
' Enable & unlock or disable and lock controls as required. Controls must
; have Lock in the tag
' tgl is assumed to be a toggle control. Note that the caption will be overwritten
' If Override value is true then the field will be unlocked
Dim ctl As Control, ctlsbf As Control
Dim Locked As Boolean
On Error GoTo tagError
If Not IsMissing(Override) Then
Locked = Override
Else
Locked = tgl.Value
End If
For Each ctl In frm.Controls
If ctl.Tag = "Lock" Then
ctl.Enabled = Locked
ctl.Locked = Not Locked
End If
If ctl.ControlType = acSubform Then
For Each ctlsbf In ctl.Form.Controls
If ctlsbf.Tag = "Lock" Then
ctlsbf.Enabled = Locked
ctlsbf.Locked = Not Locked
End If
Next ctlsbf
End If
Next ctl
If Locked Then
tgl.Caption = "Unlocked"
Else
tgl.Caption = "Locked"
End If
On Error GoTo 0
Exit Sub
tagError:
Select Case Err.Number
' This message happens on a subform control for unknown reasons
' something to do with the subform <shrug>
Case 2164 ' You can't disable a control while it has the focus.
' ignore
Resume Next
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
tglLocked_Click of VBA Document Form_Transaction Header"
End Select
Exit Sub
Resume
End Sub
And thanks for reminding me that I should create a web page on this topic.
Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog -
http://msmvps.com/blogs/access/