|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm trying to develop a cmdDel button that enables a user to delete a record from 2 tables. The master form is bound to tblInvoice while the subform form (child) is bound to tblInnov (1 to 1 relationship).
Data is only being deleted in 1 table that being tblInvoice (master or main form). I'm using 2 recordsets to access each table. The form is also not being repositioned correctly after the deletion (ie: movenext or moveprevious doesn;t seem to be working). Any suggestion would be much appreciated as I'm only a beginner at VBA & would appreciate any insight. Thanks Code: Dim db As Database Dim recInv As DAO.Recordset Dim recInn As DAO.Recordset Dim strDel As String Set db = CurrentDb If (Me.txtInvoiceID.Value = " " Or IsNull(Me.txtInvoiceID)) Then MsgBox "Error: Record not updatable." Exit Sub End If Set recInv = db.OpenRecordset("SELECT * FROM tblInvoice WHERE InnovId = " & Me.txtInnovID.Value, dbOpenDynaset) Set recInn = db.OpenRecordset("SELECT * FROM tblInnov WHERE InnovId = " & Me.txtInnovID.Value, dbOpenDynaset) If (IsNull(recInv("InnovID")) Or recInv.RecordCount <> 1) Then MsgBox "An error occurred while retrieving record for deletion. Try again later." Exit Sub End If If (recInv.Updatable = False Or recInn.Updatable = False) Then 'verify if record can be updated MsgBox "Record cannot be updated. Sorry." Exit Sub End If strDel = InputBox(Prompt:="Are you sure you wish to delete record? (y/n)", title:="Delete Record", _ xpos:=2000, ypos:=2000) strDel = LCase$(strDel) If (strDel = "y" Or strDel = "yes") Then 'if updatable, delete record after finding it If (recInv.BOF) Then recInv.Delete 'delete and then move to previous record recInn.Delete recInv.MoveNext recInn.MoveNext recInv.Close recInn.Close db.Close Exit Sub ElseIf (recInv.EOF) Then 'if first record or only record recInv.Delete 'delete and then move to previous record recInn.Delete recInv.MovePrevious recInn.MovePrevious recInv.Close recInn.Close db.Close Exit Sub Else recInv.Delete 'delete and then move to previous record recInv.MoveNext recInn.MoveNext recInv.Close recInn.Close db.Close Exit Sub End If ElseIf (strDel = "n" Or strDel = "no") Then MsgBox "Deletion aborted." recInv.Close recInn.Close db.Close Exit Sub Else MsgBox "Invalid entry.Try again." recInv.Close recInn.Close db.Close Exit Sub End If |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > deleting a record from multiple tables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|