Run Time error: 3020 when I move off the record (1 Viewer)

debbiedoobie10

Registered User.
Local time
Yesterday, 16:09
Joined
Oct 1, 2010
Messages
13
I get an 'Update or CancelUpdate without AddNew or Edit' error when running the following code. When I move off the record. Does anyone have a suggestions

Function NewNote()
Dim NEWNOTES As String

With CodeContextObject
.[ContBy] = DLookup("[AGENT CODE]", "SALES AGENTS", "[AGENT LOGON] = CURRENTUSER()")
.[This] = Date
.NOTES.SetFocus
NEWNOTES = Format(Now(), "mm/dd/yy h:nna/p") & " " & DLookup("[USER_INIT]", "USER-PIN ", "[USER NAME] = CURRENTUSER()") & " > " & Chr(13) & Chr(10)
.NOTES = NEWNOTES & .NOTES
.NOTES.SelStart = Len(NEWNOTES) - 2
End With
End Function
 

MarkK

bit cruncher
Local time
Yesterday, 16:09
Joined
Mar 17, 2004
Messages
8,187
...when running the following code. When I move off the record.
This is not clear. Moving off a record does not run code, and there is no event that fires (in an Access.Form). How is this code actually being run? How do you detect a MoveOffRecord event?

Also, it is unusual to use CodeContextObject. If this code is on a form, and you are referring to that form, then use Me instead. Or, if this code is called from a form, pass that form to this routine as a parameter, and use that object reference.

hth
Mark
 

nhorton79

Registered User.
Local time
Today, 11:09
Joined
Aug 17, 2015
Messages
147
What’s the event and code that is calling this function?


Sent from my iPhone using Tapatalk
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:09
Joined
Feb 28, 2001
Messages
27,319
The specific error says that something has updated the contents of a recordset but that you failed to notify the recordset you were going to do that. So when you try execute any kind of move of the recordset's bookmark (i.e. .MoveNext, .MoveFirst, .MovePrevious, .MoveLast, or any kind of .Findxxx), you are moving away from a dirty record without properly setting up for its disposition. And the default behavior for Access is that if you are going to move away from a dirty record, you automagically save it.

So the error is the implied save of a dirty record that was not opened as a new record OR a record to be updated.

The "dirt" might have nothing at all to do with the code. The information I see in the presented code doesn't clarify anything. Are those items on the left of your equals-signs the fields in your recordset? Or are they bound controls on a form? Because if that is a form's class module, you should use "Me." to reference controls. If that is an attempt to field an event using only general module code, I would have thought you couldn't make that reference. And if that is code based on a macro RunCode action, then you shouldn't be able to see fields via that mechanism anyway.

MarkK, if the CodeContextObject is pointing to a bound form that is also dirty, it WILL try to do an auto-update of a record if you move away from it. But the part that confuses me is that in such a case, the .AddNew or .Update would have been automatic on navigation TO that record if it was based on a form. So at the moment I can't quite place the context either.
 

debbiedoobie10

Registered User.
Local time
Yesterday, 16:09
Joined
Oct 1, 2010
Messages
13
Thank you for all the help

I have multiple forms with a Call button and the On Click Event runs a macro which runs code NewNote()

I have a Notes field that adds the code “01/31/18 2:58p DLB> “ and the cursor is place at the end of the data for input of the rest of the notes. The entry person enters the rest of the note and gets distracted by checking email or an excel file or another form in access and forgets to save the record set and when they try to exit and save the record they get the error.

How can a save the record with out them exiting?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:09
Joined
Feb 28, 2001
Messages
27,319
Ah, that I can answer easily. If the record in question is properly bound and you already have other command buttons on the form, create one more button to save the current record (and for completeness, you should do an UNDO, or CANCEL button as well.) As it happens, the form designer's command button wizards have presets for both of those exact functions. It is very close to automatic.

But I am still confused because if this is actually a bound form, you should NEVER have to prepare a record for saving. It should be automatic when you navigate to the record (for editing) or when you navigate to a blank record (for new data entry). By the way, the button wizard has a preset for THAT function (add record) too.

Why do you run a macro when the OnClick event could just as well run code directly? What else does the macro do? It is possible that precisely because you ARE running a macro, you have confused the context that should be in force on your form.
 

debbiedoobie10

Registered User.
Local time
Yesterday, 16:09
Joined
Oct 1, 2010
Messages
13
I am new to code and I took this project over for someone that left. Thank you for all your help.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:09
Joined
Feb 28, 2001
Messages
27,319
I can recall taking over someone else's P.O.S. code. That is NOT Point of Sale... the first word is Piece and the second word is Of, but I'm too much a gentleman to tell you the third word. I hope they left you good documentation and liked to include comments in the code.

But somehow I'm doubting that because I know how some people are.
 

Users who are viewing this thread

Top Bottom