Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code behind Command button to delete record 1

Status
Not open for further replies.

THWatson

Technical User
Apr 25, 2000
2,601
CA
Using Access 2016
Donations part of a church congregation database

If the user wants to Delete a donation for a particular donor, the form is opened, the donor selected, and the record desired for deletion selected.

The user has two choices: (1) press Delete on the keyboard, or (2) press a Delete command button

Behind the command button, called cmdDelete, lies this code
Code:
Private Sub cmdDelete_Click()
On Error GoTo Err_cmdDelete_Click

Select Case MsgBox("  Do you really wish to" _
                   & vbCrLf & "   DELETE this record?" _
                   & vbCrLf & "" _
                   & vbCrLf & "This cannot be undone!" _
                   , vbYesNo Or vbExclamation Or vbDefaultButton1, "Delete check")
    Case vbYes
        GoTo DeleteProcess
    Case vbNo
        Exit Sub
End Select
    
DeleteProcess:
    DoCmd.SetWarnings False
    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
    DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
    DoCmd.SetWarnings True

Exit_cmdDelete_Click:
    Exit Sub

Err_cmdDelete_Click:
    MsgBox Err.Description
    Resume Exit_cmdDelete_Click
    
End Sub

This has always worked in the past but doesn't work in Access 2016 (part of the Microsoft Office 365 suite).

Can anyone point me to a way to fix this code?

Thanks!

Tom
 
That is some old code. It is best to avoid DoMenuItem. Try use these lines instead.

Code:
    [highlight #4E9A06]'select and delete the record[/highlight]
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord

Duane
Hook'D on Access
MS Access MVP
 
Duane
Thanks. It's been quite some time since I built that database, and - primarily in view of my age - I'm doing less and less of it now.

It would seem that Access 16 may have made some changes that make old code, such as what I used, redundant.

Thanks again.
Tom
 
Actually, changing the code behind the Delete command button creates another problem.

Now, when I press the Delete command button, I get the message I built in asking whether or not I want to delete the record, yes or no, so I click Yes, and then get the message
Code:
The command or action 'Delete Record' isn't available now

Interestingly enough, selecting the record and hitting the Delete key on the keyboard works.

I have checked the Form;s properties to be sure that "Allow Deletions" is set, and it is.

Not sure what's going on.

Tom
 
Yes, it's a Continuous form.

The Delete Command button is at the top of the form, outside the area where the records show.
 
Is there a way I can send a screen shot on here?
 
It's in the Detail section, in an area across the top, just above where the records for editing show. Right beside it is a command button to "Return to Main Menu"
Tom
 
It's invisible until the user selects the particular record they wish to edit or delete.
 
You could provide your entire code. This is what works for me:

Code:
Private Sub cmdDelete_Click()
On Error GoTo Err_cmdDelete_Click

    If MsgBox("Confirm Delete", vbYesNo + vbQuestion, "Really?") = vbYes Then
        DoCmd.RunCommand acCmdSelectRecord
        DoCmd.RunCommand acCmdDeleteRecord
    End If
Exit_cmdDelete_Click:
    Exit Sub

Err_cmdDelete_Click:
    MsgBox Err.Description
    Resume Exit_cmdDelete_Click
    
End Sub

Duane
Hook'D on Access
MS Access MVP
 
Duane
I apologize. Duane. I did show the original code in the initial post. I didn't post the altered code. Here it is.

Code:
Private Sub cmdDelete_Click()
On Error GoTo Err_cmdDelete_Click

Select Case MsgBox("  Do you really wish to" _
                   & vbCrLf & "   DELETE this record?" _
                   & vbCrLf & "" _
                   & vbCrLf & "This cannot be undone!" _
                   , vbYesNo Or vbExclamation Or vbDefaultButton1, "Delete check")
    Case vbYes
        GoTo DeleteProcess
    Case vbNo
        Exit Sub
End Select
    
DeleteProcess:
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdDeleteRecord
    
    DoCmd.SetWarnings True

Exit_cmdDelete_Click:
    Exit Sub

Err_cmdDelete_Click:
    MsgBox Err.Description
    Resume Exit_cmdDelete_Click
    
End Sub

The only difference I see between your method and mine is that you are using If and End If around the two select and delete lines...whereas I am using a Case statement.

Should that make a difference?
Tom
 
Sorry, one mistake in answering your question.

Since this is an Edit form it's a Single form, not Continuous.

What happens is this:
User opens the form
Down the left side are the all Envelope numbers that are assigned.
The user selects the desired envelope number, and a list of entries for that donor come up.
The user can then edit amounts in a particular record or delete that record.


Tom
 
No subforms.

It's called frmEditNewGivings and is used strictly for editing entries that have been made for donors - to correct mistakes, duplicate entries made.
Tom
 
It's a list box that pulls data from a query.

The code for the query is
Code:
SELECT DISTINCT qryNewGivings.EnvNbr
FROM qryNewGivings
ORDER BY qryNewGivings.EnvNbr;

The list will contain the envelope numbers in which data exists.

qryNewGivings queries data in tblNewGivings

Tom
 
The list box is down the left side. It has the assigned envelope numbers.

When the user selects, say, 719, all donstions, by date given, come up for that envelope number, populated by the data from the query.

I see that I called the form Single. And that Delete command button had always worked until now.

I can change it to Continuous if it will make the difference.
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top