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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update / CancelUpdate without AddNew / Edit Error

Status
Not open for further replies.

MHadden

Programmer
May 13, 2001
105
I am using the following code, but keep getting the following error "Update or CancelUpdate without Addnew or Edit"

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim MyDB As Database
Dim MySet As Recordset

Set MyDB = CurrentDb
Set MySet = MyDB.OpenRecordset("Updates")

MySet.AddNew
MySet!FirstName = Me!FirstName.OldValue
MySet!LastName = Me!LastName.OldValue
MySet!ServiceType = Me!ServiceType.OldValue
MySet!LastConcCheck = Me!LastConcCheck
MySet!DateChanged = Now()
MySet!User = CurrentUser()
MySet.Update
MySet.close
MyDB.close

End Sub

I have studied the help files, and this forum, but can't find an answer to why it isn't working. It appears to me to be accurate & have the right syntax, but who knows? Can anyone help?
TIA - Michael
MichaelHadden@AltaVista.Com
If you give someone a fish, you have given them a meal. If you teach someone to fish, you have given them MANY meals!
 
It looks like you are trying to update a form with a recordset that does not exist. Instead using that code on the Before Update Event Try Using this on the OnCurrent Event.

If Me.NewRecord Then
Dim MyDB As Database
Dim MySet As Recordset

Set MyDB = CurrentDb
Set MySet = MyDB.OpenRecordset("Updates")

MySet.MoveLast
MySet.AddNew

Me.FirstName = MySet![FirtsName]
Me.LastName = MySet![LastName]
Me.ServiceType = MySet![ServiceType]
Me.LastConcCheck = MySet![LastConcCheck]

'Not Sure if you are trying to put this on form or in recordset.

'For recordset use this code.
MySet![DateChanged] = Now()
MySet![User] = CurrentUser()

'For Form use this code. And Remove the MySet.AddNew and The MySet.Update from this code.
Me.DateChanged = Now()
Me.User = CurrentUser()

MySet.Update
MySet.close
MyDB.close
End If


HTH
 
Thanks for the tip, I will give it a try. What I am trying to do is to use this like an audit trail. When a record is changed, it will copy the changes, date, and user to a table named "Updates". I also have this in the Delete event to copy the ones that are deleted to a table called "Deleted". The deleted files seem to copy over fine, but when I try to update any data on the form, I get this error message. I shluod have put this in my initial post, but I didn't want it to get too wordy. I tend to be long - winded.
Thanks Again,
Michael MichaelHadden@AltaVista.Com
If you give someone a fish, you have given them a meal. If you teach someone to fish, you have given them MANY meals!
 
You should Have put it in first What I wrote the Firstime Will not work. I assumed you were trying to put the last valuse entered on a form into a new record. What you had the first time should work try putting the brackets around the field names for example
MySet![FirstName] = Me!FirstName.OldValue

You need these brackets to Identify the Field names in the recordset
You also may want to check to see if there was a change for that particular field
 
Will do! Thanks again, Sorry for the confusion.
Michael MichaelHadden@AltaVista.Com
If you give someone a fish, you have given them a meal. If you teach someone to fish, you have given them MANY meals!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top