How to add check boxes to a form based on columns in table in Access?
<p>I have a form in Access that allows a user to select a table from a combo box that lists all of the tables using VBA. Based on what table the user selects, I want a set of check boxes to be added to the form. Each check box would represent a column in the chosen table. I have a VBA function that will work with the columns that are chosen through the form after a button is pressed.</p>

<p>How would I make the check boxes appear after the user has chosen a table? I want this form to be applicable to tables that would be added in the future as well so I won't know the number of columns in advance. I can get check boxes to appear dynamically in a Userform but I've heard that they aren't commonly used in Access. I have also heard that there is a limit for the amount of controls that can be added to a form in its lifetime. Is there anyway of renaming controls so that I don't hit this limit?</p>

<p>This is the code that I use to populate the combo box:</p>

<pre><code>Private Sub Form_Load()

Dim tbl As DAO.TableDef
Dim db As Database

Set db = CurrentDb
With Me![TableName]
    For Each tbl In db.TableDefs
        If Not (tbl.name Like "MSys*" Or tbl.name Like "~*") Then
            .AddItem tbl.name
        End If
    Next
End With

End Sub
</code></pre>

<p>This is example code that can generate the check boxes in a Userform:</p>

<pre><code>Private Sub RetrieveColumns_Click()

Dim TableName As String
Dim MethodName As String
Dim tdf As DAO.TableDef
Dim rs As Recordset
Dim fld As Field
Dim chkBox As MSForms.CheckBox

TableName = Me![TableName]
Set tdf = dbs.CreateTableDef(TableName)

Set rs = dbs.OpenRecordset(TableName)

With rs
    For i = 0 To .Fields.Count - 1
        Set chkBox = Me.Controls("Forms.checkbox.1", "CheckBox_" &amp; i)
        chkBox.Caption = .Fields(i).name
        chkBox.Left = 5
        chkBox.Top = 5 + ((i - 1) * 20)
    Next i
End With

End Sub
</code></pre>