How do I get a query to show all values when a combo control is em

G

Guest

Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to limit the
lines returned based on what is selected in several unbound combo boxes in a
form I created.

I can make this work if the combo box on the form has a chosen value - but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to return
all records,

The reason for this is I want a more flexible report which can be limited at
many levels,

Thanks for anyones advice
 
A

Allen Browne

Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its Click
event, build up the WhereCondition string based on the combos that are not
blank.

This example shows how to build up the string easily, by adding the AND to
the end of each one, and then chopping off the trailing AND at the end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND "
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html
 
G

Guest

Make criteria like --
Like [YourComboBox] &"*"

It will return all if there is nothing selected.
 
G

Guest

Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not sure if
I've done everything that is required in my report - do I need to add
dependency to the controls on the report?? -please find the code below does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


Allen Browne said:
Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its Click
event, build up the WhereCondition string based on the combos that are not
blank.

This example shows how to build up the string easily, by adding the AND to
the end of each one, and then chopping off the trailing AND at the end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND "
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html
 
A

Allen Browne

First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not sure if
I've done everything that is required in my report - do I need to add
dependency to the controls on the report?? -please find the code below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


Allen Browne said:
Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its
Click
event, build up the WhereCondition string based on the combos that are
not
blank.

This example shows how to build up the string easily, by adding the AND
to
the end of each one, and then chopping off the trailing AND at the end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND "
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


huzzlepuzzle said:
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to limit
the
lines returned based on what is selected in several unbound combo boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
G

Guest

Hi Allen,
OK - I got every combo to work now except for limiting between the 2 dates
would you possibly be able to advise?
I'm attempting the short date format first "\#dd\/mm\/yyyy\#" but I get a
syntax error '(DateofEntry>#29/06/)'

The second date format that I want to use is just "\#MM\/YY\#" - I took the
syntax from the website but I don't really understand it - can you advise? -
I put the code I used for the short date below:
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If


--
Kind Regards

Hazel


Allen Browne said:
First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not sure if
I've done everything that is required in my report - do I need to add
dependency to the controls on the report?? -please find the code below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


Allen Browne said:
Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its
Click
event, build up the WhereCondition string based on the combos that are
not
blank.

This example shows how to build up the string easily, by adding the AND
to
the end of each one, and then chopping off the trailing AND at the end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND "
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to limit
the
lines returned based on what is selected in several unbound combo boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
G

Guest

Thanks Carl - I tried this and found it useful, however as I need to also
limit by the date criteria I decided to try with Allens solution as it's more
flexible for this.

Your answer cleared up my question regarding the "*" syntax and I know I
will use this in the future.
--
Kind Regards

Hazel


KARL DEWEY said:
Make criteria like --
Like [YourComboBox] &"*"

It will return all if there is nothing selected.

huzzlepuzzle said:
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to limit the
lines returned based on what is selected in several unbound combo boxes in a
form I created.

I can make this work if the combo box on the form has a chosen value - but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to return
all records,

The reason for this is I want a more flexible report which can be limited at
many levels,

Thanks for anyones advice
 
A

Allen Browne

Great. Good progress.

In the context of a WHERE clause, SQL statement, or literal VBA value, you
must use the mm/dd/yyyy format, regardless of your regional settings. See:
International Date Formats in Access
at:
http://allenbrowne.com/ser-36.html

For an example of how to format the dates into the WHERE clause, see example
2 in this article:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

To explain the cryptic date format string inside the Format() function:
- Backslash indicates the followgin character is a literal, so \# is
interpreted by Format as #.

- The backslash before the forward slash again specifies a literal forward
slash. If you don't do that, the Format() function interprets the forward
slash according to the date separator specified in your regional settings,
which can give unreliable results.

If you format a date as "\#MM\/YY\#", you get: hash, month, slash, year,
hash. That is NOT a valid date in Access, so is useless in a WHERE string.
You can demonstrate that in the Immediate Window (Ctrl+G) with:
? IsDate(Format(date, "\#MM\/YY\#"))
which returns False.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,
OK - I got every combo to work now except for limiting between the 2 dates
would you possibly be able to advise?
I'm attempting the short date format first "\#dd\/mm\/yyyy\#" but I get a
syntax error '(DateofEntry>#29/06/)'

The second date format that I want to use is just "\#MM\/YY\#" - I took
the
syntax from the website but I don't really understand it - can you
advise? -
I put the code I used for the short date below:
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If


--
Kind Regards

Hazel


Allen Browne said:
First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not sure
if
I've done everything that is required in my report - do I need to add
dependency to the controls on the report?? -please find the code below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ &
Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo &
""")
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo
&
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ &
Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


:

Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its
Click
event, build up the WhereCondition string based on the combos that are
not
blank.

This example shows how to build up the string easily, by adding the
AND
to
the end of each one, and then chopping off the trailing AND at the
end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND
"
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type
field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


message
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to
limit
the
lines returned based on what is selected in several unbound combo
boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen
value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
G

Guest

Hi allen,

Sorry - I'm still struggling a bit with this. I changed to US format - ie.
Const conDateFormat = "\#mm\/dd\/yyyy\#", and my text boxes on the form are
set to short date format but I still got a syntax error, so I decided to take
the advice from the first link about creating the wrapper function SQLDate,

Being a bit of a novice I'm not sure how to call this as a const in my sub
function - could you advise?

Also - MissDate field with date MM/YY - I think I need to treat as a
number or text string rather than a date, can I still state to search between
the 2 values MissDateFirst and MissDateLast if this is so??

Also - how would input masks affect the #\/ SQL??

Thanks for your advice - this is all a bit complex

Hazel

--
Kind Regards

Hazel


Allen Browne said:
Great. Good progress.

In the context of a WHERE clause, SQL statement, or literal VBA value, you
must use the mm/dd/yyyy format, regardless of your regional settings. See:
International Date Formats in Access
at:
http://allenbrowne.com/ser-36.html

For an example of how to format the dates into the WHERE clause, see example
2 in this article:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

To explain the cryptic date format string inside the Format() function:
- Backslash indicates the followgin character is a literal, so \# is
interpreted by Format as #.

- The backslash before the forward slash again specifies a literal forward
slash. If you don't do that, the Format() function interprets the forward
slash according to the date separator specified in your regional settings,
which can give unreliable results.

If you format a date as "\#MM\/YY\#", you get: hash, month, slash, year,
hash. That is NOT a valid date in Access, so is useless in a WHERE string.
You can demonstrate that in the Immediate Window (Ctrl+G) with:
? IsDate(Format(date, "\#MM\/YY\#"))
which returns False.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,
OK - I got every combo to work now except for limiting between the 2 dates
would you possibly be able to advise?
I'm attempting the short date format first "\#dd\/mm\/yyyy\#" but I get a
syntax error '(DateofEntry>#29/06/)'

The second date format that I want to use is just "\#MM\/YY\#" - I took
the
syntax from the website but I don't really understand it - can you
advise? -
I put the code I used for the short date below:
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If


--
Kind Regards

Hazel


Allen Browne said:
First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not sure
if
I've done everything that is required in my report - do I need to add
dependency to the controls on the report?? -please find the code below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ &
Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo &
""")
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo
&
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ &
Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


:

Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In its
Click
event, build up the WhereCondition string based on the combos that are
not
blank.

This example shows how to build up the string easily, by adding the
AND
to
the end of each one, and then chopping off the trailing AND at the
end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """) AND
"
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type
field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


message
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to
limit
the
lines returned based on what is selected in several unbound combo
boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen
value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
A

Allen Browne

Interesting. I've actually been using a SQLDate() function since Access 2
(i.e. more than 10 years), and it looks like this:

Function SQLDate(vDate As Variant) As String
If IsDate(vDate) Then
SQLDate = "#" & Format$(vDate, "mm\/dd\/yyyy") & "#"
End If
End Function
Function SQLDateTime(vDate As Variant) As String
If IsDate(vDate) Then
SQLDateTime = "#" & Format$(vDate, "mm\/dd\/yyyy hh:nn:ss") & "#"
End If
End Function

It would be possible (though inefficient) to do the string conparision based
on month and year. Because it's just a string, you can use any format you
wish, e.g. yyyymm (which would sort correctly.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi allen,

Sorry - I'm still struggling a bit with this. I changed to US format -
ie.
Const conDateFormat = "\#mm\/dd\/yyyy\#", and my text boxes on the form
are
set to short date format but I still got a syntax error, so I decided to
take
the advice from the first link about creating the wrapper function
SQLDate,

Being a bit of a novice I'm not sure how to call this as a const in my sub
function - could you advise?

Also - MissDate field with date MM/YY - I think I need to treat as a
number or text string rather than a date, can I still state to search
between
the 2 values MissDateFirst and MissDateLast if this is so??

Also - how would input masks affect the #\/ SQL??

Thanks for your advice - this is all a bit complex

Hazel

--
Kind Regards

Hazel


Allen Browne said:
Great. Good progress.

In the context of a WHERE clause, SQL statement, or literal VBA value,
you
must use the mm/dd/yyyy format, regardless of your regional settings.
See:
International Date Formats in Access
at:
http://allenbrowne.com/ser-36.html

For an example of how to format the dates into the WHERE clause, see
example
2 in this article:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

To explain the cryptic date format string inside the Format() function:
- Backslash indicates the followgin character is a literal, so \# is
interpreted by Format as #.

- The backslash before the forward slash again specifies a literal
forward
slash. If you don't do that, the Format() function interprets the forward
slash according to the date separator specified in your regional
settings,
which can give unreliable results.

If you format a date as "\#MM\/YY\#", you get: hash, month, slash, year,
hash. That is NOT a valid date in Access, so is useless in a WHERE
string.
You can demonstrate that in the Immediate Window (Ctrl+G) with:
? IsDate(Format(date, "\#MM\/YY\#"))
which returns False.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,
OK - I got every combo to work now except for limiting between the 2
dates
would you possibly be able to advise?
I'm attempting the short date format first "\#dd\/mm\/yyyy\#" but I
get a
syntax error '(DateofEntry>#29/06/)'

The second date format that I want to use is just "\#MM\/YY\#" - I
took
the
syntax from the website but I don't really understand it - can you
advise? -
I put the code I used for the short date below:
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If


--
Kind Regards

Hazel


:

First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as
the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the
filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

message
Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not
sure
if
I've done everything that is required in my report - do I need to
add
dependency to the controls on the report?? -please find the code
below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ &
Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ")
AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo &
""")
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ &
Me.OrgAccountCombo
&
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ &
Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no
start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no
End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end
dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no
start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no
End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end
dates.
strWhere = strField & " Between " &
Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


:

Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In
its
Click
event, build up the WhereCondition string based on the combos that
are
not
blank.

This example shows how to build up the string easily, by adding the
AND
to
the end of each one, and then chopping off the trailing AND at the
end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """)
AND
"
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type
field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


message
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to
limit
the
lines returned based on what is selected in several unbound combo
boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen
value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query
to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
N

Noozer

Isn't the "native" date format in a database "YYYY/MM/DD" ?

Allen Browne said:
Interesting. I've actually been using a SQLDate() function since Access 2
(i.e. more than 10 years), and it looks like this:

Function SQLDate(vDate As Variant) As String
If IsDate(vDate) Then
SQLDate = "#" & Format$(vDate, "mm\/dd\/yyyy") & "#"
End If
End Function
Function SQLDateTime(vDate As Variant) As String
If IsDate(vDate) Then
SQLDateTime = "#" & Format$(vDate, "mm\/dd\/yyyy hh:nn:ss") & "#"
End If
End Function

It would be possible (though inefficient) to do the string conparision based
on month and year. Because it's just a string, you can use any format you
wish, e.g. yyyymm (which would sort correctly.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi allen,

Sorry - I'm still struggling a bit with this. I changed to US format -
ie.
Const conDateFormat = "\#mm\/dd\/yyyy\#", and my text boxes on the form
are
set to short date format but I still got a syntax error, so I decided to
take
the advice from the first link about creating the wrapper function
SQLDate,

Being a bit of a novice I'm not sure how to call this as a const in my sub
function - could you advise?

Also - MissDate field with date MM/YY - I think I need to treat as a
number or text string rather than a date, can I still state to search
between
the 2 values MissDateFirst and MissDateLast if this is so??

Also - how would input masks affect the #\/ SQL??

Thanks for your advice - this is all a bit complex

Hazel

--
Kind Regards

Hazel


Allen Browne said:
Great. Good progress.

In the context of a WHERE clause, SQL statement, or literal VBA value,
you
must use the mm/dd/yyyy format, regardless of your regional settings.
See:
International Date Formats in Access
at:
http://allenbrowne.com/ser-36.html

For an example of how to format the dates into the WHERE clause, see
example
2 in this article:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

To explain the cryptic date format string inside the Format() function:
- Backslash indicates the followgin character is a literal, so \# is
interpreted by Format as #.

- The backslash before the forward slash again specifies a literal
forward
slash. If you don't do that, the Format() function interprets the forward
slash according to the date separator specified in your regional
settings,
which can give unreliable results.

If you format a date as "\#MM\/YY\#", you get: hash, month, slash, year,
hash. That is NOT a valid date in Access, so is useless in a WHERE
string.
You can demonstrate that in the Immediate Window (Ctrl+G) with:
? IsDate(Format(date, "\#MM\/YY\#"))
which returns False.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Hi Allen,
OK - I got every combo to work now except for limiting between the 2
dates
would you possibly be able to advise?
I'm attempting the short date format first "\#dd\/mm\/yyyy\#" but I
get a
syntax error '(DateofEntry>#29/06/)'

The second date format that I want to use is just "\#MM\/YY\#" - I
took
the
syntax from the website but I don't really understand it - can you
advise? -
I put the code I used for the short date below:
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If


--
Kind Regards

Hazel


:

First, it won't work if the report is already open.

Next, you have lots of combos there.
Break it down to just 2 or 3 and get that working.
You can then build it up again from there.

Particularly, omit the date fields (if you have any.) They need # as
the
delimiter.

Immediately before the OpenReport line, add:
Debug.Print strWhere

This will give you a print in the Immediate Window (Ctrl+G) of the
filter
being applied.

Hopefully those ideas will get you debugging your issue.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

message
Hi Allen,

Sorry - got a bit lost again,
My code seems to run OK but the records arent restricting? - not
sure
if
I've done everything that is required in my report - do I need to
add
dependency to the controls on the report?? -please find the code
below
does
this look correct?

rivate Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conMissFormat = "mm/yy"
Const conDateFormat = "dd/mm/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"



If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ &
Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ")
AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo &
""")
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ &
Me.OrgAccountCombo
&
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ &
Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no
start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no
End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end
dates.
strWhere = strField & " Between " &
Format(Me.EntryDateFirst,
conDateFormat) _
& " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If
If IsNull(Me.MissDateFirst) Then
If Not IsNull(Me.MissDateLast) Then 'End date, but no
start.
strWhere = strField & " < " & Format(Me.MissDateLast,
conMissFormat)
End If
Else
If IsNull(Me.MissDateLast) Then 'Start date, but no
End.
strWhere = strField & " > " & Format(Me.MissDateFirst,
conMissFormat)
Else 'Both start and end
dates.
strWhere = strField & " Between " &
Format(Me.MissDateFirst,
conMissFormat) _
& " And " & Format(Me.MissDateLast, conMissFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End Sub

--
Kind Regards

Hazel


:

Leave the criteria out of the query all together.

Instead, use a command button on your form to open the report. In
its
Click
event, build up the WhereCondition string based on the combos that
are
not
blank.

This example shows how to build up the string easily, by adding the
AND
to
the end of each one, and then chopping off the trailing AND at the
end:

Private Sub cmdPreview_Click()
dim strWhere As String
dim lngLen As Long

If Not IsNull(Me.Combo1) Then
strWhere = strWhere & "([Field1] = " & Me.Combo1 & ") AND "
End If
If Not IsNull(Me.Combo2) Then
strWhere = strWhere & "([Field2] = """ & Me.Combo2 & """)
AND
"
End If
'etc for other combos

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If

'Open the report
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
End Sub

Note the extra quotes for Field2. That's how to handle a Text type
field.
Date fields need # as the delimiter. There's an example of that at:
http://allenbrowne.com/casu-08.html


message
Hi there - hope somebody can help?

I've created a query to base a report on and I want the query to
limit
the
lines returned based on what is selected in several unbound combo
boxes
in
a
form I created.

I can make this work if the combo box on the form has a chosen
value -
but
how can I do this if the combo box control is empty??
ie. if no criteria was chosen in the combo box I want the query
to
return
all records,

The reason for this is I want a more flexible report which can be
limited
at
many levels,

Thanks for anyones advice
 
A

Allen Browne

In the international world that may be true, and JET works with that.

However, the native JET format is mm/dd/yyyy, with # delimiters.
 
G

Guest

Hi Allen,

Sorry I'm a bit of a novice and still struggling, I'm unsure how to call a
function into my SQL so I decided to persevere with my original code.

I'm getting the results in the view screen as below:

DateofEntry < 06/29 when enter End date, but no start
DateofEntry > 06/29 when enter Start date, but no End
DateofEntry Between 06/29/2005 And 06/29 when I enter Both start and end
dates.

Both text boxes have a short date format (UK)

It seems that the year is not being applied and I can't understand why - can
you help at all?
My code is below


Private Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conDateFormat = "mm\/dd\/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"

If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.SLATypeCombo) Then
strWhere = strWhere & "([SLAType] = """ & Me.SLATypeCombo & """) AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) & " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If
Debug.Print strWhere

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
 
A

Allen Browne

Since you are not concatenating the hashes into the string, put them back
into the format string:
Const conDateFormat = "\#mm\/dd\/yyyy\#"

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,

Sorry I'm a bit of a novice and still struggling, I'm unsure how to call a
function into my SQL so I decided to persevere with my original code.

I'm getting the results in the view screen as below:

DateofEntry < 06/29 when enter End date, but no start
DateofEntry > 06/29 when enter Start date, but no End
DateofEntry Between 06/29/2005 And 06/29 when I enter Both start and end
dates.

Both text boxes have a short date format (UK)

It seems that the year is not being applied and I can't understand why -
can
you help at all?
My code is below


Private Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conDateFormat = "mm\/dd\/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"

If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.SLATypeCombo) Then
strWhere = strWhere & "([SLAType] = """ & Me.SLATypeCombo & """)
AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) & " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If
Debug.Print strWhere

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
--
Kind Regards

Hazel


Allen Browne said:
In the international world that may be true, and JET works with that.

However, the native JET format is mm/dd/yyyy, with # delimiters.
 
G

Guest

Hi Allen,

I just tried this and then remembered why I removed them. I get below error:

run-time error 3075

Syntax error in date in Query Expression '(DateOfEntry Between #06/29/2005#
And #06/29/)'

I realise that removing them isn't solving the problem - would you be able
to help?

Thanks again
--
Kind Regards

Hazel


Allen Browne said:
Since you are not concatenating the hashes into the string, put them back
into the format string:
Const conDateFormat = "\#mm\/dd\/yyyy\#"

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

huzzlepuzzle said:
Hi Allen,

Sorry I'm a bit of a novice and still struggling, I'm unsure how to call a
function into my SQL so I decided to persevere with my original code.

I'm getting the results in the view screen as below:

DateofEntry < 06/29 when enter End date, but no start
DateofEntry > 06/29 when enter Start date, but no End
DateofEntry Between 06/29/2005 And 06/29 when I enter Both start and end
dates.

Both text boxes have a short date format (UK)

It seems that the year is not being applied and I can't understand why -
can
you help at all?
My code is below


Private Sub OpenReportAll_Click()


Dim stDocName As String
Dim lngLen As Long
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.

Const conDateFormat = "mm\/dd\/yyyy"

stDocName = "RptAll"
strField = "DateofEntry"

If Not IsNull(CustomerNameCombo) Then
strWhere = strWhere & "([CustomerName] = """ & Me.CustomerNameCombo
& """) AND "
End If
If Not IsNull(Me.SiteIDCombo) Then
strWhere = strWhere & "([SiteID] = " & Me.SiteIDCombo & ") AND "
End If
If Not IsNull(Me.SiteNameCombo) Then
strWhere = strWhere & "([SiteName] = """ & Me.SiteNameCombo & """)
AND "
End If
If Not IsNull(Me.ReportingSiteIDCombo) Then
strWhere = strWhere & "([ReportingSiteID] = " &
Me.ReportingSiteIDCombo & ") AND "
End If
If Not IsNull(Me.OrgAccountCombo) Then
strWhere = strWhere & "([OrgAccount] = """ & Me.OrgAccountCombo &
""") AND "
End If
If Not IsNull(Me.SLAMissTypeCombo) Then
strWhere = strWhere & "([SLAMissType] = """ & Me.SLAMissTypeCombo &
""") AND "
End If
If Not IsNull(Me.SLATypeCombo) Then
strWhere = strWhere & "([SLAType] = """ & Me.SLATypeCombo & """)
AND "
End If
If Not IsNull(Me.ConsecutiveMonthNumberCombo) Then
strWhere = strWhere & "([ConsecutiveMonthNumber] = " &
Me.ConsecutiveMonthNumberCombo & ") AND "
End If
If IsNull(Me.EntryDateFirst) Then
If Not IsNull(Me.EntryDateLast) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.EntryDateLast,
conDateFormat)
End If
Else
If IsNull(Me.EntryDateLast) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.EntryDateFirst,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.EntryDateFirst,
conDateFormat) & " And " & Format(Me.EntryDateLast, conDateFormat)
End If
End If

'Chop off the trailing " AND "
lngLen = Len(strWhere) - 5
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If
Debug.Print strWhere

'Open the report
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
--
Kind Regards

Hazel


Allen Browne said:
In the international world that may be true, and JET works with that.

However, the native JET format is mm/dd/yyyy, with # delimiters.
 
A

Allen Browne

Please take a look at that line and see if the final date looks complete to
you.

Any programming environment is exacting. We can't help you with that kind of
mistake.
 
G

Guest

Hi Allen,

OK - I couldn't see anything wrong - I replaced my text boxes with new ones
just in case - I went through and set everything to the same date format.
Eventually I re-read all of the documentation from your website and
discovered the line " This approach does not work with aggregate (GROUP BY)
or crosstab (TRANSFORM) queries.

Mine is a GROUP BY query so I assume that the error is linked to trying to
use this code with an aggregate query,
Do you know of an alternative I could use?

Thanks for your advice
 
A

Allen Browne

The line:
(DateOfEntry Between #06/29/2005# And #06/29/)
looks complete to you?

I'm not sure I know what to make of that final date.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top