Controls Object - Access 2k

  • Thread starter Thread starter Leonard Priestley
  • Start date Start date
L

Leonard Priestley

There have been several occasions when I have found it helpful to check the
collection of controls on a form, looking for any with a particular tag.
This has always worked for me before, but now I have struck a problem. In
the same database I have two forms where I have used this technique in the
same way, and for the same reason. That is: I want to lock some text and
combo boxes and not others, and I want to indicate they are locked by
changing their backcolor. The technique works on one form and produces the
error message "Object doesn't support this property or method" on the other
form. I have spent ages comparing the forms and I can't see anything to
account for this. In fact, the code (quoted below) was copied and pasted
from the form that works to the form with the problem. I'm wondering if the
code I have used is cutting corners in some way that matters on the second
form.

This is the code I am using:

Private Sub LockBox()
'Lock any controls tagged "Lockable"
Dim L As Control

For Each L in Me.Controls
If L.Tag = "Lockable" Then
L.Locked = True
L.BackColor = 15461355
End If
Next

End Sub

The line L.Locked = True seems to work fine, but the line L.BackColor =
15461355 causes the error message. Yet I have used this code to change
both forecolor and backcolor in other forms and have had no problems. Am I
missing out on some aspect of using the control object?

Leonard Priestley
 
When it errors, choose Debug. Open the Immediate Window (Ctrl+G), and ask
Access which control is going the problem, i.e.:
? L.Name

Some controls (such as a command button) don't have a BackColor property, so
you might need to limit your code based on ControlType.
 
Allen Browne,
Yes, you put your finger on it. I was so hypnotised by a screen with lots
of text boxes that I was ignoring three little checkboxes that, as you say,
do not have a backcolor property. Your suggestion of checking the name of
the control seems so simple, but it had not occurred to me, and it would
have saved a lot of time. I think I will just put a different tag on the
checkboxes and write code that says to lock them, but doesn't bother about
color. Anyway, it's sorted out and I am most grateful. Have a nice day.

Leonard Priestley
 

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

Back
Top