Locking a Form to Prevent accidental edits

mdg

Registered User.
Local time
Today, 08:53
Joined
Feb 17, 2011
Messages
68
I have created a form that I would like to be able to click a button after I enter the data to prevent that individual record from being changed. Then if I want to enter data later on or change something etc, I can click off the button and release the record lock. I tried several different ways but must be doing something wrong. Thanks for any assistance out there.
 
One way is to add a Yes/No field to your underlying table and name it LockRecord.

Add the field to your Form.

Now use this code

Code:
Private Sub LockRecord_AfterUpdate()

If Me.LockRecord =  -1 Then
  Me.AllowEdits = False
Else
  Me.AllowEdits = True
End If
 
End Sub
Now also place this same code in the Form_Current event.

When you tick the checkbox the record will be locked to edits. When you untick it edits will be possible.

Linq ;0)>
 
Thank You. It worked. However, once I click the yes/no button I can not click it off again to regain ability to edit as the record is now locked. I made a work around with a querry to do it but would be nice if I could click off the button. Any suggestions? Thanks so much. I posted this reply yesterday but it did not show up on the forum for some reason.
 
Sorry! Was trying to do too many things at once! To only enable/disable selected Controls, which is too say all except the 'lock' checkbox:

Create a checkbox and set it’s Tag property to "skip" (without the quotes)
Code:
Private Sub LockRecord_AfterUpdate()
Dim ctrl As Control

If Me.LockRecord =  1 Then

For Each ctrl In Me.Controls
    If (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is CheckBox) Then
     If ctrl.Tag <> "Skip" Then
        ctrl.Locked = True
     End If
    End If
 Next

Else

For Each ctrl In Me.Controls
    If (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is CheckBox) Then
     If ctrl.Tag <> "Skip" Then
        ctrl.Locked = False
     End If
    End If
 Next
 
End If
End Sub
Once again, also place this in the Form_Current event.

Sorry for the slip up!

Linq ;0)>
 
Thank You again. But I think I may have messed it all up now. I did what you said but it didn't work. I'm sure it's something I am doing wrong. I am new at Access. I have the extra field 'LockRecord' and have the Me.AllowEdits code in both Form_Current and LockRecord_AfterUPdate. Then put a yes/no box tied to the field. Then put a check box on the form like you said with the tag labled skp, plus put in the additional code like you said. Now the whole thing locks up completely. Sorry for bugging you with this. You seem to know what you are talking about more than anyone else I have made contact with. Could you walk me through the whole thing step by step if you have time. I am trying to make a Data Base to track individual Cases files. So far it works ok but need to add the lock record feature so we won't lose data. I was surprised that there wasn't a command button feature that would do the same thing. You would think they would build in a feature like this since it is a really useful. I attached a snapshot of the front end. Maybe that will help .
 

Attachments

  • IMAGE 1.jpg
    IMAGE 1.jpg
    99.5 KB · Views: 153
Sorry. You should have removed all of the original code I gave you in Post #2, and only use the code I gave you in Post #4.
 
mdg you didn't mess it up :), there is a slight mistake in the code.

Delete this:
Code:
If Me.LockRecord =  1 Then
Replace with:
Code:
If Me.LockRecord =  True Then

And it should work.
 
The slight error is the missing minus sign in the code;
Code:
If Me.LockRecord =  [COLOR="Red"][B]-[/B][/COLOR]1 Then

A check box will return a value of -1 when checked or 0 when uncheck, but you can test for -1 or True and 0 or False.
 
Thanks, John! That's twice this week I've gotten bitten by this thing! For some reason, when copying text from MS Word (where I keep my boilerplate code) to this forum, actually to several forums, the minus/negative sign is stripped out!

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom