Search code examples
vbams-access

populate text boxes on Access report from text boxes on form


I'm trying to populate all the text boxes on the report with the data from the text boxes from a form. The names of the text boxes are the same on the form and on the report. I tried this, but I'm stuck:

Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.ControlType = acTextBox Then
        ctl.ControlSource = "[Forms]![frmlisteannuelle]![ctl.name]"
    End If
Next

Solution

  • Your code was almost there! It was just missing an equal sign at the start and a concatenation between the expression and the clt.Name value. Take a look:

    Private Sub Report_Open(Cancel As Integer)
        Dim ctl As Control
        For Each ctl In Me.Controls
            If ctl.ControlType = acTextBox Then
                ctl.ControlSource = "=Forms![frmlisteannuelle]![" & ctl.Name & "]"
            End If
        Next
    End Sub