No, you cannot control how the query looks.
Here is an example of dynamically assigning the control source and width of
controls on your report in its Open event. It assumes you have:
- the name of a query in the report's RecordSource.
- 20 unbound text boxes, side-by-side in the Detail section of the report,
with no labels attached, named txt00, txt01, txt02, ..., txt19.
- 20 labels, side-by-side in the Page Header section, named lbl00, lbl01,
...., lbl19.
Set the width of your report and the height of your sections as desired, but
the horizontal spacing and placement of the text boxes and labels does not
matter.
If you use fewer (or more) than 20 text boxes, set the value of
icMaxFieldsToHandle to the number you are providing.
Set the On Open property of the report to:
[Event Procedure]
Click the Build button (...) to open the code window.
Paste this in:
Private Sub Report_Open(Cancel As Integer)
'Purpose: Organize the text boxes on this report to match the fields
of the query.
'Assumes text boxes named txt00, txt01, ... and labels named lbl00,
lbl01, ...
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim iFieldCount As Integer
Dim iWidthEach As Integer
Dim i As Integer
Const icMaxFieldsToHandle = 5 'Max number of text boxes available.
'Get the querydef named in the report's RecordSource.
Set db = CurrentDb()
Set qdf = db.QueryDefs(Me.RecordSource)
'Get the number of fields in the source query.
iFieldCount = qdf.Fields.Count
If iFieldCount <= icMaxFieldsToHandle - 1 Then
'Hide boxes we do not need.
For i = iFieldCount To icMaxFieldsToHandle
Me.Controls("txt" & i).Visible = False
Me.Controls("lbl" & i).Visible = False
Next
ElseIf iFieldCount > icMaxFieldsToHandle Then
'Limit to available text boxes.
iFieldCount = icMaxFieldsToHandle
End If
'Calculate width for each text box.
iWidthEach = Int(Me.Width / iFieldCount)
'Assign the ControlSource and placement of each text box,
' and the Caption and placement of each label.
For i = 0 To iFieldCount - 1
With Me("txt" & i)
.ControlSource = qdf.Fields(i).Name
.Left = i * iWidthEach
.Width = iWidthEach
.Visible = True
End With
With Me("lbl" & i)
.Caption = qdf.Fields(i).Name
.Left = i * iWidthEach
.Width = iWidthEach
.Visible = True
End With
Next
'Clean up
Set qdf = Nothing
Set db = Nothing
End Sub