Dreaded RunCommand Has Been Canceled

JamesJoey

Registered User.
Local time
Today, 05:06
Joined
Dec 6, 2010
Messages
642
When clicking no on the delete record dialog I get that RunCommand has been canceled.

Code:
Public Function GlobalDelete()

    DoCmd.RunCommand acCmdDeleteRecord
    
End Function

How do I eliminate this???

James
 
Thanks for refreshing my memory on error handling.

Works fine.

James
 
Happy to help James.
 
A lot of more experienced Access developers I've met will avoid deleting records using this method (or any DoCmd at all, for that matter). It can sometimes be a bit tricky in that it operates on the currently active object, which can sometimes be unexpected.

A safer way to do it is to issue an appropriate SQL command to delete the record in question, albeit a bit more work.

Food for thought. Cheers,
 
Good point Jack. I typically execute SQL.
 
I've been looking into Delete query.
But can't seem to get it to work.

Code:
Dim strCurRec As String

    strCurRec = "DELETE FROM tblReminders WHERE Me!ReminderID"

When I click the delete button nothing happens.
I never understood the syntax for this stuff.
 
Currently you have the Me!ReminderID hardcoded as part of the literal string. You need to concatenate that value into the SQL string:

Code:
strCurRec = "DELETE FROM tblReminders WHERE ReminderID = " & Me!ReminderID

Be sure to test the code against a test dataset! Delete operations via SQL can be catastrophic against live data sets if it's not done correctly (if you forget the where clause, for example).

The full code I'd expect to be something like:

Code:
With CurrentDb
    .Execute "DELETE FROM tblReminders WHERE ReminderID = " & Me.ReminderID, dbFailOnError
    Debug.Print "Deleted " & .RecordsAffected & " record(s)"
End With

(I prefer CurrentDb.Execute over DoCmd.RunSQL)
 

Users who are viewing this thread

Back
Top Bottom