INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Partially locking and unlocking controls.

Partially locking and unlocking controls.

(OP)
I have several forms with many controls on them.

Controls:
PW text, Unlock button in Form Header

All other controls in Detail section.

In the past, to lock the controls of a form, In design vies I set the selected controls to data Locked = Yes. When I open the form I have one unlocked text control in the header. The user types in a password and then click a button (also in the header). The code behind the button does a me.(control).Lock = False for each control in the Detail section. When there are few controls this is easy to do.

Now I have 8 forms with up to 20 detail fields each. I want to have those controls locked when opening the form. In design view I will multiple select all the controls of concern and set their Data Locked value to yes.

When the form is opened...I want to use the same password routine and button to unlock all the controls in the detail section. Is there a way to unlock all locked controls with one VB command or do I have to do them individually?

Thanks - ahead of time.

RE: Partially locking and unlocking controls.

You may try something like:

CODE

Dim c As Control
For Each c In Me.Controls
    c.Lock = False 
Next c 

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Partially locking and unlocking controls.

That will most likely not work. Not every control type has a locked property so you could throw an error. Labels do not have a locked property. One way to avoid that would be to place a tag in the tag property. You can do this all at once. Click on all the controls you want to lock or unlock and then in tag property put something. Example "Lock". Every control type does have a Tag property.

For Each c in me.controls
if c.tag = "Lock" then c.lock = false
next c

The other way would be to check if it is the correct control type
For Each c in me.controls
if c.controlType = acTextBox or c.controlType = acComboBox or c.controlType = aclistbox....acradiobutton actogg 'whatever ones you have on your form
c.lock = false
end if
next c

RE: Partially locking and unlocking controls.

My example was just the idea that you do not need to list all controls that you want to Lock / UnLock. You can just simple loop thru all controls on your Form and change the property of the controls you need.

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Partially locking and unlocking controls.

(OP)
OK here is what I tried. All controls in Detail set to Locked and Tagged to "Lock" (Quotes included in TAG"

In the header a text box named PW. A button named Command63.

Button code:
Private Sub Command63_Click()
Dim c As Control
If Me.PW = "Test" Then
MsgBox "PW Correct"
For Each c In Me.Controls
If c.Tag = "Lock" Then c.lock = False
Next c
End If
End Sub

This returns the Msg box saying PW is correct but does not unlock any fields.

I then changed one field and removed the quotes from the Tag so Tag = Lock
Ran the unlock process again and got an error showing.
Private Sub Command63_Click()
Dim c As Control
If Me.PW = "Test" Then
MsgBox "PW Correct"
For Each c In Me.Controls
If c.Tag = "Lock" Then c.lock = False
Next c
End If
End Sub

Where am I going wrong please


RE: Partially locking and unlocking controls.

You do not need the quotes in the tag property. The tag would actually become "Lock" instead of Lock.
You do not need an "end if" if done on a single line. That "end if" is in wrong spot. And my mistake on the property name. It is ".locked" not ".lock"

CODE -->

Private Sub Command63_Click()
Dim c As Control
  If Me.PW = "Test" Then  MsgBox "PW Correct"
  For Each c In Me.Controls
     If c.Tag = "Lock" Then c.locked = False
   Next c
End Sub 

RE: Partially locking and unlocking controls.

(OP)
Thanks...this works well. The final looks like this.

Dim c As Control
If Me.PW = "Test" Then
For Each c In Me.Controls
If c.Tag = "Lock" Then c.Locked = False
Next c
End If

I only had the MsgBox in there to verify the PW check was working. It is not needed now.

Thanks very much MajP

RE: Partially locking and unlocking controls.

Up on the edit bar there is an icon that looks like a scroll to the left of the birthday present. Select your code then select that icon. It will wrap it in tags to format and make readable.

CODE -->

Dim c As Control
 If Me.PW = "Test" Then
   For Each c In Me.Controls
     If c.Tag = "Lock" Then c.Locked = False
   Next c
 End If 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close