How to lock/unlock controls?

S

Sam Kuo

Hi all,

DanJ's code below was originally used to hide/unhide controls. But I have
tried to modify it to unlock controls and to change fore color instead.
However, it seems the method I used to lock/unlock controls is incorrect. Can
someone please lead me in the right direction?

Private Sub cbEditExistingDrawing_Click()
' Change fore color of all controls with Tag property set to "Hide" to red
(225)
' also unlock them for edit
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Hide" Then
ctl.ForeColor = 225
ctl.Locked = False '<-- Access doesn't support this method
End If
Next ctl
End If
End Sub

After the button is clicked, all controls with tag property equals "Hide"
would be unlocked with its fore color changes to red. My problem is the fore
color changes alright, but the line which was intended to unlock the control
is incorrect, as the Access error message says "Object doesn't support this
property or method".

What would be the correct way to lock/unlock those controls?

Regards,
Sam
 
J

John Webb via AccessMonster.com

Sam,

Not all controls have the locked property, for example you cannot lock a
label, as a user is never able to enter data directly into it in the first
place.

Out of the standard controls, I believe you can lock text boxes, combo
boxes, listboxes, option groups, check boxes, option buttons, toggle
buttons and unbound object frames.

Therefore, the first object that your code comes to that is not within this
list will cause an error.

The enabled property is supported by more controls, but not all of them
either.

The way I see it, you have four options:

1. You could create an array of controls using a class module, something
this situation does not merit.

2. You could test ctl.Type to see if it is a lockable type, again, I don't
really think this is an option as there are quite a few types that can /
can not be locked.

3. You could add a Tag to each of the controls you wish to lock, and test
each control in your loop for this tag, then lock it if you find the tag to
be present - this is likely the option I would choose.

4. You could add error handling to test for that specific error number, and
then resume next if found - another option I might consider.

If you need any help with these options, just shout.

Hope that helps

John Webb
 
J

John Webb via AccessMonster.com

Sam,

Sorry, I did not see that you already tested for the Tag property... doh

Oh, and by the way, I would not hard code the False value, but pass it to
the routine as a boolean variable; that way you can unlock and lock the
controls using the same function ;-)

Cheers

John Webb
 
A

Arvin Meyer

Certain controls can't be locked. I actually look at each control, and it's
Tag property to determine is I want to lock it, hide it, or disable it:

Public Sub LockIt(frm As Form)
' Arvin Meyer 11/1/04
On Error Resume Next
Dim ctl As Control

For Each ctl In frm.Controls
With ctl

Select Case .ControlType
Case acTextBox
If ctl.Tag = 1 Then
ctl.Visible= False
ElseIf
ctl.Tag = 2 Then
ctl.Visible= True
ctl.Enabled = False
Else
ctl.Visible= True
ctl.Enabled = True
End If

Case acComboBox
ctl.Locked = False

Case acListBox
ctl.Locked = False

Case acCheckBox
ctl.Locked = True

Case acToggleButton
ctl.Locked = True

Case acCommandButton
If ctl.Tag = 2 Then
ctl.Enabled = False
End If

' Case acTabCtl

Case acSubform
ctl.Locked = True

Case acOptionGroup
ctl.Locked = True

Case acOptionButton
ctl.Locked = True

End Select
End With
Next ctl
Set ctl = Nothing
Set frm = Nothing

End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 

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