[英]MS Access Simple form to populate many to many table
我有一个具有以下表格的简单ms访问数据库:
Patient ID Name Medication ID Name PatientMedication ID PatientID MedicationID
第三张表是患者和药物之间的多对多表-记录每个患者服用的药物。 我想创建一个填充此表的表单,方法是允许我选择一个患者和一种药物,并将新行存储到PatientMedication表中。
我已经完成了使用下拉菜单创建表单的操作,并添加了一个按钮以将选定的行保存到db中,但是不知道如何使该按钮执行插入操作。 我是否需要为按钮编写一些VB代码? 我什至需要一个按钮吗? 看来这很简单,我应该可以通过表单的某些属性来做到这一点。 还是有一种更简单的方法?
任何帮助,将不胜感激。
您实际上可以用几种不同的方式来处理它。 以下是几个选项。
设置格式PatientMedication
的“ Record Source
属性。 将下拉列表Control Source
设置为PatientID
和MedicationID
。 然后,确保PatientMedication
表中的PatientID
和MedicationID
的required
属性设置为Yes
。 然后,当用户为下拉菜单选择一个值时,记录将被添加到数据库中。 假设ID
字段数据类型设置为AutoNumber
。
不要将表单的“ Record Source
属性设置为PatientMedication
。 不要设置下拉列表Control Source
,不要绑定它们。 将MedicationID
下拉列表的Name
属性设置为txtMedicationID
。 将PatientID
下拉列表的Name
属性设置为txtPatientID
。 将命令按钮的Name
属性设置为cmdInsertRecord
。 将以下代码用于按钮的On Click
事件:
Private Sub cmdInsertRecord_Click()
If (VBA.Strings.Len(txtPatientID & "") = 0) Then
MsgBox "You must specify a Patient ID before adding the record.", , "ERROR: Missing Information"
Exit Sub
End If
If (VBA.Strings.Len(txtMedicationID & "") = 0) Then
MsgBox "You must specify a Medication ID before adding the record.", , "ERROR: Missing Information"
Exit Sub
End If
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO PatientMedication (PatientID, MedicationID) Values " & _
"('" & txtPatientID & "', '" & txtMedicationID & "')"
DoCmd.SetWarnings True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.