How to delete selected record in MS Access (VBA) table within a form (1 Viewer)

2k7mmurtaza

New member
Local time
Today, 11:32
Joined
Mar 5, 2018
Messages
5
I have a view bookings page where once customers make a booking it shows their list of all bookings they have made.

viewbookings.jpg

The issue that I am getting is it doesn't allow me to delete a selected record and only keeps deleting the record which is on the top despite selecting another record. I inserted an error message which doesn't delete past booking dates because of keeping them as historical records. I only want to be able to delete future tense bookings. How do i delete selected records?

This is my current coding to delete a booking

Code:
Private Sub btnDelete_Click()


If Day <= Now Then
MsgBox "Past booking dates are for historical records and cannot be deleted"
Else
Msg = "Are you sure you want to cancel booking"
   Style = vbYesNo
   Response = MsgBox(Msg, Style)
   If Response = vbYes Then
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdDeleteRecord
    Exit Sub
   End If
   If Response = vbNo Then Me.Undo
      Exit Sub
    End If
Exit_ErrHandler:
Exit Sub
End If
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:32
Joined
Aug 30, 2003
Messages
36,118
It will/should delete the record with focus. I would have the form in continuous view and have the button in the detail section. I'd either use the code created by the button wizard or execute SQL that specified the record.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:32
Joined
Feb 19, 2002
Messages
42,981
Your delete button is not on the form that holds the record you want to delete so without using the unique ID of the record you want to delete, it deletes the first record in the recordset.

The simplest solution is to put a hidden control on the form with the button. Then in the Current event of the DS form, copy the ID of the current record to the hidden control.

Me.Parent.txtHidden = Me.UniqueID

Then when you press the delete button, the delete query should reference the value in the hidden button.

Delete * From YourTable Where UniqueID = Forms!yourmainform!txtHidden;
 

Users who are viewing this thread

Top Bottom