preventing users from overwriting fields (1 Viewer)

scott51

New member
Local time
Today, 12:47
Joined
Jun 22, 2012
Messages
9
Am looking to prevent users from overwriting select fields on a form once they are input to try to keep garbage from coming in. Would like Field 1 to be set so they get a message but can overwrite if need (prevent oops, typing in wrong field errors). Field 2 I would like set so they cannot modify once it is initially input. I'm thinking this should be easy, but don't see how to do it. Help! BTW, am using Access 2010. Thanks.
 

jzwp22

Access Hobbyist
Local time
Today, 12:47
Joined
Mar 15, 2008
Messages
2,629
For the second control (field), you could add some code to the On Current event of the form. You will have to check to see if the current record is a new record. If it is a new record, unlock the control otherwise lock the control.

The code would look something like this

If Me.NewRecord = True Then
Me.NameOf2ndControl.Locked = False
Else
Me.NameOf2ndControl.Locked = True
End If

Now regarding the first control (field), that is a little more difficult. What I would recommend is to have a button to allow the control to be edited otherwise keep the control locked except of course if it is a new record. You will have to utilize the On Current event of the form but also some of the events of the first control and of course the button.

I have attached a simple database with 1 table and a corresponding form that includes the code. The first name control on the form corresponds with what you want to do with your first "field". The last name control corresponds to what you want to do with your second "field".
 

Attachments

  • formcontrols.zip
    28 KB · Views: 260

scott51

New member
Local time
Today, 12:47
Joined
Jun 22, 2012
Messages
9
Thanks, that helps. It works for standard input fields, but not when selecting from a list box. Working on that now
 

jzwp22

Access Hobbyist
Local time
Today, 12:47
Joined
Mar 15, 2008
Messages
2,629
Theoretically you should be able to lock a list box as well.

How is the list box not working?
 

scott51

New member
Local time
Today, 12:47
Joined
Jun 22, 2012
Messages
9
After further testing, I can get it to lock the list box by putting code into the list box click or before update. However, if you click in the invoice field, you can change any fields "locked" with the code even though there is already data (invoice) in the field. Maybe I need to change the code a little where it goes off null instead of "new record".
 

jzwp22

Access Hobbyist
Local time
Today, 12:47
Joined
Mar 15, 2008
Messages
2,629
If you look in the On Current event of the form of the database I provided earlier you will see the code I used. There is an IF..THEN.. ELSE..END IF statement that tests to see if the current is a new record (me.NewRecord), if it is, it sets the lock property to false.

This is where you need to add code to lock or unlock other form controls.
 

scott51

New member
Local time
Today, 12:47
Joined
Jun 22, 2012
Messages
9
I thought the list box was locking, but I was incorrect. But I found a way around it by writing an Undo into the code on the list box before update which works great!
 

jzwp22

Access Hobbyist
Local time
Today, 12:47
Joined
Mar 15, 2008
Messages
2,629
Glad to hear that you got it worked out.
 

cclark9589

Registered User.
Local time
Today, 09:47
Joined
Sep 22, 2008
Messages
79
jz

I've been trying for some time now to figure out how to prevent other users from changing certain fields once a new record has been added. No luck until I stumbled across this string earlier today. The code in your first response was absolutely spot-on for what I needed.

Woohoo!!!!
 

jzwp22

Access Hobbyist
Local time
Today, 12:47
Joined
Mar 15, 2008
Messages
2,629
Glad to hear that the thread helped you out. Good luck as you move forward with your project.
 

Users who are viewing this thread

Top Bottom