Sorry John,
I don't quite understand what you mean... I'm a novice on codes...
In the form i had a button to search records which is shown as below:
--------------------------------code
starts-------------------------------------------
Private Sub Search_Click()
Dim strFilter As String
Dim bolAnd As Boolean
bolAnd = False
strFilter = ""
If Len(Me.txtOrigin.Value) > 0 Then
If bolAnd Then
strFilter = strFilter & " and "
End If
strFilter = strFilter & " Origin Like '*" & Me.txtOrigin.Value & "*'"
bolAnd = True
End If
.............................. (and other unbound textboxes)
With Me.[Daily_Report_subform].Form
If .Dirty Then
.Dirty = False
End If
If strFilter = "" Then
.FilterOn = False 'Nothing entered: show all records.
Else
.Filter = strFilter
.FilterOn = True
End If
End With
End Sub
-----------------------------------code
ends-------------------------------------------
and this is the code i use for the preview of report using the wizard:
----------------------------------------code
starts-------------------------------------
Private Sub Preview_Report_Click()
On Error GoTo Err_Preview_Report_Click
Dim stDocName As String
stDocName = "Find_Records"
DoCmd.OpenReport stDocName, acPreview
Exit_Preview_Report_Click:
Exit Sub
Err_Preview_Report_Click:
MsgBox Err.Description
Resume Exit_Preview_Report_Click
End Sub
------------------------------------code
ends-------------------------------------------
So where should i put this code: strSQL = strSQL & " AND [fieldname] = '" &
Me!controlname & "'" ?
As for the control name it is the textbox name right?
So do we still need the query for the report?
Sorry to trouble you...
Thanks
Green
John Vinson said:
Hi John,
Thanks for the quick reply!
I did try your way of creating a query for the form. However, i faced some
problems in it. As i have more than 20 unbound textboxes. It will based on
how many textboxes that the user filled in, to filter.
But in query, if i put , =[Forms]![RecordSearchform]![txtOrigin] and
[Forms]![RecordSearchform]![txtDestination] and ........... the user will
have to key in all to get the filtered records.
If i put, =[Forms]![RecordSearchform]![txtOrigin] or
[Forms]![RecordSearchform]![txtdestination] or ......... the user will only
have to key in 1 criteria to get the filtered records.
Is there any way to combine multiple criteria together?
Sorry for my poor explanation.
Hope is understandable.
Well, it's more complicated then... <g>
What you need to do is write VBA code in the button's Click event to
poll through the multiple controls on the form, building up a SQL
string which will be the report's recordsource. You'ld have an initial
SQL string such as:
Dim strSQL As String
strSQL = "SELECT this, that, theother FROM mytable WHERE TRUE"
which will (initially) return all records.
Your code would then step through the controls on the form; if the
control contains anything, you would add a criterion:
strSQL = strSQL & " AND [fieldname] = '" & Me!controlname & "'"
(using quotes for a text type field; use # for date fields and no
delimiter for numbers).
to the growing SQL string. If the control is empty, just leave strSQL
alone. Finally you'ld set the Report's Recordsource property to this
SQL string.
John W. Vinson[MVP]