Command Button to Lock Form Fields

B

Ben

I have a bunch of fields on a form, from combo boxes, to memo fields. I want
to click a button that will lock the fields. Then click another button to
unlock the fields.

I have the following for locking:
Me!WARID.Locked = False

I get the following error message:
Compile Error
Method or data member not found


What is the problem? What do I need to do to fix this?

Ben
 
D

Dirk Goldgar

Ben said:
I have a bunch of fields on a form, from combo boxes, to memo fields. I
want
to click a button that will lock the fields. Then click another button to
unlock the fields.

I have the following for locking:
Me!WARID.Locked = False

I get the following error message:
Compile Error
Method or data member not found


What is the problem? What do I need to do to fix this?

Where was that line of code located? if it was in the form's code module,
and the form has a text box, combo box, or list box named "WARID", then I
don't see why you would get that message. If, on the other hand, the code
is in a standard module, or in some other form, or if "WARID" is not the
correct name of the form, then that might explain the error.

Based on what you say you're trying to do, you may find the following
function useful:

'------ start of code ------
Public Function fncLockUnlockControls(frm As Form, LockIt As Boolean)

' Lock or unlock tagged controls on form <frm>,
' depending on the value of <LockIt>: True = lock; False = unlock.

On Error GoTo Err_fncLockUnlockControls

Dim ctl As Control

For Each ctl In frm.Controls

With ctl
If "," & .Tag & "," Like "*,LOCK,*" Then
.Locked = LockIt
End If
End With
Next ctl

Exit_fncLockUnlockControls:
Exit Function

Err_fncLockUnlockControls:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_fncLockUnlockControls

End Function
'------ end of code ------

You would identify the controls to be locked/unlocked at design time by
putting the word "LOCK" in their Tag properties (on the Other tab of the
control's property sheet). To lock all the tagged controls, you would
execute this line of code from a command button on the form:

fncLockUnlockControls(Me, True)

and unlock them with this line of code:

fncLockUnlockControls(Me, False)

Please note: I just modified that code from a somewhat different version of
the function, so I haven't tested it and may have made some mistakes in
editing it.
 
M

Maurice

Hi Ben,

tried what you wanted and used two buttons for it. In the tag of the fields
you want to lock type something like "Locked"

Behind the button to lock place this:

Private Sub cmdLock_Click()
Dim ctl As Control

For Each ctl In Me
If ctl.Tag = "locked" Then
ctl.Locked = True
End If
Next
End Sub

If you want to unlock place this behind the unlock button

Private Sub cmdUnlock_Click()
Dim ctl As Control

For Each ctl In Me
If ctl.Tag = "locked" Then
ctl.Locked = False
End If
Next
end sub

try and see if that helps
 
F

fredg

I have a bunch of fields on a form, from combo boxes, to memo fields. I want
to click a button that will lock the fields. Then click another button to
unlock the fields.

I have the following for locking:
Me!WARID.Locked = False

I get the following error message:
Compile Error
Method or data member not found

What is the problem? What do I need to do to fix this?

Ben

Try it this way with just one command button.
First set the Command button's caption to either "Lock all" or "Unlock
all".

Then code it's click event:

Private Sub CommandButtonName_Click()
On Error Resume Next
Dim c As Control
If Me.CommandButtonName.Caption = "Lock all" Then
For Each c In Controls
c.Locked = True
Next c
Me.CommandButtonName.Caption = "Unlock all"
Else
For Each c In Controls
c.Locked = False
Next c
Me.CommandButtonName.Caption = "Lock all"
End If
 

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