In my database, the user can select certain fields to search the records. Everything works fine, except the list is not sorted. I've tried placing the sort within the associated query, but this causes an error when I try to run it. How do I sort the results of the requery? I should note, that this is associated with a sub-form, just in case.
Here's the current code:
Private Sub SearchRecords()
Dim QueryDef As QueryDef, db As Database, strSql As String, strOriginalSQL As String
Dim iSplit As Integer
' Modify the Query.
Set db = CurrentDb
Set QueryDef = db.QueryDefs("qrysfrmDocumentSearch")
strOriginalSQL = QueryDef.SQL
QueryDef.Close
iSplit = InStr(strOriginalSQL, ";")
strSql = Left(strOriginalSQL, iSplit - 1)
strSql = strSql & " Where SubmissionDate Between " & DateTimeSQL(txtlnkDateFrom) & " and " & DateTimeSQL(txtlnkDateTo) & " "
If txtDocumentID <> "" Then
strSql = strSql & " And tqryDocuments.Documentid = " & intdocid
End If
If Len(strDocType) > 0 Then
strSql = strSql & " And DocumentType in (" & strDocType & ") "
End If
If Len(strClient) > 0 Then
strSql = strSql & " And Client in (" & strClient & ") "
End If
If intAuthor > 0 Then
strSql = strSql & " And Author = " & intAuthor
End If
If Len(strContractType) > 0 Then
strSql = strSql & " And ContractType = """ & strContractType & """ "
End If
If Len(strDocTitle) > 0 Then
strSql = strSql & " And DocumentTitle Like """ & strDocTitle & """ "
End If
If Len(strDocDesc) > 0 Then
strSql = strSql & " And DocumentDescription Like """ & strDocDesc & """ "
End If
If Len(strAudience) > 0 Then
strSql = strSql & " And Audience in (" & strAudience & ") "
End If
If Len(strLang) > 0 Then
strSql = strSql & " And Language = """ & strLang & """ "
End If
strSql = strSql & ";"
Me.sfrmDocumentSearch.Form.RecordSource = strSql
sfrmDocumentSearch.Requery
QueryDef.Close
End Sub
Here's the current code:
Private Sub SearchRecords()
Dim QueryDef As QueryDef, db As Database, strSql As String, strOriginalSQL As String
Dim iSplit As Integer
' Modify the Query.
Set db = CurrentDb
Set QueryDef = db.QueryDefs("qrysfrmDocumentSearch")
strOriginalSQL = QueryDef.SQL
QueryDef.Close
iSplit = InStr(strOriginalSQL, ";")
strSql = Left(strOriginalSQL, iSplit - 1)
strSql = strSql & " Where SubmissionDate Between " & DateTimeSQL(txtlnkDateFrom) & " and " & DateTimeSQL(txtlnkDateTo) & " "
If txtDocumentID <> "" Then
strSql = strSql & " And tqryDocuments.Documentid = " & intdocid
End If
If Len(strDocType) > 0 Then
strSql = strSql & " And DocumentType in (" & strDocType & ") "
End If
If Len(strClient) > 0 Then
strSql = strSql & " And Client in (" & strClient & ") "
End If
If intAuthor > 0 Then
strSql = strSql & " And Author = " & intAuthor
End If
If Len(strContractType) > 0 Then
strSql = strSql & " And ContractType = """ & strContractType & """ "
End If
If Len(strDocTitle) > 0 Then
strSql = strSql & " And DocumentTitle Like """ & strDocTitle & """ "
End If
If Len(strDocDesc) > 0 Then
strSql = strSql & " And DocumentDescription Like """ & strDocDesc & """ "
End If
If Len(strAudience) > 0 Then
strSql = strSql & " And Audience in (" & strAudience & ") "
End If
If Len(strLang) > 0 Then
strSql = strSql & " And Language = """ & strLang & """ "
End If
strSql = strSql & ";"
Me.sfrmDocumentSearch.Form.RecordSource = strSql
sfrmDocumentSearch.Requery
QueryDef.Close
End Sub