繁体   English   中英

MS Access简单表格可填充多对多表格

[英]MS Access Simple form to populate many to many table

我有一个具有以下表格的简单ms访问数据库:

Patient
    ID
    Name

Medication
    ID
    Name

PatientMedication
    ID
    PatientID
    MedicationID

第三张表是患者和药物之间的多对多表-记录每个患者服用的药物。 我想创建一个填充此表的表单,方法是允许我选择一个患者和一种药物,并将新行存储到PatientMedication表中。

我已经完成了使用下拉菜单创建表单的操作,并添加了一个按钮以将选定的行保存到db中,但是不知道如何使该按钮执行插入操作。 我是否需要为按钮编写一些VB代码? 我什至需要一个按钮吗? 看来这很简单,我应该可以通过表单的某些属性来做到这一点。 还是有一种更简单的方法?

任何帮助,将不胜感激。

您实际上可以用几种不同的方式来处理它。 以下是几个选项。

选项1:

设置格式PatientMedication的“ Record Source属性。 将下拉列表Control Source设置为PatientIDMedicationID 然后,确保PatientMedication表中的PatientIDMedicationIDrequired属性设置为Yes 然后,当用户为下拉菜单选择一个值时,记录将被添加到数据库中。 假设ID字段数据类型设置为AutoNumber

选项2:

不要将表单的“ 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.

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