Trying to pass a parameter into a query to sort a recordset. I was keeping a recordset in memory to sort it but the rowsource will not take a recordset directly. So I figured I could use a parameter to pass into the query and sort it that way. The code works if I hard code the sort criteria but if I just get a bunch of records in no particular order if I pass a paremter in. Is it even possible to do this in Access? Here is the code.
Code:
'************** Calling Sub ************************
Private Sub GetMaintLogBySort()
'Init sort criteria
sortStates = Array("ProjectName ASC", "ProjectName DESC", "Location ASC, ProjectName ASC", _
"Location DESC, ProjectName DESC", "StartDate ASC", "StartDate DESC")
sortState = sortStates(SortStateIndex.Project_Asc)
Set rs = IData.GetMaintLogBySort(sortState)
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF
Debug.Print rs!projectName & ", " & rs!location & ", " & rs!startDate
rs.MoveNext
Loop
Else
Debug.Print "No records in recordset..."
End If
End Sub
'************** GetMaintLogBySort() ************************
Public Function GetMaintLogBySort(ByVal sortOrder As String) As Recordset
Dim qDef As QueryDef
'Don't flag error
On Error Resume Next
'Init function
'Set GetMaintLog = New clsMaintLog
'Set database object
Set db = CurrentDb
'Set query to execute
Set qDef = db.QueryDefs("GetMaintLogBySort")
qDef.Parameters("@SortOrder") = sortOrder
'Execute
Set rs = qDef.OpenRecordset()
Debug.Print "Record Count: " & rs.RecordCount
Set GetMaintLogBySort = rs
End Function
'************** Query being called ************************
SELECT ProjectName, Location, StartDate
FROM MaintLog
ORDER BY SortOrder;