Hi
I'm using an adapted version of this search form :
http://www.access-programmers.co.uk/forums/showthread.php?t=99353
It works how I expect, except that when trying to run a report based on the search results I get the error
"Run-time error 3075 - syntax error (missing operator) in query expression"
I'm not hugely familiar with this function and hope that someone can help.
Here is my code to run the function based on a Search or Report button:
the debug shows a result of :
( [StatusType] = "Outstanding" ) ORDER BY [ScheduledDate] ASC;
This is the Buildfilter function :
When the Search button is clicked the subform displays the information exactly as I would expect. However when trying to run the report on the same filter I get the above error.
Can anyone help me with a solution ?
I'm using an adapted version of this search form :
http://www.access-programmers.co.uk/forums/showthread.php?t=99353
It works how I expect, except that when trying to run a report based on the search results I get the error
"Run-time error 3075 - syntax error (missing operator) in query expression"
I'm not hugely familiar with this function and hope that someone can help.
Here is my code to run the function based on a Search or Report button:
Code:
Private Sub btn_Search_Click()
' Update the record source
On Error Resume Next
Me.frm_ClientSearchSub.Form.RecordSource = "SELECT * FROM Qry_ClientSearch " & BuildFilter
Me.frm_ClientSearchSub.Form.RecordSource = "SELECT * FROM Qry_ClientSearch WHERE " & BuildFilter
On Error GoTo 0
' Requery the subform
Me.frm_ClientSearchSub.Requery
End Sub
Private Sub btn_Report_Click()
Dim stDocName As String
On Error Resume Next
Me.frm_ClientSearchSub.Form.RecordSource = "SELECT * FROM Qry_ClientSearch " & BuildFilter
Me.frm_ClientSearchSub.Form.RecordSource = "SELECT * FROM Qry_ClientSearch WHERE " & BuildFilter
On Error GoTo 0
Debug.Print BuildFilter
stDocName = "Rpt_ClientSearch"
DoCmd.OpenReport stDocName, acNormal, WhereCondition:=BuildFilter
End Sub
the debug shows a result of :
( [StatusType] = "Outstanding" ) ORDER BY [ScheduledDate] ASC;
This is the Buildfilter function :
Code:
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim varStatus As Variant
Dim varItem As Variant
Dim varOrder As Variant
Dim varDirection As Variant
Dim intIndex As Integer
varWhere = Null ' Main filter
varStatus = Null ' Subfilter used for Status'
varOrder = Null ' Sort by
varDirection = Null ' Sort Direction
' Check for LIKE Project code
If Me.txtProjectCode > "" Then
varWhere = varWhere & "[ProjectCode] LIKE """ & Me.txtProjectCode & "*"" AND "
End If
' Check for LIKE InvoiceID
If Me.txtInvoiceID > "" Then
varWhere = varWhere & "[InvoiceID] LIKE """ & Me.txtInvoiceID & "*"" AND "
End If
' Check for LIKE RowID
If Me.txtRowID > "" Then
varWhere = varWhere & "[RowID] LIKE """ & Me.txtRowID & "*"" AND "
End If
' Check for LIKE RecordID
If Me.txtRecordID > "" Then
varWhere = varWhere & "[RecordID] LIKE """ & Me.txtRecordID & "*"" AND "
End If
' Check Dates Greater Than
If Me.txtGreaterThan > "" Then
varWhere = varWhere & "[ScheduledDate] >= #" & Format(Me.txtGreaterThan, "mm/dd/yyyy") & "# AND "
End If
' Check Dates Less Than
If Me.txtLessThan > "" Then
varWhere = varWhere & "[ScheduledDate] <= #" & Format(Me.txtLessThan, "mm/dd/yyyy") & "# AND "
End If
' Check for Third Paty
If Me.cmbThirdParty <> "<ALL>" Then
varWhere = varWhere & "[ThirdParty] = """ & Me.cmbThirdParty & """ AND "
End If
' Check for Account
If Me.cmbAccount <> "<ALL>" Then
varWhere = varWhere & "[AccountName] = """ & Me.cmbAccount & """ AND "
End If
' Check for Status in multiselect list
For Each varItem In Me.lstStatus.ItemsSelected
varStatus = varStatus & "[Status] = """ & Me.lstStatus.ItemData(varItem) & """ OR "
Next
' Check for Transaction Type in multiselect list
For Each varItem In Me.lstType.ItemsSelected
varStatus = varStatus & "[StatusType] = """ & Me.lstType.ItemData(varItem) & """ OR "
Next
' Check for Account
If Me.cmbTransactionType <> "<ALL>" Then
varWhere = varWhere & "[TransactionType] = """ & Me.cmbTransactionType & """ AND "
End If
'BUILD THE VARIABLE, ADJUSTING ANY FILTERS AS NEEDED
' Test to see if we have subfilter for Status...
If IsNull(varStatus) Then
' do nothing
Else
' strip off last "OR" in the filter
If Right(varStatus, 4) = " OR " Then
varStatus = Left(varStatus, Len(varStatus) - 4)
End If
' Add some parentheses around the subfilter
varWhere = varWhere & "( " & varStatus & " )"
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
Else
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
'Build the Sort Order
If Me.cmbSortOrder > "" Then
Select Case Me.cmbSortOrder
Case "ACCOUNT NAME"
varOrder = "[AccountName]"
Case "INVOICE NUMBER"
varOrder = "[InvoiceID]"
Case "PAID DATE"
varOrder = "[PaidDate]"
Case "PAYMENT STATUS"
varOrder = "[PaymentStatus]"
Case "PAYMENT TYPE"
varOrder = "[TransactionType]"
Case "PROJECT CODE"
varOrder = "[ProjectCode]"
Case "RECORD ID"
varOrder = "[RecordID]"
Case "ROW ID"
varOrder = "[RowID]"
Case "SCHEDULED DATE"
varOrder = "[ScheduledDate]"
Case "THIRD PARTY"
varOrder = "[ThirdParty]"
Case "VALUE"
varOrder = "[InvoiceValue]"
End Select
End If
'Build the Sort Direction
If Me.cmbSortDirection = "" Then
varDirection = "DESC"
Else
If Me.cmbSortDirection = "ASCENDING" Then varDirection = "ASC" Else If Me.cmbSortDirection = "DESCENDING" Then varDirection = "DESC"
End If
'Passes the SQL Select Statement back to the BuildFilter variable
If Me.cmbSortOrder > "" And Me.cmbSortDirection > "" Then
BuildFilter = varWhere & " ORDER BY " & varOrder & " " & varDirection & ";"
Else
BuildFilter = varWhere & " ORDER BY [ScheduledDate] ASC;"
End If
End Function
When the Search button is clicked the subform displays the information exactly as I would expect. However when trying to run the report on the same filter I get the above error.
Can anyone help me with a solution ?