Locking/Unlocking Records (1 Viewer)

Geezer

Registered User.
Local time
Today, 23:33
Joined
Jul 14, 2008
Messages
62
Have a form which AllowEdits set to No as default. Have an "Edit" button so the user can unlock the record and have attached some simple code which is causing an issue:

Private Sub Cmd_LockUnlockRecords_Click()

If Me.Form.AllowEdits = False Then
Me.Form.AllowEdits = True
ElseIf Me.Form.AllowEdits = True Then
Me.Form.AllowEdits = False
End If

End Sub

With the form set to AllowEdits = No pressing the Edit button sets AllowEdits = Yes. However on clicking the button again the AllowEdits is NOT set to No.

Would greatly appreciate a bit fo help/pointer.

Thanks,

Gareth
 

Geezer

Registered User.
Local time
Today, 23:33
Joined
Jul 14, 2008
Messages
62
Odd, just tried adding two buttons with SetValue macros attached to each. One set to "No" for locking the record the other to "Yes" for unlocking the record.

The Unlock works fine but the Lock doesn't.

Something else must be happening, any ideas where to look?

Thanks,

Gareth
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:33
Joined
Jan 20, 2009
Messages
12,866
I don't know the answer to your problem but toggling a boolean value or property can be simply achieved with one line rather than the If you posted.

whatever = Not whatever

Are you sure the AllowEdits = False is not working?
This won't prevent deletions.
 

Geezer

Registered User.
Local time
Today, 23:33
Joined
Jul 14, 2008
Messages
62
Thanks,

I'll take a look at the toggle option, not overly familiar with toggle buttons.

The AllowEdits = False button (macro) works only if I jump to another record or click on the Record Selector bar for example. Oddly enough the AllowEdits = True button (macro) works just fine.

I'm using tabs, wonder if that might be causing problems?

I don't know the answer to your problem but toggling a boolean value or property can be simply achieved with one line rather than the If you posted.

whatever = Not whatever

Are you sure the AllowEdits = False is not working?
This won't prevent deletions.

Thanks again for your help, much appreciated.
 

Geezer

Registered User.
Local time
Today, 23:33
Joined
Jul 14, 2008
Messages
62
Solved

Added a bit of Refresh code each time before locking, not entirely sure why but it's all working fine now.

Gareth
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:33
Joined
Jan 20, 2009
Messages
12,866
I'll take a look at the toggle option, not overly familiar with toggle buttons.

I was not actually refering to toggle buttons but toggling a y/n value in a field or property using VBA. You don't need to test the initial state value because there are only two.

In your case the toggle code would be:

Me.Form.AllowEdits = Not Me.Form.AllowEdits

Glad you sorted your problem.
Would not have thought Refresh was involved in a form property.
Having said that, I have succeeded in fixing a odd problem before with a Requery that didn't seem necessary.

Most would agre that making something work while not understanding why is more important than understanding a problem but not being able to make it work. ;)
 

boblarson

Smeghead
Local time
Today, 04:33
Joined
Jan 12, 2001
Messages
32,059
The problem actually occurs because the form property, once set, needs to be cycled through its Current Event in order to set those again. A refresh or requery causes the Current event to fire which then will clean things up and allow you to reset the value again.

I typically just let the unlock happen and then let it be locked when the record moves to another record. Then there isn't the need to relock.
 

boblarson

Smeghead
Local time
Today, 04:33
Joined
Jan 12, 2001
Messages
32,059
Oh, and you can also cause it to relock if you issue a SAVE:

If Me.Dirty Then Me.Dirty = False
and then set the allow edits back to no

(because the ON Current still fires again for this).
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 21:33
Joined
Jan 20, 2009
Messages
12,866
The problem actually occurs because the form property, once set, needs to be cycled through its Current Event in order to set those again.

Now there is a crucial piece of information. I love this place. Thanks for sharing your knowledge Bob.
 

Geezer

Registered User.
Local time
Today, 23:33
Joined
Jul 14, 2008
Messages
62
Thanks Bob for clarrifying why I needed a refresh.

Thanks Galaxiom for the toggle info, seems a tidier option than coding several lines.

Gareth
 

Users who are viewing this thread

Top Bottom