简体   繁体   中英

Deleting/Adding records on a subform that has a similar record source of the main form

I am trying to make a subform that works similarly to a splitform.

I would use splitform but, as far as I could tell, I can't filter only the results of the datasheet portion of the split form, and not the results of the rest. See here... 零件明细表为img

The subformPartDetail only returns records where the OrderID is equal to the mainform's OrderID. There is no filter in the mainform so it returns all partDetail Records regardless of the OrderID. In this particular instance, the main form has 21 records to cycle where as the subform only has four.

An issue occured when I use the subform for adding or deleting records. When I try to use the main form to cycle through the records, the added ones were skipped and the deleted ones threw an error telling me that the record I was attempting to go to has been deleted. I corrected it like this by putting these on the subform events...

Private Sub Form_AfterInsert()
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset

rst.Requery
End Sub

Private Sub Form_Delete(Cancel As Integer)
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset

rst.Requery
End Sub

But say I try to delete the record the mainform is displaying from the subform. That code does not work and will not requery. It throws an error when I cycle away from and then back to that record.

How can I fix this?

I know I can move the record from the deleted one before it deletes by using

 rst.move

I don't know where to began for testing whether the record that is being deleted is the same one that the main form is displaying.

Maybe I'm not understanding the situation correctly, but there shouldn't be any need for VBA to achieve this. If the subform is using the Link Master Fields and the Link Child Fields with your subform, those IDs would work automatically. And if you have a relationship set up between the two tables, if you delete the Order record (and have Enforce Referential Integrity: Cascade Delete) selected, it should clear out the line items automatically to avoid orphan records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM