Code to lock fields on form

Les Isaacs

Registered User.
Local time
Today, 14:34
Joined
May 6, 2008
Messages
186
Hi All
I have written the procedure below to lock/unlock all the fields on a form, and it all works perfectly where the form is open in datasheet view, but when the form is open in continuous form view I get "Object doesn't support this property or method" on line 110/140. I'm guessing the locked property is different for continuous forms? I'd be very grateful for any advice.
Thanks in advance.

Code:
Public Sub LockControls(SetLocked As Boolean, frm As Form)

          Dim ctl As control
          Dim strPassword As String, strConfirmation As String

10        On Error GoTo LockControls_Error

20        If SetLocked = False Then
30            strPassword = InputBox("If you want to edit the system parameters, enter the management password below.")
40            If strPassword <> "justdoit" Then
50                MsgBox ("You have not entered the correct management password!")
60                Exit Sub
70            End If
80        End If

90        For Each ctl In frm.Controls
100           If SetLocked = False And strPassword = "justdoit" Then
110               ctl.Locked = False
120               strConfirmation = "The table is now unlocked: be careful!"
130           Else
140               ctl.Locked = True
150               strConfirmation = "The table is now locked!"
160           End If
170       Next

180       MsgBox (strConfirmation)
190       Set frm = Nothing

200       On Error GoTo 0
210       Exit Sub

LockControls_Error:

220       Call WriteErrors(Erl, Err.number, Err.Description, "LockControls", "Module", "modUtils")
End Sub
 
Your problem actually arises, I suspect, from line 90:

For Each ctl In frm.Controls

In Datasheet View, you only have Controls that contain data, i.e. Textboxes, Comboboxes, etc. and these Controls do have the Locked Property, hence it runs just fine.

In Continuous View (and in Single View, as well) these Controls probably have Labels attached to them, and Label Controls do not have the Locked Property, hence the error!

You need to wrap the code in an If...Then construct to only include the type of Controls that can be formatted

Code:
For Each ctl In frm.Controls
  
  [COLOR="Red"] If ctl.ControlType = acTextBox or ctl.ControlType = acComboBox Then
[/COLOR]
     If SetLocked = False And strPassword = "justdoit" Then
        ctl.Locked = False
        strConfirmation = "The table is now unlocked: be careful!"
     Else
        ctl.Locked = True
        strConfirmation = "The table is now locked!"
     End If
  
 [COLOR="Red"]  End If
[/COLOR]
Next
Linq ;0)>
 
Brilliant!
That works perfectly - many thanks!
Les
 

Users who are viewing this thread

Back
Top Bottom