how do i determine if a control has the LOCKED property?

K

Keith G Hicks

I need to determine if a control has the "Locked" property during code
execution as follows:

Dim ctl as Control
For Each ctl In Me.Controls
if < one of the properties of ctl is "Locked"> Then
' do some things
Next ctl

The above is wrong of course but it explains what I need I think. Any
suggestions? I'm hoping it won't involve enumerating all the properties of
the control.

Thanks,

Keith
 
D

Dirk Goldgar

Keith G Hicks said:
I need to determine if a control has the "Locked" property during code
execution as follows:

Dim ctl as Control
For Each ctl In Me.Controls
if < one of the properties of ctl is "Locked"> Then
' do some things
Next ctl

The above is wrong of course but it explains what I need I think. Any
suggestions? I'm hoping it won't involve enumerating all the
properties of the control.

Thanks,

Keith

As you know, not all controls will have a Locked property. Do you care
what the value of the Locked property is, or do you just want to know if
it exists? You could just try to get it, and test if you got an error:

Dim ctl as Control
Dim fLocked As Boolean

On Error Resume Next

For Each ctl In Me.Controls
Err.Clear
fLocked = ctl.Locked
If Err.Number = 0 Then
' The control has a Locked property, and
' fLocked holds its value.

End If
Next ctl

On Error GoTo 0 ' or your error-handler
 
K

Ken Snell [MVP]

Try this:

Dim ctl as Control
Dim blnTest As Boolean
On Error Resume Next
For Each ctl In Me.Controls
blnTest = ctl.Locked
If Err.Num = 0 Then
If blnTest = True Then
' do some things
End If
Else
Err.Clear
End If
Next ctl
 
K

Keith G Hicks

No, I don't care at that point what the value is.
I had a feeling that was probably the way to go. Thank you.


As you know, not all controls will have a Locked property. Do you care
what the value of the Locked property is, or do you just want to know if
it exists? You could just try to get it, and test if you got an error:

Dim ctl as Control
Dim fLocked As Boolean

On Error Resume Next

For Each ctl In Me.Controls
Err.Clear
fLocked = ctl.Locked
If Err.Number = 0 Then
' The control has a Locked property, and
' fLocked holds its value.

End If
Next ctl

On Error GoTo 0 ' or your error-handler
 
B

Brendan Reynolds

Generally, there are two ways to determine whether a specified object exists
in a collection - try to get a reference to the object and trap the error if
it doesn't exist (already demonstrated) or loop the collection comparing the
name of each member to the name of the object you're looking for ...

For Each prp In ctl.Properties
If prp.Name = strPropName Then
boolHasProp = True
Exit For
End If
Next prp

This may be somewhat slower than the trap-the-error technique, but in the
context of relatively small collections the difference will not be
significant.

I personally prefer to use this loop-the-collection method whenever
possible, because it does not raise errors when debugging with the Error
Trapping option set to 'Break On All Errors'.
 
K

Keith G Hicks

In fact, this is actually exactly what I ended up doing almost word for
word. It works just fine. :) Thanks for confirming that I was on the right
track.

Keith

"Brendan Reynolds" <anonymous at discussions dot microsoft dot com> wrote in
message Generally, there are two ways to determine whether a specified object exists
in a collection - try to get a reference to the object and trap the error if
it doesn't exist (already demonstrated) or loop the collection comparing the
name of each member to the name of the object you're looking for ...

For Each prp In ctl.Properties
If prp.Name = strPropName Then
boolHasProp = True
Exit For
End If
Next prp

This may be somewhat slower than the trap-the-error technique, but in the
context of relatively small collections the difference will not be
significant.

I personally prefer to use this loop-the-collection method whenever
possible, because it does not raise errors when debugging with the Error
Trapping option set to 'Break On All Errors'.
 

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