locking a field in a form

D

Debbie S.

Hello,

I want to be able to lock a field but still allow the user to enter NEW data
into the field in the form. (In other words, allow users to enter new data
into a particular field of a form but not change existing data in that
field). If I lock the field in the properties sheet, the data can't be
edited, which is good, but you can't enter new data either, which defeats the
purpose of the form. Is there a way to do this?

Thanks,
Debbie
 
K

Klatuu

Yes, use the form's Current event:

With Me
If .NewRecord Then
.txtSomeControl.Enabled = True
.txtSomeControl.Locked = False
ElseIf IsNull(.txtSomeControl) Then
.txtSomeControl.Enabled = True
.txtSomeControl.Locked = False
Else
.txtSomeControl.Enabled = False
.txtSomeControl.Locked = True
End If
End With

It will unlock the control for new records and for existing records where no
entry has been made and Lock it for exiting records where a value exits.
 
D

Debbie S.

Thank you for your quick response. I just tried this, and I got an error
message:

Compile Error: Method or data member not found

It was highlighting the name of the control I had substituted for
".txtsomecontrol" in your code. I substitued as follows: .txtmycontrolnamehere

Is that the correct way to do it?

Thanks again,
Debbie
 
D

Debbie S.

I did put the actual name of the control, I just did not want to post the
name of the control here. It is the actual name of the control. I checked the
spelling and case (upper/lower). I had to change one letter to upper case
that I had mistakenly written as lower case. I still got the same error.
 
K

Klatuu

Did you include the With / End with?
The code as posted should work.
names in Access are not case sensitive, so that would not make a difference.
 
D

Debbie S.

This is exactly what is in the code builder in the current event ("on
current") of the form wrote:

Private Sub Form_Current()
With Me
If .NewRecord Then
.txtTitleOfArticle.Enabled = True
.txtTitleOfArticle.Locked = False
ElseIf IsNull(.txtTitleOfArticle) Then
.txtTitleOfArticle.Enabled = True
.txtTitleOfArticle.Locked = False
Else
.txtTitleOfArticle.Enabled = False
.txtTitleOfArticle.Locked = True
End If
End With

End Sub

What does txt refer to--does that mean text box? Should this code be in the
current event of the text box or the current event of the form?
 
K

Klatuu

There is no current event for a control, only the form. It fires each time
you change records.
txt is a naming convention I use to say "this is a text box control" For
example, my combo boxes always start with cbo, command buttons with cmd, etc.
So if your control name is not exactly txtTitleOfArticle it is a naming
problem. It should be exactly what the name of the control is in the Name
property of the properties dialog for that control.
 
D

Debbie S.

Oh! Well that explains it then. I'll try it without the txt and see what
happens.
Thank you!
Debbie
 
D

Debbie S.

It works perfectly! Thank you so much.

Klatuu said:
There is no current event for a control, only the form. It fires each time
you change records.
txt is a naming convention I use to say "this is a text box control" For
example, my combo boxes always start with cbo, command buttons with cmd, etc.
So if your control name is not exactly txtTitleOfArticle it is a naming
problem. It should be exactly what the name of the control is in the Name
property of the properties dialog for that control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top