Insert multiple records with multiple tables having the same ID into a table using DAO in Access

时间:2017-07-10 15:25:16

标签: sql access-vba dao

(1) I have 4 tables (A-Sched,B-Trans,(C-ItemRecep and D-ItemPharm)) with one Lookup table called D-TransDetail. Below is the relationship diagram and the DAO record set for storing records.

enter image description here enter image description here

Public Sub RecpSchedule1()
'Consultation ONLY
Dim db As DAO.Database
Dim rs As DAO.Recordset, rt As DAO.Recordset, rd As DAO.Recordset, ri As    DAO.Recordset
Dim lngTransId As Long
Dim lngItemRecepId As Long


Set db = CurrentDb
Set rs = db.OpenRecordset("Sched")
Set rt = db.OpenRecordset("Trans")
Set ri = db.OpenRecordset("ItemRecep")
Set rd = db.OpenRecordset("TransDetail")

lngItemRecepId = Nz(DMax("ID", "ItemRecep"), 0) + 1 'Next ItemRecep ID
lngTransId = Nz(DMax("ID", "Trans"), 0) + 1 'Next Trans ID

    With rs
        .AddNew
        !SDate = Me.txtSchedDate
        !PatientName = Me.cmbPatientName
        !RegNo = Me.txtRegNo
        !DateOfBirth = Me.txtAge
        !Gender = Me.txtGender
        !PatientClass = Me.PatientClass
        !RecepSchedule = True
        .Update
    End With


    With rt
        .AddNew
        !ID = lngTransId
        !SchedRegNo = Me.txtRegNo
        ![Total_RecepFee] = Me.txtConsFee + ![Total_RecepFee]
       .Update
    End With

    With ri
        .AddNew
        !ID = lngItemRecepId
        !ItemName = "ConsFee"
        !Price = Me.txtConsFee.Value
        !Dept = "Reception"
        .Update
    End With

    With rd
        .AddNew
        !TransID = lngTransId
        !TransID = DMax("ID", "Trans")
        !ItemRecepID = DMax("ID", "ItemRecep")
        .Update
    End With



    rs.Close
    rt.Close
    ri.Close
    rd.Close

 Set rs = Nothing
 Set rt = Nothing
 Set rd = Nothing
 Set ri = Nothing
 Set db = Nothing

 End Sub



 Public Sub RecpSchedule2()

Dim db As DAO.Database
Dim rs As DAO.Recordset, rt As DAO.Recordset, rd As DAO.Recordset, ri As    DAO.Recordset
Dim lngTransId As Long
Dim lngItemRecepId As Long


Set db = CurrentDb
Set rt = db.OpenRecordset("Trans")
Set ri = db.OpenRecordset("ItemRecep")
Set rd = db.OpenRecordset("TransDetail")

lngItemRecepId = Nz(DMax("ID", "ItemRecep"), 0) + 1 'Next ItemRecep ID
lngTransId = Nz(DMax("ID", "Trans"), 0) + 1 'Next Trans ID

    With ri
        .AddNew
        !ID = lngItemRecepId
        !ItemName = "IOPFee"
        !Price = Me.txtIOPFee.Value
        !Dept = "Reception"
        .Update
    End With

    With rd
        .AddNew
        !TransID = lngTransId
        !TransID = DMax("ID", "Trans")
        !ItemRecepID = DMax("ID", "ItemRecep")
        .Update
    End With

    rt.Close
    ri.Close
    rd.Close

 Set rt = Nothing
 Set rd = Nothing
 Set ri = Nothing
 Set db = Nothing
 End Sub

(2) I created 2 Queries ("TransQry" and "SubTransQry") from the tables above.

(3) Finally I created a parent form (frmAccount)containing a child "TransForm" which also Parents it's own child "SubTransForm".

enter image description here

This model works perfect when it is only "ItemRecep"(Table C), but the subforms were not reporting correctly on the parent form the moment I introduce the new "ItemPharm"(Table D). I expected the form to display a new subform transaction Itempharm. (with diff ID i.e 4) containing it's own sub items with Price subtotal. And the Grand Total from the figure above adding up the 2 sub totals.

P.S -Please let me know if my explanation is not understood, I am ready to provide more information

1 个答案:

答案 0 :(得分:0)

解决方案在以下查询中:

SELECT DISTINCT Sched.RegNo
    ,Sched.PatientName
    ,Sched.DateOfBirth
    ,Sched.Gender
    ,Sched.PatientClass
    ,Sum(ItemRecep.Price) AS SumOfPrice
    ,Sched.SDate
    ,Trans.PDate
    ,Avg(Sched.TotalDiscount) AS AvgOfTotalDiscount
    ,Sum([ItemRecep] ! [Price]) - Avg([Sched] ! [AmtPaid]) AS Bal
    ,Avg(Sched.AmtPaid) AS AvgOfAmtPaid
    ,Trans.RecepTotalPayStatus
    ,Sum(Trans.Total_PharmFee) AS SumOfTotal_PharmFee
    ,Sum(Trans.Total_PharmPay) AS SumOfTotal_PharmPay
    ,Sum(Trans.Pharm_Discount) AS SumOfPharm_Discount
    ,Sum(Trans.Total_PharmBal) AS SumOfTotal_PharmBal
    ,Trans.PharmTotalPayStatus
    ,Sum(Trans.Total_OpticalFee) AS SumOfTotal_OpticalFee
    ,Sum(Trans.Total_OpticalPay) AS SumOfTotal_OpticalPay
    ,Sum(Trans.Optical_Discount) AS SumOfOptical_Discount
    ,Sum(Trans.Total_OpticalBal) AS SumOfTotal_OpticalBal
    ,Trans.OpticalTotalPayStatus
    ,Sum(Trans.Total_LabFee) AS SumOfTotal_LabFee
    ,Sum(Trans.Total_LabPay) AS SumOfTotal_LabPay
    ,Sum(Trans.Lab_Discount) AS SumOfLab_Discount
    ,Sum(Trans.Total_LabBal) AS SumOfTotal_LabBal
    ,Trans.LabTotalPayStatus
    ,Sum(Trans.Total_SurgeryFee) AS SumOfTotal_SurgeryFee
    ,Sum(Trans.Total_SurgeryPay) AS SumOfTotal_SurgeryPay
    ,Sum(Trans.Surgery_Discount) AS SumOfSurgery_Discount
    ,Sum(Trans.Total_SurgeryBal) AS SumOfTotal_SurgeryBal
    ,Trans.SurgeryTotalPayStatus
    ,Sum(Trans.Grand_TotalFee) AS SumOfGrand_TotalFee
    ,Sum(Trans.Grand_TotalPay) AS SumOfGrand_TotalPay
    ,Sum(Trans.Grand_TotalDiscount) AS SumOfGrand_TotalDiscount
    ,Sum(Trans.Grand_TotalBal) AS SumOfGrand_TotalBal
    ,Trans.Grand_TotalPayStatus
FROM (
    Sched INNER JOIN Trans ON Sched.RegNo = Trans.SchedRegNo
    )
INNER JOIN (
    ItemRecep INNER JOIN TransDetailRecep ON ItemRecep.ID = TransDetailRecep.ItemRecepID
    ) ON Trans.ID = TransDetailRecep.TransID
GROUP BY Sched.RegNo
    ,Sched.PatientName
    ,Sched.DateOfBirth
    ,Sched.Gender
    ,Sched.PatientClass
    ,Sched.SDate
    ,Trans.PDate
    ,Trans.RecepTotalPayStatus
    ,Trans.PharmTotalPayStatus
    ,Trans.OpticalTotalPayStatus
    ,Trans.LabTotalPayStatus
    ,Trans.SurgeryTotalPayStatus
    ,Trans.Grand_TotalPayStatus
HAVING (
        ((Sched.SDate) = DATE ())
        AND ((Trans.RecepTotalPayStatus) = False)
        )
ORDER BY Sched.PatientName;