lock button

M

MAR

I have a lock button, but is not working properly, 'It will only allow me to
lock the first time and first page after 'hitting the lock button again, I
want to lock each case as being 'completed and be able to edit and lock
anytime......HEEELLLLLPPPPPP!!!!!!!!

Option Compare Database
Private Sub addingBtn_Click()
On Error GoTo Err_addingBtn_Click


DoCmd.GoToRecord , , acNewRec

Exit_addingBtn_Click:
Exit Sub

Err_addingBtn_Click:
MsgBox Err.Description
Resume Exit_addingBtn_Click

End Sub

Private Sub CloseBtn_Click()
On Error GoTo Err_CloseBtn_Click


DoCmd.Close

Exit_CloseBtn_Click:
Exit Sub

Err_CloseBtn_Click:
MsgBox Err.Description
Resume Exit_CloseBtn_Click

End Sub
Private Sub modifyBtn_Click()
On Error GoTo Err_modifyBtn_Click

'Forms!Employees!EmployeeID.SetFocus
Me.Text2.SetFocus
'Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_modifyBtn_Click:
Exit Sub

Err_modifyBtn_Click:
MsgBox Err.Description
Resume Exit_modifyBtn_Click

End Sub
Private Sub LockBtn_Click()
On Error GoTo Err_LockBtn_Click

'Clicking this button will prevent the user from editing

Me.AllowEdits = False

Exit_LockBtn_Click:
Exit Sub

Err_LockBtn_Click:
MsgBox Err.Description
Resume Exit_LockBtn_Click

End Sub
Private Sub EditBtn_Click()
On Error GoTo Err_EditBtn_Click

'Clicking this button will allow to edit once is being locked

Me.AllowEdits = True

Exit_EditBtn_Click:
Exit Sub

Err_EditBtn_Click:
MsgBox Err.Description
Resume Exit_EditBtn_Click

End Sub
 
D

Danny J. Lesandrini

You're setting the property of a form when what you really want to do is to
set the property of a record.

Add a Yes/No column to your table named Locked and set its default to False.

Add code in the on current event to read this bit and lock the record by
toggling the Locked property of all controls.

Add a button to lock a record, or simply put the code in the chkLocked after
update event as well.
--
Danny J. Lesandrini
(e-mail address removed)
www.amazecreations.com
 
K

Ken Sheridan

You can lock/unlock the form with a single button like so:

Dim ctrl As Control

Set ctrl = Me.ActiveControl

Me.AllowEdits = Not Me.AllowEdits
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")

If you want the form locked by default put the following in its
Current event procedure:

Dim ctrl As Control

Set ctrl = Me.cmdLock_Modify

Me.AllowEdits = False
ctrl.Caption = "Modify"
ctrl.Enabled = Not Me.NewRecord

and the following in its AfterInsert event procedure:

Dim ctrl As Control

Set ctrl = Me.Command38

Me.AllowEdits = False
ctrl.Enabled = True
ctrl.Caption = "Modify"

Ken Sheridan
Stafford, England
 
K

Ken Sheridan

Having seen Danny's reply and re-read your original post I'm not sure
which of the two you want to do, but you now have solutions for both,
so the choice is yours. Command38 should have been cmdLock_Modify of
course, which is what I've called the button.

Ken Sheridan
Stafford, England
 
M

MAR

I tried the suggested and got compiling error on the cmdLock_Modify, as far
as the first suggestion, did not work except it only changed the button I.D.
from Lock to Modify.

I don't really understand Danny's suggestion, I'm barely learning acces.,
but my guess is that I need to create the table for the Lock button and and
assign it to a Yes/No Data Type, then give the current coding to the button
with a default FALSE (Now I got confused) Still need help......
 
M

MAR

Forgot to mention that I'm using access 2000 or at least I'm assuming, it
does not display in the properties....another comment is that I would like to
lock the form per case and leave the Add, Modify and Close buttons unlock
when the rest is locked.......
 
K

Ken Sheridan

I'm still unclear as what is you want to do. We've given you two
solutions, which do different things:

1. My solution locks the form by default for every record apart from
when adding a new one. A record can be unlocked for editing by
clicking the button and then re-locked by clicking it again or by
moving to another record. On what line are you getting the error, and
what is the error?

2. Danny's solution enables you to lock specific records by adding a
Boolean (Yes/No) field, named Locked say, to the underlying table, so
you can lock or unlock any particular record in the form and it will
stay locked or unlocked until you unlock or lock it. With this,
rather than using a button on your form you can use a check box with
the field as its ControlSource and in the button's Click event
procedure put:

Dim ctrl As Control

Set ctrl = Me.ActiveControl

Me.Locked = Not Me.Locked
Me.AllowEdits = Not Me.AllowEdits
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")

In the form's Current event procedure put:

Dim ctrl As Control

Set ctrl = Me.cmdLock_Modify

Me.AllowEdits = Not Nz(Me.Locked,False)
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")
ctrl.Enabled = Not Me.NewRecord

and the following in its AfterInsert event procedure:

Dim ctrl As Control

Set ctrl = Me.cmdLock_Modify

Me.AllowEdits = True
ctrl.Enabled = True
ctrl.Caption = "Lock"

Ken Sheridan
Stafford, England
 
M

MAR

Ken,

I have about 8 fields for the user to enter information plus some combo
boxes to select, so I really don't want to lock each entry as I complete the
form. Although, I typed:

Private Sub LockBtn_Click()

Dim ctrl As Control

Set ctrl = Me.cmLock_Modify

Me.AllowEdits = False
ctrl.Caption = "Modify"
ctrl.Enabled = Not Me.NewRecord

End Sub

And the error is in:

Set ctrl = Me.cmdLock_Modify

It says that it does not recognize

cmdLock_Modify

I do not show any AfterInsert there is only AfterUpdate in the text
fields....Do you mind if I send you the database.... Maybe I'm not explaining.

Thanks,
 
K

Ken Sheridan

cmLock_Modify is the name I'd given to the button, but yours is
clearly named LockBtn, so change each instance of cmLock_Modify to
LockBtn in the code, so it would be:

For the button's Click event procedure:

Dim ctrl As Control

Set ctrl = Me.ActiveControl

Me.AllowEdits = Not Me.AllowEdits
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")

If you want the form locked by default put the following in its
Current event procedure:

Dim ctrl As Control

Set ctrl = Me.LockBtn

Me.AllowEdits = False
ctrl.Caption = "Modify"
ctrl.Enabled = Not Me.NewRecord

and the following in its AfterInsert event procedure:

Dim ctrl As Control

Set ctrl = Me.LockBtn

Me.AllowEdits = False
ctrl.Enabled = True
ctrl.Caption = "Modify"


The AfterInsert event is an event of the form, not a control, so you
need to select the form and then add the code to the event procedure.
The same goes for the Current event.

I'm sure we can crack this without your sending me the file. In any
event sending to the email address in my profile won't get you
anywhere as its reserved for junk and not monitored.

Ken Sheridan
Stafford, England
 
M

MAR

Thanks for clarification Ken, did what you told me to, but still got no
activity for the lock button exept it displays a "-1" and "0" for the first
two trials and then changes from "Lock" to "Modify" for other trials.

Here is what I have now:

Private Sub Form_AfterInsert()

Dim As Control

Set strl = Me.LockBtn

Me.AllowEdits = False
ctrl.Enabled = True
ctrl.Caption = "Modify"

End Sub

Private Sub Form_Current()

Dim ctrl As Control

Set ctrl = Me.LockBtn

Me.AllowEdits = False
ctrl.Caption = "Modiy"
ctrl.Caption = Not Me.NewRecord

End Sub

Private Sub LockBtn_Click()

Dim ctrl As Control

Set ctrl = Me.ActiveControl

Me.AllowEdits = Not Me.AllowEdits
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")

End Sub
 
K

Ken Sheridan

This line in the Current event proceure:

ctrl.Caption = Not Me.NewRecord

should be:

ctrl.Enabled = Not Me.NewRecord

Ken Sheridan
Stafford, England
 
M

MAR

It works only the FIRST time on the first case, second case and on, the
button is disabled....
 
K

Ken Sheridan

I can't really see why that should be the case. When you move to a
record each line of code in the Current event procedure does as
follows:

' this next line disallows edits, i.e. 'locks' the form
' for all but a new record
Me.AllowEdits = False

' this sets the button's caption to 'Modify'
ctrl.Caption = "Modify"

' this enables the button for all records
' apart from a new one, where the button
' is not relevant, so should not be
' available to a user
ctrl.Enabled = Not Me.NewRecord

So I can't see any way that it can be disabled for anything apart from
a new record. Its disabled when entering a new record because there
is nothing to 'modify' or 'lock' in that context, only when the user
navigates to an existing record, or after saving the new record.
Because the AllowAdditions property is never set to False a user can
enter a new record regardless of the setting of the AllowEdits
property.

This is the code for the entire module of a real form in which I've
tested it:

Option Compare Database
Option Explicit

Private Sub Form_AfterInsert()

Dim ctrl As Control

Set ctrl = Me.LockBtn

Me.AllowEdits = False
ctrl.Enabled = True
ctrl.Caption = "Modify"

End Sub

Private Sub Form_Current()

Dim ctrl As Control

Set ctrl = Me.LockBtn

Me.AllowEdits = False
ctrl.Caption = "Modify"
ctrl.Enabled = Not Me.NewRecord

End Sub

Private Sub LockBtn_Click()

Dim ctrl As Control

Set ctrl = Me.ActiveControl

Me.AllowEdits = Not Me.AllowEdits
ctrl.Caption = IIf(Me.AllowEdits, "Lock", "Modify")

End Sub

If that isn't doing what you want then you'll need to explain in more
detail just what it is you expect to happen.

Ken Sheridan
Stafford, England
 
M

MAR

I’m doing a Lessons Learned database, where eight or less people will have
access to it, they will required to input all information necessary to have
an approval. The master user will type in the name of the person who
approve each lessons learned case and after lock it, so when regular users
log in they will not be able to edit a closed case, but they will be able to
add new cases and modify unclosed ones.

With
 
M

MAR

Forgot to mention, only the master user will be required to input a password
to allow the edits on a closed case. (need to fiugure that one too)
 
K

ken

We are back to square one! As Danny said in the first reply to your
original post, and I amplified on in my third reply, you need to add a
Boolean (Yes/No) column to the underlying table. Re-read those posts.

As regards the 'master user' locking/unlocking the form you can make
sure the master user has access to the record via a form with a button
or bound check box and the code I gave you in my third post, while
other users only have access via a form without the button or check
box or code.

Ken Sheridan
Stafford, England
 

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