andrewf10
Registered User.
- Local time
- Today, 10:50
- Joined
- Mar 2, 2003
- Messages
- 114
Hi
I have a database with a setup kind of like this.
Form1 for Table1, Form 2 for Table2 and Form 3 for Table3. There are no relationships set.
All 3 tables have an ID field, which 90% of the time, will have the same ID number on them.
What I'm trying to do is set up a button on Form1 which when clicked, will delete the current record on this form, then go to Tables 2 and 3 and delete the same record from them.
Can someone please advise me on the best way of doing this? Below is the code I've managed to get working...with 1 flaw.
If for whatever reason the current ID is not found on Tables 2 or 3, it goes and deletes the first record. I'd prefer the code to move onto the next table without deleting anything if it didnt find a matching ID.
Any help or advice would be hugely appreciated as I've used up my little bit of VBA knowhow getting this far!
Many thanks
Andrew
Private Sub DELETE_Click()
On Error GoTo Err_DELETE_Click
Dim resp As String
Dim currentrecord As String
Dim currentdescription As String
'Capture the current ID and Description for the message box'
currentrecord = Me.ID
currentdescription = Me.Description
resp = MsgBox("Are you sure you want to delete " & currentrecord & " - " & currentdescription & "?", vbYesNo, "Confirm deletion")
'If user confirms deletion, Table2 is opened on the current ID number and the deletion code is executed'
If resp = vbYes Then
DoCmd.OpenTable "Table2"
DoCmd.FindRecord Forms!Form1!ID
DoCmd.SetWarnings False 'System warning message is turned off
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
DoCmd.Close
'Table3 is opened on the current ID number and the deletion code is executed'
DoCmd.OpenTable "Table3"
DoCmd.FindRecord Forms!Form1!ID
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
DoCmd.Close
'Form1, which has been open all the time, has its current ID number deleted'
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
'If the user cancels the deletion, the code is exited'
ElseIf resp = vbNo Then
Exit Sub
End If
Exit_DELETE_Click:
Exit Sub
Err_DELETE_Click:
MsgBox Err.Description
Resume Exit_DELETE_Click
End Sub
I have a database with a setup kind of like this.
Form1 for Table1, Form 2 for Table2 and Form 3 for Table3. There are no relationships set.
All 3 tables have an ID field, which 90% of the time, will have the same ID number on them.
What I'm trying to do is set up a button on Form1 which when clicked, will delete the current record on this form, then go to Tables 2 and 3 and delete the same record from them.
Can someone please advise me on the best way of doing this? Below is the code I've managed to get working...with 1 flaw.
If for whatever reason the current ID is not found on Tables 2 or 3, it goes and deletes the first record. I'd prefer the code to move onto the next table without deleting anything if it didnt find a matching ID.
Any help or advice would be hugely appreciated as I've used up my little bit of VBA knowhow getting this far!
Many thanks
Andrew
Private Sub DELETE_Click()
On Error GoTo Err_DELETE_Click
Dim resp As String
Dim currentrecord As String
Dim currentdescription As String
'Capture the current ID and Description for the message box'
currentrecord = Me.ID
currentdescription = Me.Description
resp = MsgBox("Are you sure you want to delete " & currentrecord & " - " & currentdescription & "?", vbYesNo, "Confirm deletion")
'If user confirms deletion, Table2 is opened on the current ID number and the deletion code is executed'
If resp = vbYes Then
DoCmd.OpenTable "Table2"
DoCmd.FindRecord Forms!Form1!ID
DoCmd.SetWarnings False 'System warning message is turned off
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
DoCmd.Close
'Table3 is opened on the current ID number and the deletion code is executed'
DoCmd.OpenTable "Table3"
DoCmd.FindRecord Forms!Form1!ID
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
DoCmd.Close
'Form1, which has been open all the time, has its current ID number deleted'
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 'Select record'
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 'Delete the selected record'
'If the user cancels the deletion, the code is exited'
ElseIf resp = vbNo Then
Exit Sub
End If
Exit_DELETE_Click:
Exit Sub
Err_DELETE_Click:
MsgBox Err.Description
Resume Exit_DELETE_Click
End Sub