Re: Filtering a query for a form based on dates
- From: "Graham Mandeno" <Graham.Mandeno@xxxxxxxxxxxxx>
- Date: Tue, 26 Aug 2008 10:14:48 +1200
Hi Dave
You haven't really given enough information about what you have tried for me
to hazard a guess at what might be causing the problem.
However, I can give you a couple of tips that might help.
SQL (the language used for Access queries and filters) is quite fussy about
date formats. The best format to use is either the North American format
(mm/dd/yyyy) or the international format (yyyy-mm-dd). I prefer the latter
because it's less ambiguous. Also, dates and times in SQL statements should
always be enclosed in hash/pound signs (#).
I find the following function useful for formatting dates for SQL:
Public Function SQLDate(ByVal d As Variant) As String
Dim sFormat As String
On Error Resume Next
d = CDate(d)
If Err = 0 Then
If TimeValue(d) = 0 Then
sFormat = "\#yyyy\-mm\-dd\#"
Else
sFormat = "\#yyyy\-mm\-dd hh\:nn\:ss\#"
End If
SQLDate = Format(d, sFormat)
Else
Err.Clear
End If
End Function
Now, when you wish to apply a filter to your form, I recommend that you do
it via the form's Filter property. As I understand it, you currently have
your recordsource query referring to textboxes on the form and you requery
the whole form when the textboxes are updated. This is unnecessarily
complicated.
Let's say you have two unbound textboxes, txtStartDate and txtEndDate.
Write a function in your form module like this:
Private Function SetFilter()
Const cAND as String = " AND "
Dim fltr as string
If Not IsNull(txtStartDate) then
sfltr = "([DateField]>=" & SQLDate(txtStartDate) & ")" & cAND
End If
If Not IsNull(txtEndDate) then
sfltr = "([DateField]<=" & SQLDate(txtEndDate) & ")" & cAND
End If
' add in as many other filter fragments as you require
If Len(fltr) = 0 then
Me.FilterOn = False
Else
' remove the last AND
Me.Filter = Left(fltr, Len(fltr) - Len(cAND))
Me.FilterOn = True
End If
End Function
Then, for txtStartDate and txtEndDate (and any other filtering controls) set
the AfterUpdate property to:
=SetFilter()
--
Good Luck :-)
Graham Mandeno [Access MVP]
Auckland, New Zealand
"xsdaver via AccessMonster.com" <u45689@uwe> wrote in message
news:8937347271612@xxxxxx
I have a query that is used to provide data for a form. The query has a
field that identifies the date an item was sold. That field is specified
as
a date field. In the form, I put a value into a text box and requery.
The
only thing that will work if there are dates in the text box. If I put in
anything that you might otherwise put into the criteria filed of a query
it
does not work. Things like Like "*", or >#1/1/2008# return an error
message
that states the formula is too complex to use. I've tried with and
without
quotes and the # symbol but nothing seems to work. Can what I want to do
be
done?
Thanks,
Dave
--
Message posted via http://www.accessmonster.com
.
- Follow-Ups:
- Re: Filtering a query for a form based on dates
- From: xsdaver via AccessMonster.com
- Re: Filtering a query for a form based on dates
- References:
- Filtering a query for a form based on dates
- From: xsdaver via AccessMonster.com
- Filtering a query for a form based on dates
- Prev by Date: Re: Secondary Tag
- Next by Date: RE: Cancel a close command
- Previous by thread: Re: Filtering a query for a form based on dates
- Next by thread: Re: Filtering a query for a form based on dates
- Index(es):
Relevant Pages
|