By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,115 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,115 IT Pros & Developers. It's quick & easy.

How to lock a field in a record or make it read only

P: 8
Hi, Im fairly new in the Access world. I would like to know how I can prevent users from changing data entered into a specific field of a record. Once I have completed the data Entry they should be able to see the information but not be able to change it.
Jul 18 '07 #1
Share this Question
Share on Google+
11 Replies

puppydogbuddy
Expert 100+
P: 1,923
Hi, Im fairly new in the Access world. I would like to know how I can prevent users from changing data entered into a specific field of a record. Once I have completed the data Entry they should be able to see the information but not be able to change it.
Here is one way, assuming that you are using a bound form for data entry:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. If Not Me.NewRecord Then
  3.    'Lock YourControl for each record.
  4.     Me!YourControl.Locked = True
  5. Else
  6.     Me!YourControl.Locked = False
  7. End If
  8. End Sub 
Jul 18 '07 #2

Expert 100+
P: 636
Hi
Here is one way, assuming that you are using a bound form for data entry:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. If Not Me.NewRecord Then
  3.    'Lock YourControl for each record.
  4.     Me!YourControl.Locked = True
  5. Else
  6.     Me!YourControl.Locked = False
  7. End If
  8. End Sub 
As an alternative is
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. If Me.NewRecord Then
  3.     Me.AllowEdits = True
  4. Else
  5.     Me.AllowEdits = False
  6. End If
  7. End Sub 
You could also include
Me.AllowDeletions= ....
if you want to prevent record deletion also.


MTB
Jul 19 '07 #3

damonreid
Expert 100+
P: 114
You could always just go into the Properties and change
Enabled = False
Locked = True

and ignore the VB side of it.
Jul 19 '07 #4

P: 8
Thanks for the advise. I have used this code to lock all fields in current form. Once the first record is created the code doesn't allow any editing or deleting. The problem I have now is how to make changes to that record at a later time.
Any Additional advise will be highly appreciated

1. Private Sub Form_Current()
2. If Me.NewRecord Then
3. Me.AllowAdditions = True
4. Me.AllowDeletions = True
5. Me.AllowEdits = True
6. Else
7. Me.AllowAdditions = True
8. Me.AllowDeletions = False
9. Me.AllowEdits = False
10. End If
Jul 19 '07 #5

Expert 100+
P: 636
Thanks for the advise. I have used this code to lock all fields in current form. Once the first record is created the code doesn't allow any editing or deleting. The problem I have now is how to make changes to that record at a later time.
Any Additional advise will be highly appreciated

1. Private Sub Form_Current()
2. If Me.NewRecord Then
3. Me.AllowAdditions = True
4. Me.AllowDeletions = True
5. Me.AllowEdits = True
6. Else
7. Me.AllowAdditions = True
8. Me.AllowDeletions = False
9. Me.AllowEdits = False
10. End If
Two things

Define 'that record' and
Define under what circumstances you want the record to be edited.


M TB
Jul 19 '07 #6

P: 8
Two things

Define 'that record' and
Define under what circumstances you want the record to be edited.


M TB
Im very new at access and I really don't know much of VBA programming. So Im just looking for an easy way to control the data entry process. Can I create a Command Button and lets say we call it (Edit) and from there you could work in edit mode for that form? A (delete) Button etc. I was thinking about running These command buttons from a Main Form Menu. I just dont know the language to make this work correctly. If you have any better suggestions I would highly appreciate it.
Jul 19 '07 #7

Expert 100+
P: 636
Im very new at access and I really don't know much of VBA programming. So Im just looking for an easy way to control the data entry process. Can I create a Command Button and lets say we call it (Edit) and from there you could work in edit mode for that form? A (delete) Button etc. I was thinking about running These command buttons from a Main Form Menu. I just dont know the language to make this work correctly. If you have any better suggestions I would highly appreciate it.
cmdEdit button

Expand|Select|Wrap|Line Numbers
  1. Sub cmdEdit_Click
  2. On Error GoTo Err_cmdEdit_Click
  3.  
  4.    Me.AllowEdits = True
  5.  
  6. Exit_cmdEdit_Click:
  7.     Exit Sub
  8.  
  9. Err_cmdEdit_Click:
  10.     MsgBox Err.Description
  11.     Resume Exit_cmdEdit_Click
  12.  
  13. End sub
The OnCurrent event will reset allow edits when you move to the next record.

cmdDeleteButton

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2. On Error GoTo Err_cmdDelete_Click
  3.  
  4.     If MsgBox("Do you want to delete record?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
  5.  
  6.     RunCommand acCmdDeleteRecord
  7.  
  8. Exit_cmdDelete_Click:
  9.     Exit Sub
  10.  
  11. Err_cmdDelete_Click:
  12.     MsgBox Err.Description
  13.     Resume Exit_cmdDelete_Click
  14.  
  15. End Sub
Have you tried the button wizzard when placing buttons on a form.

There are options for deleting records etc. (although the code generated will be different form above).


MTB
Jul 20 '07 #8

P: 8
Thanks for your patience and help . I used the code for editing you gave me. I created a Command button on the actual form the data entry is being done on and named it (Edit). I have used the code below to lock the form , and to be able to edit it. When the form is opened all the fields are locked(good) but when I click on the cmdEdit button the fields remain locked. I have saved and closed the form various times but records are still locked. I haven't done the (delete)button until I know that the code does work. Should I put the command (edit) button somewhere other than the data entry form for this code to work?

Thank You

Private Sub Form_Current()
If Me.NewRecord Then
Me.AllowEdits = True
Me.AllowDeletions = True
Else
Me.AllowEdits = False
Me.AllowDeletions = False
End If
End Sub

Private Sub cmdEdit_Click()
On Error GoTo Err_cmdEdit_Click

Me.AllowEdits = True

Exit_cmdEdit_Click:
Exit Sub

Err_cmdEdit_Click:
MsgBox Err.Description
Resume Exit_cmdEdit_Click
End Sub
Jul 20 '07 #9

P: 8
I have also used the Code for deleting a record and I get an error " The command or action Deleterecord isn't available now.
Jul 20 '07 #10

Expert 100+
P: 636
I have also used the Code for deleting a record and I get an error " The command or action Deleterecord isn't available now.
To answer your last probelm fist I thonk you need this

Me.AllowDeletions = True
RunCommand acCmdDeleteRecord
Me.AllowDeletions = False

in the delete code.

for now, I do not know why the edit doesn'twork, it does for me.

I will think about it over the week end, but I am not sure about
Should I put the command (edit) button somewhere other than the data entry form for this code to work?
What data entry form and what other form?

MTB
Jul 20 '07 #11

P: 4
This is exactly the issue that came up for me yesterday - my problem was the simpler version that MikeTheBike and puppydogbuddy offered up solutions for. And it worked! Thank you both!
Jul 20 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.