how to lock all controls in the form at once

kosala1981

New member
Local time
Today, 06:23
Joined
Apr 5, 2007
Messages
9
how to lock set of controls in a form with out specifying one by one. i tried a for loop. but it didnt work. i couldn't get the Locked property of the controls.

here is my code:

For x = 0 To Me.Count - 1
If TypeOf Me.Controls(x) Is TextBox Then
Me.Controls(x).Locked = True
ElseIf TypeOf Me.Controls(x) Is ComboBox Then
Me.Controls(x).Locked = True
End If
Next x

the above code gives an error.

how to lock all controls in the form at once. answer would be really appreciated.
 
Code:
Dim ctl As Control
  For each ctl in Me.Controls
     If TypeOf ctl Is TextBox Then
        ctl.Locked = True
     End If
 Next ctl
 
I'm not an expert in vba programming but I've found a way to lock controls, make them invisible/visible and etc...

It's very simple.

Just create this sub on a Module (if you want to lock/ unlock controls):

Code:
Sub Block(aForm As String, aTag As String, YN As Boolean)

Dim ctrl As Control

If YN = True Then

    For Each ctrl In Forms(aForm).Controls
        If ctrl.Tag = aTag Then
            ctrl.[COLOR="Red"]Locked[/COLOR] = True
        End If
    Next

Else

    For Each ctrl In Forms(aForm).Controls
        If ctrl.Tag = aTag Then
            ctrl.[COLOR="red"]Locked[/COLOR] = False
        End If
    Next

End If

End Sub

Then you can call the sub any time you want

Code:
Private Sub Form_Load()

Call Block([I]Form_Name[/I],[I]Tag_Name[/I], [I]True[/I])

End Sub

Sub string:
aFrom - write the name of the form
aTag - write the Tag of the controls
YN - type true to lock / type false to unlock

This sub will only work on the controls with the same Tag.
To change the Tag of a control select the control or group of controls then:
Properties -> Other -> Tag

Hope this will be useful. It's a lot to me :)
 
I'm not an expert in vba programming but I've found a way to lock controls, make them invisible/visible and etc...

It's very simple.
Hope this will be useful. It's a lot to me :)

It´s not working...
 
Hi. Maybe it's not what you want but if you want to lock the entire form, then you could try using:


Me.Edit = False
 
Hi. Maybe it's not what you want but if you want to lock the entire form, then you could try using:


Me.Edit = False

Just one control should remain usable... I have attached the screenshot, the only unlocked control should be the one beside ENTER CARD NUMBER".
 

Attachments

  • Form Feedback Lock Controls.png
    Form Feedback Lock Controls.png
    29.2 KB · Views: 263
Just one control should remain usable... I have attached the screenshot, the only unlocked control should be the one beside ENTER CARD NUMBER".
In that case, I think the simpler approach for you is to loop through the controls and check if its name is the one for the textbox you want to exclude. If it matches, skip it; otherwise, if it's a control you can lock, lock it. Hope it makes sense...
 
For your needs, DBGuy's suggestion may be easiest
Alternatively using my approach, place this function in a module:

Code:
Public Sub LockControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
        Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler

    'set controls to locked or not according to the control tag value
     For Each ctrl In Screen.ActiveForm.Controls
        Select Case ctrl.ControlType
        
        Case acLabel, acCommandButton, acTabCtl, acPage, acImage, acLine, acRectangle, acPageBreak
            'no code here - these can't be locked
            
        Case Else
            If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
                    Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.Locked = State
                    
        End Select
            
    Next ctrl
  
Exit_Handler:
    Exit Sub

Err_Handler:
    MsgBox "Error " & Err.Number & " in LockControls procedure: " & Err.Description
    Resume Exit_Handler
    
End Sub

Then use 2 different tag values for the various controls you want to manage e.g. L & U (for lock & unlock)

Then use this code in form load

Code:
LockControls True, "L"
LockControls False, "U"
 
For your needs, DBGuy's suggestion may be easiest
Alternatively using my approach, place this function in a module:
...
Then use 2 different tag values for the various controls you want to manage e.g. L & U (for lock & unlock)

Then use this code in form load

Code:
LockControls True, "L"
LockControls False, "U"

Is this looping through all controls? In that case I will create a new case for the txtCardFind control?
 
The LockControls procedure loops through each control with the assigned tag value and locks or unlocks as appropriate.
Not sure what you meant by 'a new case ....'
 
The LockControls procedure loops through each control with the assigned tag value and locks or unlocks as appropriate.
Not sure what you meant by 'a new case ....'

I am not sure how to use this, Colin... :o

I have to tag a control with "L" if I want it to be locked when I call this function?

But what do I have to do if I wanted them to be unlocked?

I am sorry but I facing a real nightmare on a form and I think I am not thinking straight anymore...:(
 
You can use any letters you want for the tag values.
But using my suggestions, tag all those you want locked as L. Tag the one you don't want locked as U.

I tend to use this approach with complex forms, especially those where I want to change the control state(s) when certain events happen

However this may well be overkill for your purposes.
Why not just select all the controls you want to lock then set their locked property =yes in the property sheet.
No code required. Job done.
 
...
However this may well be overkill for your purposes.

Given the number of controls I guess you are right, but I will add it to my reference notebook for sure!:)

Why not just select all the controls you want to lock then set their locked property =yes in the property sheet.
No code required. Job done.

Unless I did not understand your approach of the property sheet - I am sorry in advance:

I need to unlock it when the user will input the feedback information.

The events will be:
  • On form load: LOCKED = True
  • On cmdSearch_Click() if a record is found: LOCKED = False
  • On cmdSave_Click(): Save, Clear Controls and LOCKED = True
I will try a loop through all controls assessing their control types and add a IF/Case Select to exclude the txtCardFind control.

Thanks!
 
Why not just lock them all and then unlock that one and only.?
 
Why not just lock them all and then unlock that one and only.?

The reason I want to lock them all is to prevent the user from creating a unwanted record on the table if he clicks on the controls before running a search...

I thought I have just managed that with this:

Code:
Private blnGood As Boolean 'This is used to prevent the form to saving the record automatically.
 
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
        If Not blnGood Then Cancel = True
End Sub
 
Private Sub cmdSave_Click()
'RESET BOOLEAN TO AUTOSAVE ON THE BOUND FORM.
    blnGood = True
' SAVE THE RECORD
    Call DoCmd.RunCommand(acCmdSaveRecord)
'DISPLAY THE EMPTY RECORD FOR NEW INSERTION.
    DoCmd.GoToRecord , , acNewRec
'RESET BOOLEAN TO PREVENT THE AUTOSAVE ON THE BOUND FORM.
    blnGood = False
    Me.txtFindCard.Value = Null
'TELL USER
    MsgBox "Feedback saved!"

But when I click on the reset button is return a error:

"2105" You can´t go to the specified record

Then it highlights here:

Code:
Private Sub cmdReset_Click()
DoCmd.GoToRecord , , acNewRec ' highlight this with error 2105
Me.txtFindCard.Value = Null
Me.txtFindCard.SetFocus
Me.txtFindCard.BackColor = RGB(255, 250, 240)
Me.txtFindCard.ForeColor = RGB(142, 68, 173)

I am always stuck on this...
 
You seem to be making this unnecessarily complicated.

Let me check I've understood you properly.
All controls except txtCardFind to be locked permanently.
Textbox txtCardFind to be locked at the start but then unlocked/relocked as explained in your last post.

If so, lock ALL textboxes as suggested by Gasman using the property sheet.
The 'locked' property is near the bottom of the Data tab in the property sheet.
Save the form

No code is needed in form load as the controls will be locked when it is loaded.
Now add code to cmdSearch_Click event similar to this

Code:
Private Sub cmdSearch_Click()

If Nz(Me.txtSearch,"")<>"" Then Me.txtSearch.Locked=False

End Sub

Then do similar code on cmdSave to lock the control again.

Hope that makes sense. Adapt as necessary if I've got the details wrong
 
I provide video instructions on how to lock and unlock controls on my website here:- Lock, Unlock Controls

I show several methods from a Basic Approach right up to a Sophisticated, Object Oriented Approach.
 

Attachments

Last edited:
You seem to be making this unnecessarily complicated.
...
If so, lock ALL textboxes as suggested by Gasman using the property sheet.

I have followed you and gasman´s suggestions and it works, unfortunately that´s was not the source of the problem I was having.

I can´t make the reset button to work... but I will post that on another thread because this one is another subject.

Thanks guys.
 

Users who are viewing this thread

Back
Top Bottom