Controls collection question

B

Biggles

I am trying to call this in an after update event, but keep getting a run
Time Error 438, object doesn't support this property or method. I thought I
had done this before, what am I doing wrong.

Public Sub lockctl()

Dim ctl As Control

If DateDiff("d", Me.Date_Closed, Now()) > 45 Then
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl
End If

End Sub
 
B

Biggles

I wish I could edit my original post. I am trying to lock all the fields on
the form detail section, textboxes, check boxes, combo boxes, etc. but not
command buttons. Thought that might help.

Sean
 
D

Dirk Goldgar

In
Biggles said:
I am trying to call this in an after update event, but keep getting a
run Time Error 438, object doesn't support this property or method.
I thought I had done this before, what am I doing wrong.

Public Sub lockctl()

Dim ctl As Control

If DateDiff("d", Me.Date_Closed, Now()) > 45 Then
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl
End If

End Sub

Only data-bindable controls have a Locked property. For example, labels
don't, and lines don't. You could test the type of each control before
checking the property, but it's easiest to just ignore errors in that
block of code:

If DateDiff("d", Me.Date_Closed, Now()) > 45 Then

On Error Resume Next
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl

On Error GoTo Err_Handler ' modify name of handler
End If
 
D

Douglas J. Steele

Not all controls can be locked. For example, Labels and Lines can't be.

Try:

Public Sub lockctl()

Dim ctl As Control

If DateDiff("d", Me.Date_Closed, Now()) > 45 Then
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acCommandButton, acOptionButton, acCheckBox, _
acOptionGroup, acTextBox, acListBox, acComboBox, _
acSubform, acToggleButton
ctl.Locked = True
End Select
Next ctl
End If

End Sub

(I think that's all of them)
 
D

Dirk Goldgar

In
Biggles said:
I wish I could edit my original post. I am trying to lock all the
fields on the form detail section, textboxes, check boxes, combo
boxes, etc. but not command buttons. Thought that might help.

Command buttons don't have a Locked property. See my other reply.
 
M

Marshall Barton

Biggles said:
I am trying to call this in an after update event, but keep getting a run
Time Error 438, object doesn't support this property or method. I thought I
had done this before, what am I doing wrong.

Public Sub lockctl()

Dim ctl As Control

If DateDiff("d", Me.Date_Closed, Now()) > 45 Then
For Each ctl In Me.Controls
ctl.Locked = True
Next ctl
End If

End Sub


To selectively lock a "group" of controls, you need to use
something to identify the controls in the group. A heavy
handed way would be to lock all controls by their type:

For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextbox, acCombobox, acCheckbox, . . .
ctl.Locked = True
End Select
Next ctl

A more selective approach would be to set the Tag property
of just the controls you want to lock to something like
LOCK:

For Each ctl In Me.Controls
If ctl.Tag = "LOCK" Then
ctl.Locked = True
End If
Next ctl
 

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