Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1545
  • Last Modified:

Access 2007 - Prompt and Error while deleting a record

I have a split database. I have a button on a form bound to table (tblCalls).  The intent of this button is to delete the current record.   I am using the following code in the OnClick event to do this deletion:

            SQL = "DELETE * FROM tblCalls WHERE ID = " & Me.ID
            DoCmd.RunSQL SQL

This comes back with the following warning: "You are about to delete 1 row(s) from the specified table".  This is correct, but I would like this question to not appear.  How do you do this?  In Access Options/Advanced I unchecked the Confirm/Document Deletions check box, but that didn't do anything.

If I answer Yes to the above question, I get the following error message "can't delete 0 record(s) in the delete query due to key violations and 1 record(s) due to lock violations.
Selecting Yes to this message is suppose to run the query.  The record is not deleted.  There are no other records associated with this parent record.  I have set "Cascade Delete Related Records" to TRUE.

I have set "Record Locks" on the form to "Edited Record".   The record was not edited. I tried moving to another record with the following code:

            Dim iCurrentID
            iCurrentID = Me.ID
           
            ' Move away from current record so it can be deleted
            DoCmd.GoToRecord acDataTable, "tblCalls", acNext
           
            ' Delete Call - This record must exist
            SQL = "DELETE * FROM tblCalls WHERE ID = " & iCurrentID
            DoCmd.RunSQL SQL

 I get the error "GoToRecord isn't available now".  What does this error message mean?

Thanks for your help

Steve
0
SheahaST
Asked:
SheahaST
  • 3
1 Solution
 
peter57rCommented:
Docmd.setwarnings False

....delete your records

Docmd.setwarnings True
0
 
SheahaSTAuthor Commented:
Thank you.  The above does get rid of the warnings, but the code still does not delete the current record.  Any answers for the rest of my problem?
0
 
SheahaSTAuthor Commented:
Setting the form's Record Locks to "No Locks" and then deleting the record with the above SQL command appears to delete the record from the table but not from the form.  Navigating back and forth gives a "Deleted Record" error, and eventually I reach a reference to a field in the deleted record.
0
 
harfangCommented:
You are in effect trying to delete the current record directly in the table, and not from the current recordset.

To delete the current record, use one of these:

    RunCommand acCmdDeleteRecord
    Me.Recordset.Delete

To delete from the table (but not from the recordset, as you noticed), use

    DoCmd.RunSQL "DELETE FROM tblCalls WHERE ID=123"   ' confirmation msg
    CurrentDb.Execute "DELETE FROM tblCalls WHERE ID=123"   ' no confirmation

Any open recordset containing that record will display a "#deleted" error message. Note also that a form normally locks the current record to prevent just that.

Good luck!
(°v°)
0
 
SheahaSTAuthor Commented:
Thank you very much harfang.  Not only for the solution but for clarifying in my head the differences between a recordset and a table.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now