(1)我有3个表(A-Sched,B-Trans和C-ItemRecep)和一个名为D-TransDetailRecep的Lookup表。下面是关系图。 {{3}}
(2)我通过点击按钮将表格中的值插入到表格字段中,点击按钮实现了下面的DAO:
Public Sub RecpSchedule1() '仅限咨询 Dim db作为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("TransDetailRecep")
lngItemRecepId = Nz(DMax(“ID”,“ItemRecep”),0)+ 1'下一个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] = Nz((Me.txtConsFee + Me.txtIOPFee + ![Total_RecepFee]), 0)
.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("TransDetailRecep")
lngItemRecepId = Nz(DMax(“ID”,“ItemRecep”),0)+ 1'下一个ItemRecep ID lngTransId = Nz(DMax(“ID”,“Trans”),0)+ 1'Next Trans ID
'With rt
' .AddNew
' !ID = lngTransId
'!SchedRegNo = Me.txtRegNo
'![Total_RecepFee] = Nz((Me.txtIOPFee + ![Total_RecepFee]), 0)
'.Update
' End With
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
{{3}} (3)我的挑战:Total_RecepFee的字段应显示500,其中Price为500,并显示1200,其中Price为1200.(我现在的情况是两者都显示值的总和)
答案 0 :(得分:0)
问题在于:
rd!TransID = rt!ID
rd!ItemRecepID = ri!ID
尚未运行rt.Update
和ri.Update
方法来保存记录集。但即便如此,.AddNew
也会创建一条新记录,因此rt!ID
和ri!ID
都是空的。
您可以先尝试保存rt
和ri
个记录集,然后通过DMax
函数检索Max(ID)(因为这将是最后添加的ID)。但是,如果多个用户有权同时添加数据,这可能会有风险(实际上风险很大)。
我建议为什么不删除自动编号ID并手动处理它?</ p>
Dim lngId as Long
lngId = DMax("ID", "Trans") + 1
'You can now feed it to both tables
尝试下面的更新代码。我自己无法测试它。
Public Sub RecpSchedule1()
Dim db As DAO.Database
Dim rs As DAO.Recordset, rt As DAO.Recordset, rd As DAO.Recordset, ri As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Sched")
Set rt = db.OpenRecordset("Trans")
Set ri = db.OpenRecordset("ItemRecep")
Set rd = db.OpenRecordset("TransDetailRecep")
With rs
.AddNew
!SDate = Me.txtSchedDate
!PatientName = Me.cmbPatientName
!RegNo = Me.txtRegNo
.Update
End With
With rt
.AddNew
!SchedRegNo = Me.txtRegNo
!TDate = Me.txtSchedDate
.Update
End With
With ri
.AddNew
!ItemName = "ConsFee"
!Price = Me.txtConsFee.Value
!Dept = "Reception"
.Update
End With
With rd
.AddNew
!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
Set db = CurrentDb
Set rt = db.OpenRecordset("Trans")
Set ri = db.OpenRecordset("ItemRecep")
Set rd = db.OpenRecordset("TransDetailRecep")
With ri
.AddNew
!ItemName = "IOP"
!Price = Me.txtIOPFee.Value
!Dept = "Reception"
.Update
End With
With rd
.AddNew
!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
<强>更新强>
为了手动处理ID,您需要在输入数据时确定表 Trans 和 ItemRecep 的下一个ID。
Dim lngTransId as Long
lngTransId = Nz(DMax("ID", "Trans"),0) + 1 'Next Trans ID
Dim lngItemRecepId as Long
lngItemRecepId = Nz(DMax("ID", "ItemRecep"),0) + 1 'Next ItemRecep ID
With rt
.AddNew
!ID = lngTransId
!SchedRegNo = Me.txtRegNo
!TDate = Me.txtSchedDate
.Update
End With
With ri
.AddNew
!ID = lngItemRecepId
!ItemName = "ConsFee"
!Price = Me.txtConsFee.Value
!Dept = "Reception"
.Update
End With
With rd
.AddNew
!TransID = lngTransId
!ItemRecepID = lngItemRecepId
.Update
End With