Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runtime error 2166

Status
Not open for further replies.

bw2601

Technical User
Dec 16, 2004
29
US
hi

I am trying to lock a field in my table so after a user enters data it will lock. This is my code:

private sub fieldname_change()

if not isnull(me.fieldname) then
me.fieldname.locked = true
else
me.fieldname.locked = false
end iff

end sub

when i try to test it i get a runtime error 2166 "cannot lock a control while it has unsaved changes."

can any one help me figure out why i am getting this error.

thanks
 
Code:
[blue]private sub fieldname_change()

[purple][b]Docmd.RunCommand acCmdSaveRecord[/b][/purple]

if not isnull(me.fieldname) then
me.fieldname.locked = true
else
me.fieldname.locked = false
end iff

end sub[/blue]
And if the user makes a typo?

Calvin.gif
See Ya! . . . . . .
 
Sry that did not work. I still get the same error, and depending on where i place the code i also get this error:

runtime 3101, Microsoft jet database engine cannot find matching field(s) "fieldname"

any suggestions??
please?
 
bw2601 . . . . .

Sorry . . . I didn't pay attention to your use of the [blue]Change[/blue] event.

Hmmmmm . . . what is your [blue]definition of locked[/blue] as far as your trying to use it?

Is this on a network?



Calvin.gif
See Ya! . . . . . .
 
i am going to try to be specific:

i have a form that when a user enters information and hits tab or click to another field, i want the field they were on to lock after they enter the information. this way if they try to go back and change something on this form, they can't. i hope this helps.

thanks
 
OK bw2601 . . . . . much better!

Normally you can [blue]lock/disallow editing[/blue] of [purple]saved records[/purple], not individual fields. That is to say for your case, you can't [blue]lock/disallow editing[/blue] when a record is in [purple]Edit Mode[/purple] (user is entering data). [blue]A record stays in edit mode until its saved.[/blue]

So . . . since you can't lock in edit mode as you require, another approach is warranted.

[blue]Suggestion:[/blue]
[ol][li]First, locking is set to [blue]only allow editing of a new record.[/blue][/li]
[li]You'll need an [blue]unbound checkbox[/blue] for each control that receives user input (these can be hidden on the form!). This is for tracking those controls (thru their AfterUpdate events) that received data.[/li]
[li]Code will check the tracking and [blue]send the focus[/blue] to the next empty control in the tab order. The [blue]OnEnter[/blue] event of [blue]all controls[/blue] of interest will be used to trigger the code.[/li]
[li]When tracking is complete (all checkboxes are True) or the user goes to a new record, the record is saved and [blue]checkboxes are reset.[/blue][/li][/ol]
A bit more than what you wanted, but I don't see any other way around it.

So . . . Ya Think!


Calvin.gif
See Ya! . . . . . .
 
ok,

I have a question for you then. Because it is not possible to lock the field after something is entered, is it possible to lock the whole form after it is saved, so when a user goes back into it after it has been saved, they would not be able to make changes to it? and if this is possible can you give me some pointers on how to get started on this?

i would really appreciate it and thank you, you have been so helpful already.
 
bw2601 . . . . .

Very easily. In the [blue]OnCurrent[/blue] event of the form, copy paste the following:
Code:
[blue]   If Me.NewRecord Then
      Me.AllowEdits = True
   Else
      Me.AllowEdits = False
   End If[/blue]
Users can only edit [purple]New Records![/purple]

Calvin.gif
See Ya! . . . . . .
 
Dude, you rock on so many levels right now. i don't know how i am going to be able to thank you enough.

you are awsome everything works great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top