user3248712
user3248712

Reputation: 1

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.

How to achive this?

Upvotes: 0

Views: 2846

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123549

One way to accomplish this would be to use a subform. Say the main form is named [frmTest]. Create a table named [frmTest_CheckBoxItems] with the following fields:

[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.

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:

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] ...

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

would look like this:

FormView1.png

Add another row to [SourceTable] ...

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

... and the next time you open [frmTest] you'll get

FormView2.png

Upvotes: 2

Related Questions