Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Partially locking and unlocking controls. 1

Status
Not open for further replies.

puforee

Technical User
Oct 6, 2006
741
US
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.

 
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.
 
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

 
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.
 
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


 
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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top