I think you have some of the names mixed up. At least you
still have some of the example names that I used. You did
change one field name to [Date], but you did it in two
places. There is clearly a miscommunication somewhere in
here, because using a date field in my example of how to
write the where criteria for a numeric type field will
definitely not work.
Note that you should NOT use reserved names for anything you
create, i.e. using Date as the name of a field creates all
kinds of confusion. Change that field in the table to a
more descriptive name like InvoiceDate or whatever relates
to the data in your table.
The code I posted had three example If - End If blocks that
are templates of how to deal with three common types of
fields in a table. You do not have to have all three or you
might need multiple blocks of the same type. You do have to
have a separate block for each unbound control that you have
provided for users to enter search criteria in the form
header. It's up to you to make those design decisions and
copy and modify the appropriate template block needed for
each unbound control (and change the template's example
control name (txt...) that I used to the unbound control
name that you are using).
As for the error, without knowing what error message you
got, I can't tell what it's complaining about. Actually I
don't see how that line can have an error unless the
strWhere string is an illegal filter, which wouldn't
surprise me at this point. Did you hover the mouse over the
strWhere variable when the line was highlighted so you could
see the string and determine what the code actually did? If
so what did it look like? If not, try it, you might get a
clue as to what is wrong.
I added the unbound control box and the commmand button with this code:
Private Sub Command74_Click()
Dim strWhere As String
If Not IsNull(txtDate) Then
strWhere = strWhere & " AND [Date]=" _
& Format(txtDate, "\#m\/d\/yyyy\#")
End If
If Not IsNull(txtText) Then
strWhere = strWhere & " AND [textfield]=" _
& """" & txtText & """"
End If
If Not IsNull(txtNumber) Then
strWhere = strWhere & " AND [Date]=" & txtNumber
End If
Me.Filter = Mid(strWhere, 6)
Me.FilterOn = True
End Sub
but when I click on the command button it errors and goes to the following
line for debugging:
Me.Filter = Mid(strWhere, 6)
:
You know, that's a good question. I guess I don't, at least
I can't think of one at the moment. When all else fails,
see if you can find some thing in the Northwind sample
database that comes with Access.
Actually, what I posted is pretty much what I would expect
in a sample,but without the actual form. And, that should
be fairly easy to create. Just add the text boxes to a new
form, name them appropriately and change the names in my
posted code (my names use "txt" as a prefix for the unbound
text boxes and the names that have "field" in them are
columns in the form's record source table.
If you hit a specific roadblock, come on back with the
details and we'll go from there.
Brook wrote:
Do you know of any samples that I could look at the help me with this? I'm
going to get started with what you posted.
Brook wrote:
I have frmcheckingacct that I would like to add a custom search feature so
that for example if I wanted to search by date, it would in a new pop up box
and or unbound text box would show all the records for that date,
or if I wanted to search by payee, it would pull all the records for that
payee?
:
Not only possible, but common to many applications ;-)
The usual arrangement is to make a Continuous form bound to
your table with the important data fields bound to controls
in the forms detail section.
To provide the "search" facility, plaxe an unbound text box
for each of the seach items (along with a button to run the
search) into the form's header section.
The code in the button's Click event procedure would be
along the lines of this air code:
Dim strWhere As String
If Not IsNull(txtDate) Then
strWhere = strWhere & " AND [datefield]=" _
& Format(txtDate,"\#m\/d\/yyyy\#")
End If
If Not IsNull(txtText) Then
strWhere = strWhere & " AND [textfield]=" _
& """" & txtText & """"
End If
If Not IsNull(txtNumber) Then
strWhere = strWhere & " AND [datefield]=" & txtNumber
End If
Me.Filter = Mid(strWhere, 6)
Me.FilterOn = True