I thought this code worked!!!

T

Tony Williams

I have 3 controls on a form that I want to lock to make uneditable after
they have been input. I had the Locked property set to yes and this code in
the On Current event

Private Sub Form_Current()
If Me.NewRecord Then
Me.Action_Date.Locked = False
Else
Me.Action_Date.Locked = True
End If
If Me.NewRecord Then
Me.Action_Time.Locked = False
Else
Me.Action_Time.Locked = True
End If
If Me.NewRecord Then
Me.Action_Log.Locked = False
Else
Me.Action_Log.Locked = True
End If
End Sub

It seems to do what I want it to do but when I run Debug I get an error
message that says method or data member not found and Me.NewRecord is
highlighted.

Also although the data is locked in the form view I can still delete the
records from the table. Is there anyway of making the data not deletable.
This is for a database where there is an Action log and we don't want anyone
able to delete or amend the data in the log once it has been input.
Anyone any clues for me?
Thanks
Tony
 
D

Dirk Goldgar

In
Tony Williams said:
I have 3 controls on a form that I want to lock to make uneditable
after they have been input. I had the Locked property set to yes and
this code in the On Current event

Private Sub Form_Current()
If Me.NewRecord Then
Me.Action_Date.Locked = False
Else
Me.Action_Date.Locked = True
End If
If Me.NewRecord Then
Me.Action_Time.Locked = False
Else
Me.Action_Time.Locked = True
End If
If Me.NewRecord Then
Me.Action_Log.Locked = False
Else
Me.Action_Log.Locked = True
End If
End Sub

It seems to do what I want it to do but when I run Debug I get an
error message that says method or data member not found and
Me.NewRecord is highlighted.

Also although the data is locked in the form view I can still delete
the records from the table. Is there anyway of making the data not
deletable. This is for a database where there is an Action log and we
don't want anyone able to delete or amend the data in the log once it
has been input. Anyone any clues for me?

It looks like it should work, though I don't see why you test
Me.NewRecord three times. Why not just:

If Me.NewRecord Then
Me.Action_Date.Locked = False
Me.Action_Time.Locked = False
Me.Action_Log.Locked = False
Else
Me.Action_Date.Locked = True
Me.Action_Time.Locked = True
Me.Action_Log.Locked = True
End If

?

You say this code is working in practice, but when debugging it doesn't?
How are you debugging it? Are you setting a breakpoint, then opening
the form and letting the Current event fire? That ought to work, and if
it doesn't, there's a problem. Or are you trying to run the code
independently of the form and its events, somehow? That wouldn't work,
because Me must return a reference to a form object.

If you don't want to allow users to delete records from the form, set
the form's AllowDeletions property to No (in the form's design-view
property sheet) or False (in code, at run time). If there are no
circumstances where you'd allow deletions from this form, just set the
property in design view.
 
T

Tony Williams

Thanks Dirk I've used your version of the code.

And I've found my problem! I'd used the form to create a report (used Save
as - report) and of course it still had the code there. It was the code in
the report that was showing the error, I hadn't noticed the report's name in
the VBA screen when the error showed up. It's getting late, a hard day and
things like this tell me it's time for a break and try again in the morning!
Really sorry to have bothered you.
Thanks for the tip on the delete question though.
Cheers
Tony
 
T

Tony Williams

Sorry Dirk I've changed the form as you said on deletions but I can still
delete if I open the table and delete from there. How can I stop deletions
from the table?
Tony
 
D

Dirk Goldgar

In
Tony Williams said:
Sorry Dirk I've changed the form as you said on deletions but I can
still delete if I open the table and delete from there. How can I
stop deletions from the table?

The only way you can do that is to apply user-level security -- quite a
can of worms in itself -- and then only give delete permissions on that
table to certain users or user groups. If you really need to do this,
you can; however, it may be that simply hiding the database window and
disabling the built-in shortcut keys -- things you can do with the
Startup options -- will be enough to keep your users out of the tables.
 
T

Tony Williams

Thanks Dirk I think on this occasion I'll take the easy route as you
suggested in the end of your post.
Thanks anyway
Tony
 

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