简体   繁体   English

Dynamic Checkbox MS Access 2013

[英]Dynamic Checkbox MS Access 2013

I would like to create a form, where checkboxes are created dynamically depending on the records of another table. 我想创建一个表单,其中复选框是根据另一个表的记录动态创建的。

Example: 例:

Source Table content:
ID     Name
1      ABC
2      DEF
3      GHI
4      JKL

I would like that the form will generate 4 checkboxes on loading. 我希望表单在加载时会生成4 checkboxes

How to achive this? 如何实现这一目标?

One way to accomplish this would be to use a subform. 实现此目的的一种方法是使用子表单。 Say the main form is named [frmTest]. 假设主窗体名为[frmTest]。 Create a table named [frmTest_CheckBoxItems] with the following fields: 使用以下字段创建名为[frmTest_CheckBoxItems]的表:

[Seq]: Long Integer, Primary Key  
[Description]: Text(255)  
[Selected]: Yes/No  

Then you could add a Continuous Forms subform to [frmTest] that includes the [Selected] (Check Box) and [Description] (Text Box) fields. 然后,您可以将[连续表单]子表单添加到[frmTest],其中包括[Selected](复选框)和[Description](文本框)字段。

In the Form Load event of the main form [frmTest] you could load the values from the [SourceTable] into the [frmTest_CheckBoxItems] table with code like this: 在主窗体[frmTest]的Form Load事件中,您可以使用以下代码将[SourceTable]中的值加载到[frmTest_CheckBoxItems]表中:

Option Compare Database
Option Explicit

Private Sub Form_Load()
    ReloadCheckBoxItems
End Sub

Private Sub ReloadCheckBoxItems()
    Dim cdb As DAO.Database
    Set cdb = CurrentDb
    cdb.Execute _
            "DELETE FROM frmTest_CheckBoxItems", _
            dbFailOnError
    cdb.Execute _
            "INSERT INTO frmTest_CheckBoxItems (Seq, Description) " & _
            "SELECT ID, [Name] FROM SourceTable", _
            dbFailOnError
    Set cdb = Nothing
    Me.frmTest_CheckBoxItems_subform.Requery
End Sub

With a bit of tweaking to the format of the subform (hide the Record Selectors, Navigation Buttons, borders, alternating background colours, etc.) your layout ... 稍微调整一下子窗体的格式(隐藏记录选择器,导航按钮,边框,交替背景颜色等)你的布局......

DesignView.png

... combined with data in [SourceTable] ... ...结合[SourceTable]中的数据......

ID  Name
--  ----
 1  ABC
 2  DEF
 3  GHI
 4  JKL

would look like this: 看起来像这样:

FormView1.png

Add another row to [SourceTable] ... 在[SourceTable]中添加另一行...

ID  Name
--  ----
 1  ABC
 2  DEF
 3  GHI
 4  JKL
 5  AAA

... and the next time you open [frmTest] you'll get ......下次打开[frmTest]时,你会得到

FormView2.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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