Delete button is not the same as RunCmd delete?

F

Frank Schwab

Hello, everybody,

I ran into a very strange behaviour of Access 2000 and 2003 that I
can't explain. Any ideas would be very helpful. Here is what I have:

- A table "tblFiles" with an "autonum" field "filNr" and a Text(30)
field "filName".

- The table has a primary index and an unique, nonulls index on
"filName".

- There is a query "qryFilesOrderedByName" which reads "select *
from tblFiles order by filName;".

- I have a form that has "qryFilesOrderedByName" as it's data source.

- On that form is an unbound combo box which has a data source of
"qryFilesOrderedByName".

- There is also a button "btnDel" that should delete the current record.

- The code of the form reads like this:

Code:
Option Compare Binary
Option Explicit

Private Sub btnDel_Click()
On Error GoTo Err_btnLöschen_Click

DoCmd.SetWarnings False
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True

Exit_btnDel_Click:
Exit Sub

Err_btnDel_Click:
MsgBox Err.Description
Resume Exit_btnLöschen_Click

End Sub

Private Sub cboFile_NotInList(NewData As String, Response As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("tblDateien")

'
' Add the new file to the table
'
rs.AddNew
rs!datName = NewData
rs.Update

rs.Close
Set rs = Nothing
Response = acDataErrAdded
End Sub

Private Sub Form_Current()
cboFile.Value = Me!filNr
End Sub

Private Sub Form_Delete(Cancel As Integer)
Dim i As Long

i = MsgBox("This delete is canceled!", vbExclamation + vbOKOnly,
Delete canceled")

Cancel = True
End Sub

What this effectively does is, that it shows the values of
"tblFiles" and allows to add additional file names. Now for the strange
thing:

If I press the "Del" button I get an error message that "action
RunCmd was cancelled". If I try to delete the record (which doesn't work
since it is cancelled) by pressing the "delete record" button on the
toolbar I don't get this error message.

This is a very stripped down example. The true application does a
lot more when it decides wether the delete should be cancelled or not,
but it has the strange effect that I get a "no current record" error
message when I cancel by pushing the "Del" button and no error message
when I press the "delete record" button on the toolbar.

What is happening here? Any ideas how to remedy this? Any help would
be greatly appreciated. Sorry for posting so much text.


Regards,

Frank
 
A

Allen Browne

There is a known issue with Access 2002 SP2 giving this error, but that's
not relevant for the versions you have.

This code avoids a couple of unnecessary problems:
If Me.Dirty Then Me.Undo
If Not Me.NewRecord Then RunCommand acCmdDeleteRecord

Does that help?

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Frank Schwab said:
Hello, everybody,

I ran into a very strange behaviour of Access 2000 and 2003 that I
can't explain. Any ideas would be very helpful. Here is what I have:

- A table "tblFiles" with an "autonum" field "filNr" and a Text(30)
field "filName".

- The table has a primary index and an unique, nonulls index on
"filName".

- There is a query "qryFilesOrderedByName" which reads "select * from
tblFiles order by filName;".

- I have a form that has "qryFilesOrderedByName" as it's data source.

- On that form is an unbound combo box which has a data source of
"qryFilesOrderedByName".

- There is also a button "btnDel" that should delete the current
record.

- The code of the form reads like this:

Code:
Option Compare Binary
Option Explicit

Private Sub btnDel_Click()
On Error GoTo Err_btnLöschen_Click

DoCmd.SetWarnings False
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True

Exit_btnDel_Click:
Exit Sub

Err_btnDel_Click:
MsgBox Err.Description
Resume Exit_btnLöschen_Click

End Sub

Private Sub cboFile_NotInList(NewData As String, Response As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("tblDateien")

'
' Add the new file to the table
'
rs.AddNew
rs!datName = NewData
rs.Update

rs.Close
Set rs = Nothing
Response = acDataErrAdded
End Sub

Private Sub Form_Current()
cboFile.Value = Me!filNr
End Sub

Private Sub Form_Delete(Cancel As Integer)
Dim i As Long

i = MsgBox("This delete is canceled!", vbExclamation + vbOKOnly, Delete
canceled")

Cancel = True
End Sub

What this effectively does is, that it shows the values of "tblFiles"
and allows to add additional file names. Now for the strange thing:

If I press the "Del" button I get an error message that "action RunCmd
was cancelled". If I try to delete the record (which doesn't work since it
is cancelled) by pressing the "delete record" button on the toolbar I
don't get this error message.

This is a very stripped down example. The true application does a lot
more when it decides wether the delete should be cancelled or not, but it
has the strange effect that I get a "no current record" error message when
I cancel by pushing the "Del" button and no error message when I press the
"delete record" button on the toolbar.

What is happening here? Any ideas how to remedy this? Any help would be
greatly appreciated. Sorry for posting so much text.


Regards,

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top