Code with Error

  • Thread starter Thread starter Merwin12
  • Start date Start date
M

Merwin12

HI,
I have a delete command button which produces this error:

Update or CancelUpdate without Addnew or Edit

This is the code behind it:
Dim DB As Database
Dim rs As Recordset
Dim msg As String
Dim CR As String

CR = Chr(13)

msg = "Are you sure you want to delete this record?"

If MsgBox(msg, vbQuestion + vbYesNo) = vbNo Then
response = acDataErrContinue
MsgBox " Please try again.", vbInformation, "Project
Storage"
Me.DELETED = ""
Else
'user wants to delete the record
Set DB = DBEngine.Workspaces(0).Databases(0)
'Set DB = CurrentDb
Set rs = DB.OpenRecordset("TblNwkStorage",
dbOpenDynaset)
On Error Resume Next

'Delete the record
rs.Delete
rs![DELETED] = "D"
rs.UPDATE

rs.close
Set rs = Nothing


If Err Then
response = acDataErrContinue
MsgBox Error$ & CR & CR & "Please try again.",
vbExclamation, "Project Storage"
Else
response = acDataErrAdded
End If
End If


I can't find the root of this problem.
Pls. help.
thanks.
 
Actually, after clarifying with user, they don't want to
delete the record but only update the current record the
form is displaying by putting a "D" on the deleted field.
Pls. advise.
Thanks.

-----Original Message-----
Hi,
Yes, well you need to issue an rs.Edit before calling
rs.Update.
However, your code doesn't make sense to me.
You open a recordset from the table, but how do you know
which record is the current one, and therefore which record
you're actually deleting?
And, once you delete this record, which record are you updating?

Are you trying to delete the record your form is currently displaying?

--
HTH
Dan Artuso, Access MVP


HI,
I have a delete command button which produces this error:

Update or CancelUpdate without Addnew or Edit

This is the code behind it:
Dim DB As Database
Dim rs As Recordset
Dim msg As String
Dim CR As String

CR = Chr(13)

msg = "Are you sure you want to delete this record?"

If MsgBox(msg, vbQuestion + vbYesNo) = vbNo Then
response = acDataErrContinue
MsgBox " Please try again.", vbInformation, "Project
Storage"
Me.DELETED = ""
Else
'user wants to delete the record
Set DB = DBEngine.Workspaces(0).Databases(0)
'Set DB = CurrentDb
Set rs = DB.OpenRecordset("TblNwkStorage",
dbOpenDynaset)
On Error Resume Next

'Delete the record
rs.Delete
rs![DELETED] = "D"
rs.UPDATE

rs.close
Set rs = Nothing


If Err Then
response = acDataErrContinue
MsgBox Error$ & CR & CR & "Please try again.",
vbExclamation, "Project Storage"
Else
response = acDataErrAdded
End If
End If


I can't find the root of this problem.
Pls. help.
thanks.


.
 
Back
Top