Hi Melissa,
As much explanation as to
how to modify this code to work is appreciated.
If Not IsNull(Me.TxtFilterShipTo) Then
strWhere = strWhere & "([ShipTo] = """ & Me.TxtFilterShipTo & """) AND "
End If
this:
If Not IsNull(Me.TxtFilterShipTo)
checks to see if you have filled out the TxtFilterShipTo control.
this:
strWhere = strWhere & "([ShipTo] = """ & Me.TxtFilterShipTo & """) AND "
adds the ShipTo file to the filter string. If you have "Mary Smith" in
the TxtFilterShipTo control, this is the result of what is built
strWhere = strWhere & "([ShipTo] = ""Mary Smith"") AND "
if you want a double quote in the middle of a string that is delimited
with double quotes, you need to use 2 of them
personally, I do not like automatically concatenating the 'AND' onto the
end -- because then, your code has to remove it when you are done. It
is better to take advantage of using a variant for the filter and using
+ instead of & to concatenate the AND
read about the Difference between & and + here:
Access Basics by Crystal
http://www.accessmvp.com/strive4peace
8-part free tutorial that covers essentials in Access
If you want to be able to choose more than one control to filter the
ShipTo field, the easiest way is to add another textbox to your form header
if you have 2 textboxes to choose for the ShipTo filed and they are named
TxtFilterShipTo1
TxtFilterShipTo2
then, you would want to use an OR condition ... but the second one might
not be filled out, so you would do this:
'~~~~~~~~~~~~~~~~~~~
dim varFilter as variant
varFilter = null
'if the first filter control for ShipTo is filled out
If not IsNull(me.TxtFilterShipTo1 ) Then
'if the second filter control for ShipTo is also filled out
If not IsNull(me.TxtFilterShipTo2 ) Then
varFilter = (varFilter + " AND " ) _
& "([ShipTo]= '" & me.TxtFilterShipTo1 & "'" _
& " OR [ShipTo]= '" & me.TxtFilterShipTo1 & "')" _
Else
'only the first ShipTo filter is filled out
varFilter = (varFilter + " AND " ) _
& "[ShipTo]= '" & me.TxtFilterShipTo1 & "'"
End If
end if
'~~~~~~~~~~~~~~~~~~~
space underscore at the end of a line means that the statement is
continued on the next line
here is some generic information on building filter strings:
Set Form or Subform Filter
---
Put comboboxes and textboxes on the form (I put then in the header and
give them a unique background color so they are not confused with data
controls). Assign this to the AfterUpdate event of each one...
=SetFormFilter()
then put this code behind the form
'~~~~~~~~~~~~~~~
Private Function SetFormFilter()
dim varFilter as variant
varFilter = null
If not IsNull(me.text_controlname ) Then
varFilter = (varFilter + " AND " ) _
& "[TextFieldname]= '" & me.text_controlname & "'"
end if
If not IsNull(me.date_controlname ) Then
varFilter = (varFilter + " AND " ) _
& "[DateFieldname]= #" & me.date_controlname & "#"
end if
If not IsNull(me.numeric_controlname ) Then
varFilter = (varFilter + " AND " ) _
& me.numeric_controlname
end if
'**************************************************
' Choose on of following code blocks
'**************************************************
'--------------- Filter form you are behind
if not IsNull(varFilter) then
me.filter = varFilter
me.FilterOn = true
else
me.FilterOn = false
end if
me.requery
'OR
'--------------- Filter subform
' if not IsNull(varFilter) then
' me.subformcontrolname.form.filter = varFilter
' me.subformcontrolname.form.FilterOn = true
' else
' me.subformcontrolname.form.FilterOn = false
' end if
' me.subformcontrolname.form.requery
End Function
'~~~~~~~~~~~~~~~
me.numeric_controlname refers to the NAME property of a control on the
form you are behind
(Me. represents the form -- kinda like "me" for me is not "me" for you
)
delimiters are used for data that is not a number
quote marks ' or " for text
number signs # for dates
varFilter is a variant that will hold the string you are building for
each condition
-- but if nothing is specified in the filter control (IsNull),
then that addition to the filter string is skipped.
finally, when the filter string is done, it is applied to your form.
That means that as you flip through records, ONLY records matching that
filter will show
Then, put another command button on the form
Name --> btnShowAll
OnClick --> [Event Procedure]
'~~~~~~~~~~~~~~~
me.filteron = false
me.requery
'~~~~~~~~~~~~~~~
Warm Regards,
Crystal
remote programming and training
Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace
*
have an awesome day
*
I have a search form with the following fields.
Text - Region
Text - Ship To
Text - State
Yes/No - Communication
Yes/No - Performance
Yes/No - Fuel
I would like to be able to enter multiple values in the fields. For example,
find results where the Ship To is 123456 or 111111. Is this possible?
Here is the code I am using. And again, let me preface this question with
the fact that I am not very familiar with VBA code. As much explanation as to
how to modify this code to work is appreciated.
If Not IsNull(Me.TxtFilterShipTo) Then
strWhere = strWhere & "([ShipTo] = """ & Me.TxtFilterShipTo & """)
AND "
End If
Thanks,
Melissa