Lock / Unlock Button not working...help me please...

Randomblink

The Irreverent Reverend
Local time
Today, 11:33
Joined
Jul 23, 2001
Messages
279
Ok...
The following code is supposed to be locked to a button...
When the button is clicked it is supposed to lock or unlock the form...this way the user opens the form and it is locked, then they have to unlock it to make changes...
When it is unlocked they can make changes...
The button doesn't do anything...
What am I doing wrong...???

FYI: The button is actually a label_field that has an On_Click event.

Private Sub btn_keymaster_Click()
' lock and unlock the form for editing with this button
Select Case KEY
Case "Locked"
Unlock_Key
Case "Unlocked"
Lock_Key
Case Else
KEY = "Locked"
End Select
End Sub

Private Sub Lock_Key()

Form_frm_Class_Mailing_List.AllowAdditions = False
Form_frm_Class_Mailing_List.AllowDeletions = False
Form_frm_Class_Mailing_List.AllowEdits = False

Let KEY = "Locked"
lbl_LOCKED.Visible = True
lbl_Unlocked.Visible = False

End Sub


Private Sub Unlock_Key()

Form_frm_Class_Mailing_List.AllowAdditions = True
Form_frm_Class_Mailing_List.AllowDeletions = True
Form_frm_Class_Mailing_List.AllowEdits = True

Let KEY = "Unlocked"
lbl_LOCKED.Visible = False
lbl_Unlocked.Visible = True

End Sub

Then I have the form do this...

Private Sub Form_Open(Cancel As Integer)
Dim KEY As String
KEY = "Locked"
End Sub

HELP!
Argh!
This was supposed to be a quick easy help-out for a friend and now I can't proceed until I get this fixed or I will go nuts...
 
Why not try somthing like this using the caption property of the button as the trigger:

Private Sub btn_keymaster_Click()
Dim blnToggle as Boolean
Dim TheCaption as String
'use an if statement to set variables used later in code
If Me![btn keymaster].Caption = "Lock" Then
'set lock variables
blnToggle = False
TheCaption = "Unlock"
Else 'must have been "Unlock"
'set unlock variables
blnToggle = True
TheCaption = "Lock"
End if

'do the work
Me.AllowAdditions = blnToggle
Me.AllowDeletions = blnToggle
Me.AllowEdits = blnToggle
Me![btn keymaster].Caption = TheCaption

There is no need to use the OnOpen event as the form properties you set at design time will be adopted - therefore set your caption to UNLOCK and data edit properties to false at design.

HTH

Ian



[This message has been edited by Fornatian (edited 09-11-2001).]
 
Or even shorter:

Private Sub btn_keymaster_Click()
Dim blnToggle as Boolean

blnToggle = Iif(Me![btn keymaster].Caption ="LOCK",False,True)

Me.AllowAdditions = blnToggle
Me.AllowDeletions = blnToggle
Me.AllowEdits = blnToggle

Me![btn keymaster].Caption = Iif(blnToggle = False,"UNLOCK","LOCK")

Ian
 
I would hate to think I am one of those annoying people who don't look on their own before they ask...
So I tried out the code you BOTH gave me...
Both of them work...after a fashion...
Sometimes it shuts down the editing and sometimes it doesnt...
Any idea why?

(oh, and I did add an End Sub for both of those suggestions, so it isn't that)

Can you figure out why it might not work...?
 
Why dont you set your FIELDS to LOCKED, then create TWO buttons. One to UNLOCK the FIELDS and another to LOCK the FIELDS.

To do this, set the FIELD's that you want locked to YES. Then create a button that unlocks the field, Call the button UNLOCK (LOWERCASE). On the ON CLICK event procedure, enter the following code:


Private Sub unlock_Click()
On Error GoTo Err_unlock_Click

Me.YOUR FIELD NAME.Locked = False

(YOU CAN HAVE AS MANY AS YOU LIKE)

Exit_unlock_Click:
Exit Sub

Err_unlock_Click:
MsgBox Err.Description
Resume Exit_unlock_Click

End Sub

Next Create a button that will UNLOCK your fields, try this: Call the Button LOCK (Lowercase) then on the ON CLICK event procdure, enter:

Private Sub lock_Click()
On Error GoTo Err_lock_Click

Me.YOUR FIELD NAME.Locked = True
(AGAIN ENTER AS MANY FIELDS THAT YOU
WANT TO LOCK)

Exit_lock_Click:
Exit Sub

Err_lock_Click:
MsgBox Err.Description
Resume Exit_lock_Click

This way, when the user access your form, the fields that you wish to lock will be locked. When they want to edit the fields, they click on the unlock button. Then click on the LOCK button when they have finished. If they close the form without locking the fields again, it will be OK as the default value will set the fields back to LOCKED.

Hope i helped.
 
It won't wokr properly if you change records, to handlt this put this code in the OnCurrent event:

'set the button text to unlock so it locks when the event runs
Me![btn keymaster].Caption = "UNLOCK"
btn_keymaster_Click 'call the event

Maybe it?

Ian
 

Users who are viewing this thread

Back
Top Bottom