populate text boxes on Access report from text boxes on form

Viewed 45

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
2 Answers

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

If you want to set ControlSource property with reference to form control name by using report control name, concatenate the variable and include = sign. Consider:

ctl.ControlSource = "=[Forms]![frmlisteannuelle]![" & ctl.name & "]"

Related