Add a field to your underlying table for the owner of that particular record. (I just called it Owner
.)
Add the following code to your form:
Private Sub Form_BeforeInsert(Cancel As Integer)
Owner = Environ("username")
End Sub
Private Sub Form_Current()
Dim editable As Boolean
if nz(Owner,"")="" then
editable=true
else
editable = (Environ("username") = Owner)
End If
Form.AllowDeletions = editable
Form.AllowEdits = editable
End Sub
This is a fairly generic solution that does the job. Environ("username")
returns the user's windows username. This is not necessarily all that secure. A person could go to the command prompt and edit the username
environment variable so that they appear to be another person. This will definitely not keep out the NSA or anyone else with a little computer knowledge and bad intentions. But it will keep the honest people honest.
Explanation:
The BeforeInsert
runs as a new record is being saved. In this case, it sets the record's Owner
field to the user's Windows username.
The OnCurrent
portion runs every time a new record is displayed. The code will check to see if the current Windows username matches the username stored in the record's Owner
field and set the forms AllowEdits
and AllowDeletions
flags accordingly. Also, if the Owner
field is null
(as in the case of a new record), editable
will be set to true
. If you wanted to really emphasize the fact that the user shouldn't be changing things, you could also enable/disable the individual text boxes...
TextBox1.Enabled=editable
ComboBox2.Enabled=editable
... etc.
Simple solution. Keeps honest people from overwriting each others' work. I use this approach a lot.