The creation and customization of database applications using Microsoft Access
There is an example of reporting a crosstab with unknown headings at http://access.hookom.net/Samples.htm
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a table in MS Access with Medical Lab results. There are 25 fields of results. I have a dynamic query that allows changing the field to display in a query and it is working properly. I want to build a report that will allow the different fields to be displayed. Currently when I build a report based on the dynamic query the field name remains fixed while the dynamic query results changes the field name correctly. How can I do this?
The creation and customization of database applications using Microsoft Access
There is an example of reporting a crosstab with unknown headings at http://access.hookom.net/Samples.htm
AI answer
Use a fixed alias for the changing query column, bind the report text box to that alias, and update only the heading to show the selected laboratory field.
Modify the SQL generated by the existing dynamic query so that whichever laboratory field is selected is always returned as the same alias—for example, ResultValue.
SELECT
PatientID,
TestDate,
[Glucose] AS ResultValue
FROM LabResults;
When another result is selected, generate:
SELECT
PatientID,
TestDate,
[Cholesterol] AS ResultValue
FROM LabResults;
The source field changes, but the output column remains ResultValue. Access SQL supports assigning a different result-column name with AS.^1^
Configure the report as follows:
RecordSource can be a query name or SQL statement.^2^ResultValue. The text box will now remain bound even when the underlying laboratory field changes.^3^lblResultHeading.If the field selection is made on a form—for example, combo box cboResultField on frmLabSelector—set the report heading in its On Open event:
Private Sub Report_Open(Cancel As Integer)
Me.lblResultHeading.Caption = _
Forms!frmLabSelector!cboResultField.Column(1)
End Sub
Adjust the column number to the combo-box column containing the display name. The label’s Caption property controls its displayed text.^4^
Keep the selector form open while opening the report.
If changing the query to return a stable alias is not possible, change both the text box binding and its label when the report opens:
Private Sub Report_Open(Cancel As Integer)
Dim fieldName As String
fieldName = Forms!frmLabSelector!cboResultField.Value
Me.txtResult.ControlSource = "[" & fieldName & "]"
Me.lblResultHeading.Caption = fieldName
End Sub
This requires the selected field to exist in the report’s current RecordSource; otherwise Access reports that the field is not in the field list. Limit the combo box to the 25 known field names rather than accepting arbitrary user-entered names.
The alias approach is preferable because the report always sees the same structure: PatientID, TestDate, ResultValue, and any other fixed columns. Only the query decides which underlying result populates ResultValue.
References
The response from DGB resolved my problem
When you say "dynamic" query, does that mean you're using a crosstab query? To create a dynamic report, you may have to resort to using some VBA code. Are you okay with that?