Edit --> Delete & Delete Button on a form.

D

Dennis

Hi,

I’m new to Access & VBA, but many years of coding experience.

OS: XP Pro – SP3
Access: 2003 – SP3

Issue:
I’m having a problem getting a Delete Button to work. I want to delete the
current record from the table on a simple name and address form. No
sub-screens involved.

There are two issues:
1. Sometimes it tells me that there is no current record even though I can
see it on the screen.
2. Sometime Events fire in wrong sequence.


1. I can see the record on the screen, but it tells me there is no current
record. When I click on the Edit on the Access Main Menu, there is no Select
Record or Select All visible on the drop down menu. This is intermittent.

2. As part of my testing, I’ll click on the Delete Button three or four
times and answer No each time. The normal events that fire (based on
Debug.Print) are:

cbDelMem_Click
From_Delete

Every once in a while, the events that fire are:

cbDelMem_Click
Form_BeforeDelConfirm
Form_AfterDelConfirm - which displays the “member†was deleted msgbox
Form_Delete – which displays “Ok to delete member�

My questioin is what is causing the Form_BeforeDelConfirm and
Form_AfterDelConfirm to fire before the Form_Delete event and how do I fix it?

History
Been reading Delete Button items on the internet for the last week try to
solve it. But I’m afraid it Access 1 Me 0.

Following is the code:

Private Sub cbDelMem_Click()
On Error GoTo Err_cbDelMem_Click

Debug.Print (“cbDelMem_Clickâ€)
If Me.Dirty Then
Me.Undo
End If
If Not Me.NewRecord Then
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
End If

Exit_cbDelMem_Click:
Exit Sub

Err_cbDelMem_Click:

' If the Delete Command was cancelled, Access generates a RunCmdCancel
error message.
' We do not want to display this message, so we will not display the
RunCmdCancel message.
'
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description, "cbDelMem_Click")
Resume Exit_cbDelMem_Click

End Sub

==========================================================

Private Sub Form_AfterDelConfirm(Status As Integer)
On Error GoTo Err_Form_AfterDelConfirm

Debug.Print ("Form_AfterDelConfirm")
DoCmd.SetWarnings True ' Turn on Access
delete msg - turned off in B4 Del Confirm

glngMbButton = vbOKOnly + vbInformation
gstrMbTitle = "After Del Confirmation "

Select Case Status
Case acDeleteOK
gstrMbText = gstrMemName & " was deleted." ' Position new
record after delete!!!
Case acDeleteCancel
gstrMbText = gstrMemName & " was NOT deleted" & vbCrLf & vbCrLf
gstrMbText = gstrMbText & "Programmer canceled the deletion."
Case acDeleteUserCancel
gstrMbText = gstrMemName & " was NOT deleted" & vbCrLf & vbCrLf
gstrMbText = gstrMbText & "You canceled the deletion."
End Select
gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle)

Exit_Form_AfterDelConfirm:
Exit Sub


Err_Form_AfterDelConfirm:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description,
"Form_AfterDelConfirm ")
Resume Exit_Form_AfterDelConfirm

End Sub

==========================================================

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
On Error GoTo Err_Form_BeforeDelConfirm

Debug.Print ("Form_BeforeDelConfirm")
DoCmd.SetWarnings False ' Turn off Access generated
delete msg
' Turn on Access generated
delete msg in AfterDelConfirm
Exit_Form_BeforeDelConfirm:
Exit Sub

Err_Form_BeforeDelConfirm:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description,
"Form_BeforeDelConfirm ")
Resume Exit_Form_BeforeDelConfirm

End Sub

============================================================

Private Sub Form_Delete(Cancel As Integer)
On Error GoTo Err_Form_Delete

' A custom Ok to Delete Member Msg Box is done in the BeforeDelConfirm
event on the Form
' Properties. The BeforeDelConfirm event is processed as a result of
running the
' acCmdDeleteRecord.
'
' If the user agrees to the ok to delete, then the member record is
deleted.
' If the user does not agree to the ok to delete, then the member record
is restored.

Dim sSp6 As String
Debug.Print ("Form_Delete")

sSp6 = " " ' 6 spaces
gstrMemName = "'" & [FirstName] & " " & [LastName] & "'"

gstrMbTitle = "Ok to Delete Member?"
glngMbButton = vbYesNo + vbExclamation + vbDefaultButton2
gstrMbText = "Do you want to delete " & gstrMemName & "?" & sSp6 &
vbCrLf & vbCrLf
gstrMbText = gstrMbText & "Yes = Yes, Delete the member's information."
& vbCrLf
gstrMbText = gstrMbText & "No = No, Do NOT delete the member's
information"

gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle) ' OK to
delete member? Msgbox

Select Case gintMbResp ' Process user
response to Ok to Delete?
Case vbYes ' User click YES,
delete member
Cancel = False
Case Else
Cancel = True ' Setting Cancel
= True cancels the
glngMbButton = vbOKOnly + vbInformation ' Delete event!
Display msg to let
gstrMbText = gstrMemName & " was NOT been deleted" '
user know.
gstrMbTitle = "Member NOT Deleted"
gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle)
End Select

Exit_Form_Delete:
Exit Sub

Err_Form_Delete:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description, "Form_Delete ")
Resume Exit_Form_Delete

End Sub
 
A

Allen Browne

Try something like this in your code:

Private Sub cmdDeleteRecord_Click()
If Me.Dirty Then Me.Undo
If Not Me.NewRecord Then RunCommand acCmdDeleteRecord
End Sub

If you click the button while an edit is in progress, Access has to deal
with that first. Since you want to dump the record, it makes sense to undo
it rather than let Access save it and delete it (and it avoids any issues if
the record cannot be saved.)

After that, you may find you are at a new record, which can't be deleted.
Avoiding these 2 cases may solve your issues.

The 'No Current Record' issue is a known bug with A2002 SP3. Just add error
handling to ignore the error in the (AfterDelConfirm?) event.

The order of events you describe is weird. What it should do is fire the
Delete event first, once for *each* record being deleted. (That's one only
if the command button is clicked, but could be multiples in a
datasheet/continuous form if you use other methods such as the toolbar.) It
will be different if you use an ADP (not an MDB):
http://support.microsoft.com/kb/234866/

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

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

Dennis said:
Hi,

I’m new to Access & VBA, but many years of coding experience.

OS: XP Pro – SP3
Access: 2003 – SP3

Issue:
I’m having a problem getting a Delete Button to work. I want to delete
the
current record from the table on a simple name and address form. No
sub-screens involved.

There are two issues:
1. Sometimes it tells me that there is no current record even though I
can
see it on the screen.
2. Sometime Events fire in wrong sequence.


1. I can see the record on the screen, but it tells me there is no
current
record. When I click on the Edit on the Access Main Menu, there is no
Select
Record or Select All visible on the drop down menu. This is intermittent.

2. As part of my testing, I’ll click on the Delete Button three or four
times and answer No each time. The normal events that fire (based on
Debug.Print) are:

cbDelMem_Click
From_Delete

Every once in a while, the events that fire are:

cbDelMem_Click
Form_BeforeDelConfirm
Form_AfterDelConfirm - which displays the “member†was deleted msgbox
Form_Delete – which displays “Ok to delete member�

My questioin is what is causing the Form_BeforeDelConfirm and
Form_AfterDelConfirm to fire before the Form_Delete event and how do I fix
it?

History
Been reading Delete Button items on the internet for the last week try to
solve it. But I’m afraid it Access 1 Me 0.

Following is the code:

Private Sub cbDelMem_Click()
On Error GoTo Err_cbDelMem_Click

Debug.Print (“cbDelMem_Clickâ€)
If Me.Dirty Then
Me.Undo
End If
If Not Me.NewRecord Then
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
End If

Exit_cbDelMem_Click:
Exit Sub

Err_cbDelMem_Click:

' If the Delete Command was cancelled, Access generates a RunCmdCancel
error message.
' We do not want to display this message, so we will not display the
RunCmdCancel message.
'
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description,
"cbDelMem_Click")
Resume Exit_cbDelMem_Click

End Sub

==========================================================

Private Sub Form_AfterDelConfirm(Status As Integer)
On Error GoTo Err_Form_AfterDelConfirm

Debug.Print ("Form_AfterDelConfirm")
DoCmd.SetWarnings True ' Turn on Access
delete msg - turned off in B4 Del Confirm

glngMbButton = vbOKOnly + vbInformation
gstrMbTitle = "After Del Confirmation "

Select Case Status
Case acDeleteOK
gstrMbText = gstrMemName & " was deleted." ' Position new
record after delete!!!
Case acDeleteCancel
gstrMbText = gstrMemName & " was NOT deleted" & vbCrLf & vbCrLf
gstrMbText = gstrMbText & "Programmer canceled the deletion."
Case acDeleteUserCancel
gstrMbText = gstrMemName & " was NOT deleted" & vbCrLf & vbCrLf
gstrMbText = gstrMbText & "You canceled the deletion."
End Select
gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle)

Exit_Form_AfterDelConfirm:
Exit Sub


Err_Form_AfterDelConfirm:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description,
"Form_AfterDelConfirm ")
Resume Exit_Form_AfterDelConfirm

End Sub

==========================================================

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
On Error GoTo Err_Form_BeforeDelConfirm

Debug.Print ("Form_BeforeDelConfirm")
DoCmd.SetWarnings False ' Turn off Access generated
delete msg
' Turn on Access generated
delete msg in AfterDelConfirm
Exit_Form_BeforeDelConfirm:
Exit Sub

Err_Form_BeforeDelConfirm:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description,
"Form_BeforeDelConfirm ")
Resume Exit_Form_BeforeDelConfirm

End Sub

============================================================

Private Sub Form_Delete(Cancel As Integer)
On Error GoTo Err_Form_Delete

' A custom Ok to Delete Member Msg Box is done in the BeforeDelConfirm
event on the Form
' Properties. The BeforeDelConfirm event is processed as a result of
running the
' acCmdDeleteRecord.
'
' If the user agrees to the ok to delete, then the member record is
deleted.
' If the user does not agree to the ok to delete, then the member
record
is restored.

Dim sSp6 As String
Debug.Print ("Form_Delete")

sSp6 = " " ' 6 spaces
gstrMemName = "'" & [FirstName] & " " & [LastName] & "'"

gstrMbTitle = "Ok to Delete Member?"
glngMbButton = vbYesNo + vbExclamation + vbDefaultButton2
gstrMbText = "Do you want to delete " & gstrMemName & "?" & sSp6 &
vbCrLf & vbCrLf
gstrMbText = gstrMbText & "Yes = Yes, Delete the member's information."
& vbCrLf
gstrMbText = gstrMbText & "No = No, Do NOT delete the member's
information"

gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle) ' OK to
delete member? Msgbox

Select Case gintMbResp ' Process user
response to Ok to Delete?
Case vbYes ' User click
YES,
delete member
Cancel = False
Case Else
Cancel = True ' Setting
Cancel
= True cancels the
glngMbButton = vbOKOnly + vbInformation ' Delete event!
Display msg to let
gstrMbText = gstrMemName & " was NOT been deleted" '
user know.
gstrMbTitle = "Member NOT Deleted"
gintMbResp = MsgBox(gstrMbText, glngMbButton, gstrMbTitle)
End Select

Exit_Form_Delete:
Exit Sub

Err_Form_Delete:
Call BaseUtil.Dsp_Err_Msg(Err.Number, Err.Description, "Form_Delete ")
Resume Exit_Form_Delete

End Sub
 
D

Dennis

Allen,

Thanks for the quick response and I hope you having a wonderful day in
Perth. t

A couple of comments.

The code you gave and mine are the same except for I have RunCommand
acCmdSelectRecord before the acCmdDeleteRecord line. Just to confirm -
should I delete the "RunCommand acCmdSelectRecord" line?

I can code around the No Current Record bug. Thanks for that info.

The order of event happens very intermintently. I can not find a pattern to
when it happens.
 
A

Allen Browne

Dennis said:
The code you gave and mine are the same except for I have RunCommand
acCmdSelectRecord before the acCmdDeleteRecord line. Just to confirm -
should I delete the "RunCommand acCmdSelectRecord" line?

Give it a try: let us know if you need both.
 
D

Dennis

Allen,

I did some reading and supposedly the acCmdSelectRecord selects the current
reocrd. I don't know as I could not figure out a way to prove it. I comment
out the code and the debug.print did not show any additional events fired. I
decided to leave it in because the build code wizard stuck it there.

New question. Is there an easy way to get Access to display the next or
previous record (if I deleted last record) after the current record is
deleted? Below is the code I came up with. It seems awkward.




If Me.Dirty Then
Me.Undo
End If
If Not Me.NewRecord Then
On Error Resume Next
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
If Err.Number <> 0 And Err.Number <> intCouldNotFindObj Then
GoTo Err_cbDelMem_Click
End If
If Me.Recordset.RecordCount > 0 Then
If Me.CurrentRecord > Me.Recordset.RecordCount Then
DoCmd.GoToRecord , , acPrevious
Else
DoCmd.GoToRecord , , acNext
End If
End If
End If
 
D

Dennis

Allen,

Thanks for your help.

By the way, I think I figured out the out of sequence events I dicussed.
I'm using debugger to watch the code execute. sometime I use the Run -->
Reset option in the middle of a delete option. It seems that this cause some
flags to not be reset so the next time I click on the delete button, it
finished off the previouse delete (before & after del confirm) with the
current member's name. My guess anyhow - any thoughs on my leap of
imagination?

Dennis
 
A

Allen Browne

Dennis said:
By the way, I think I figured out the out of sequence events I dicussed.
I'm using debugger to watch the code execute. sometime I use the Run -->
Reset option in the middle of a delete option. It seems that this cause
some
flags to not be reset ...

Yes: that could mess up what your flags are telling you.

FWIW, I never edit running code (in break mode) as I suspect this can
contribute to corruption.
 
A

Allen Browne

It's not needed.

Best to just work with the default behavior here (in terms of displaying the
next/previous record after a deletion.) IMHO, there's too many factors going
on here, including the A2002 SP3 bug.
 
D

Dennis

Thank you for all of your help. Life is good again - well at least until I
run head first into my next Access challenge.
 

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