There are a couple of ways you could do this. One is in VBA and the other is in the query itself using multiple IIF statements.
In your query, make a column called SortBy. Assuming your form name is MyForm and the field to sort by is SortBy, the Field property of the query would be: SortBy: IIf(forms!MyForm!SortBy=”Name”,MyTable.Name,IIF(forms!MyForm!SortBy=”ZipCode”,MyTable.ZipCode,iif(forms!MyForm!SortBy=”State”,MyTable.State))) – I just did a sample of three columns; but you can add more. Make sure you indicate how you want it to sort.
If you have a print button on the form that runs the query, you can have the following code: (if you don’t have a button, you could put the code in the after_update event of the SortBy field.
Private Sub Print_Click()
Dim SQL As string
SQL = “Select * From MyTable Order By ” & Me!SortBy & “;” ‘Build the query with the selected SortBy
on error resume next ‘if the query doesn’t exist, don’t worry about it
DoCmd.DeleteObject acQuery, “MyQuery” ‘Deletes the query first
on error goto 0 ‘resume error checking
Currentdb().CreateQueryDef (“MyQuery”,SQL) ‘Build the query