473,657 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Str where and muliple criteria problem

Hi Folks

I wondered if someone could point me in the right direction before I
completely tear my hair out.
I have a user selection form where options can be selected for a report.
Users now want to also filter the options by date selections or not if they
wish.

I added to unbound text fields to input the start and end dates and inserted
them into my str Where code. It was working fine until the these were added.
Now I get a missing syntax error, or it completely ignores the dates entered
if other options and the dates are selected.I cannot seem to make this work
if a user selects options and dates, it only works if the date input boxes
are ignored.

I am in england and use dd/m/yyyy format but have included a const to change
it to the american format.Could this be a problem or can you not have string
and dates selections in a str where clause

Can anyone see where I am going wrong? Thanks in advance for any help offered

Dim RptName As String
Dim strWhere As String
Dim strInput As String 'Date inputted field.
Dim strBIU As String 'BIU date field
Const conDateFormat = "\#mm\/dd\/yyyy\#"
Dim strID As String
Dim strfield As String

RptName = Forms!ReportsSe lector!ReportNa me
strfield = "Date inputted"
If Not IsNull(Me!ChWar d) Then
strWhere = "Ward = '" & Me!ChWard & "' And"
End If

If (Me!ChWard) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If

If Not IsNull(Me!ChAre a) Then
strWhere = strWhere & " Area = '" _
& Me!ChArea & "' And"
End If

If (Me!ChArea) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChCas eOfficer) Then
strWhere = strWhere & " CaseOfficer = '" _
& Me!ChCaseOffice r & "' And"
End If

If (Me!ChCaseOffic er) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChRoa d) Then
strWhere = strWhere & " [Road] = '" _
& Me!ChRoad & "' And"
End If

If (Me!ChRoad) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChPro p) Then
strWhere = strWhere & " [Property Type] = '" _
& Me!ChProp & "' And"
End If

If (Me!ChProp) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If

If Not IsNull(Me!ChOpt ion) Then
strWhere = strWhere & " [back into use status] = '" _
& Me.ChOption & "' And" '& Me.ChOption & "'"
End If

If (Me!ChOption) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me.Start Date) Then 'start no end date
strWhere = strWhere & strfield & " >= " & Format(Me.Start Date,
conDateFormat) & "' And "

End If

If Not IsNull(Me.EndDa te) Then 'end date no start
strWhere = strWhere & strfield & " <= " & Format(Me.EndDa te,
conDateFormat) & "' And "

End If
If Not IsNull(Me.Start Date) And Not IsNull(Me.EndDa te) Then
'Both start and end dates.
strWhere = strWhere & strfield & " Between " & Format(Me.Start Date,
conDateFormat _
) & " And " & Format(Me.EndDa te, conDateFormat) & "'"
End If

If Right(strWhere, 4) = " And" Then
strWhere = Trim(Left(strWh ere, Len(strWhere) - 4))
Else
strWhere = Trim(strWhere)
End If

DoCmd.OpenRepor t RptName, acViewPreview, , strWhere

DoCmd.OpenForm "SelectReportCa tegory", , , , , acHidden
Exit_CmdPrevw_C lick:
Exit Sub

Prev_Err:
If Err = 2501 Then

Resume Exit_CmdPrevw_C lick
Else
MsgBox Err.Description
Resume Exit_CmdPrevw_C lick
End If
End Sub

Thanks in advance
Ceebaby

--
Ceebaby

Trying to be great at Access

Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200801/1

Jan 10 '08 #1
2 5128
Ceebaby via AccessMonster.c om wrote:
Hi Folks

I wondered if someone could point me in the right direction before I
completely tear my hair out.
I have a user selection form where options can be selected for a report.
Users now want to also filter the options by date selections or not if they
wish.

I added to unbound text fields to input the start and end dates and inserted
them into my str Where code. It was working fine until the these were added.
Now I get a missing syntax error, or it completely ignores the dates entered
if other options and the dates are selected.I cannot seem to make this work
if a user selects options and dates, it only works if the date input boxes
are ignored.

I am in england and use dd/m/yyyy format but have included a const to change
it to the american format.Could this be a problem or can you not have string
and dates selections in a str where clause

Can anyone see where I am going wrong? Thanks in advance for any help offered

Dim RptName As String
Dim strWhere As String
Dim strInput As String 'Date inputted field.
Dim strBIU As String 'BIU date field
Const conDateFormat = "\#mm\/dd\/yyyy\#"
Dim strID As String
Dim strfield As String

RptName = Forms!ReportsSe lector!ReportNa me
strfield = "Date inputted"
If Not IsNull(Me!ChWar d) Then
strWhere = "Ward = '" & Me!ChWard & "' And"
End If

If (Me!ChWard) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If

If Not IsNull(Me!ChAre a) Then
strWhere = strWhere & " Area = '" _
& Me!ChArea & "' And"
End If

If (Me!ChArea) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChCas eOfficer) Then
strWhere = strWhere & " CaseOfficer = '" _
& Me!ChCaseOffice r & "' And"
End If

If (Me!ChCaseOffic er) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChRoa d) Then
strWhere = strWhere & " [Road] = '" _
& Me!ChRoad & "' And"
End If

If (Me!ChRoad) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me!ChPro p) Then
strWhere = strWhere & " [Property Type] = '" _
& Me!ChProp & "' And"
End If

If (Me!ChProp) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If

If Not IsNull(Me!ChOpt ion) Then
strWhere = strWhere & " [back into use status] = '" _
& Me.ChOption & "' And" '& Me.ChOption & "'"
End If

If (Me!ChOption) = "All" Then
DoCmd.OpenRepor t RptName, acViewPreview
End If
If Not IsNull(Me.Start Date) Then 'start no end date
strWhere = strWhere & strfield & " >= " & Format(Me.Start Date,
conDateFormat) & "' And "

End If

If Not IsNull(Me.EndDa te) Then 'end date no start
strWhere = strWhere & strfield & " <= " & Format(Me.EndDa te,
conDateFormat) & "' And "

End If
If Not IsNull(Me.Start Date) And Not IsNull(Me.EndDa te) Then
'Both start and end dates.
strWhere = strWhere & strfield & " Between " & Format(Me.Start Date,
conDateFormat _
) & " And " & Format(Me.EndDa te, conDateFormat) & "'"
End If

If Right(strWhere, 4) = " And" Then
strWhere = Trim(Left(strWh ere, Len(strWhere) - 4))
Else
strWhere = Trim(strWhere)
End If

DoCmd.OpenRepor t RptName, acViewPreview, , strWhere

DoCmd.OpenForm "SelectReportCa tegory", , , , , acHidden
Exit_CmdPrevw_C lick:
Exit Sub

Prev_Err:
If Err = 2501 Then

Resume Exit_CmdPrevw_C lick
Else
MsgBox Err.Description
Resume Exit_CmdPrevw_C lick
End If
End Sub

Thanks in advance
Ceebaby
Here's an example from I reply I gave yesterday

Dim strFilter as string
If Not IsNull(Me.Combo Name) Then
strFilter = "TableNameF ld = '" & Me.ComboName & "' And "
Endif
If Not IsNull(Me.FromD ate) Then
strFilter = strFilter & "DateFld >= #" & Me.FromDate & "# And "
Endif
If Not IsNull(Me.ToDat e) Then
strFilter = strFilter & "DateFld >= #" & Me.ToDate & "# And "
Endif

'if there is a filter, remove the trailing 'And'
If strFilter "" Then strFilter = Left(strFilter, Len(strFilter)-5)
Docmd.OpenRepor t YourReportName, ,,strFilter

Remember, strings are surrounded by ' or ", dates by #, and numbers by
nothing.

If you need to convert dates, perhaps you should consider the DateSerial
function. I don't know if this will help. I don't think formatting
your dates will help in your case.
d = date()
? DateSerial(Year (d),Month(d),Da y(d))
1/10/2008
Here's some info on international dates in case you need it.
http://allenbrowne.com/ser-36.html

Regarding your code...I'd go thru all of the criteria list first and
build your filter then call the report.

Mermaid
http://www.youtube.com/watch?v=cucKok05nEI
Jan 10 '08 #2
On Jan 10, 4:04*am, "Ceebaby via AccessMonster.c om" <u6919@uwewrote :
Hi Folks

I wondered if someone could point me in the right direction before I
completely tear my hair out.
I have a user selection form where options can be selected for a report.
Users now want to also filter the options by date selections or not if they
wish.

I added to unbound text fields to input the start and end dates and inserted
them into my str Where code. It was working fine until the these were added.
Now I get a missing syntax error, *or it completely ignores the dates entered
if other options and the dates are selected.I cannot seem to make this work
if a user selects options and dates, it only works if the date input boxes
are ignored.

I am in england and use dd/m/yyyy format but have included a const to change
it to the american format.Could this be a problem or can you not have string
and dates selections in a str where clause

Can anyone see where I am going wrong? Thanks in advance for any help offered

* * * * Dim RptName As String
* * * * Dim strWhere As String
* * * * Dim strInput As String * * *'Date inputted field.
* * * * Dim strBIU As String * * * 'BIU date field
* * * * Const conDateFormat = "\#mm\/dd\/yyyy\#"
* * * * Dim strID As String
* * * * Dim strfield As String

* * * * RptName = Forms!ReportsSe lector!ReportNa me
* * * * strfield = "Date inputted"

*If Not IsNull(Me!ChWar d) Then
* * * *strWhere = "Ward = '" & Me!ChWard & "' And"
* End If

* * * * * * If (Me!ChWard) = "All" Then
* * * * * * * * * * DoCmd.OpenRepor t RptName, acViewPreview
* * * * * * End If

* *If Not IsNull(Me!ChAre a) Then
* * * *strWhere = strWhere & " Area = '" _
* * * * * *& Me!ChArea & "' And"
* *End If

* * * * * * If (Me!ChArea) = "All" Then
* * * * * * * * DoCmd.OpenRepor t RptName, acViewPreview
* * * * * * End If

* *If Not IsNull(Me!ChCas eOfficer) Then
* * * *strWhere = strWhere & " CaseOfficer = '" _
* * * * * *& Me!ChCaseOffice r & "' And"
* *End If

* * * * * * * *If (Me!ChCaseOffic er) = "All" Then
* * * * * * * * * * *DoCmd.OpenRepo rt RptName, acViewPreview
* * * * * * * *End If

* * If Not IsNull(Me!ChRoa d) Then
* * * *strWhere = strWhere & " [Road] = '" _
* * * * * *& Me!ChRoad & "' And"
* *End If

* * * * * * * *If (Me!ChRoad) = "All" Then
* * * * * * * * * * *DoCmd.OpenRepo rt RptName, acViewPreview
* * * * * * * *End If

* * If Not IsNull(Me!ChPro p) Then
* * * *strWhere = strWhere & " [Property Type] = '" _
* * * * * *& Me!ChProp & "' And"
* *End If

* * * * * * * *If (Me!ChProp) = "All" Then
* * * * * * * * * * *DoCmd.OpenRepo rt RptName, acViewPreview
* * * * * * * *End If

* * If Not IsNull(Me!ChOpt ion) Then
* * * *strWhere = strWhere & " [back into use status] = '" _
* * * * *& Me.ChOption & "' And" *'& Me.ChOption & "'"
* * * End If

If (Me!ChOption) = "All" Then
* * * * * * * * * * *DoCmd.OpenRepo rt RptName, acViewPreview
* * * * * * * *End If

* *If Not IsNull(Me.Start Date) Then * * *'start no end date
* * * * * *strWhere = strWhere & strfield & " >= " & Format(Me.Start Date,
conDateFormat) & "' And "

* * * * * *End If

* * If Not IsNull(Me.EndDa te) Then * * * 'end date no start
* * * * * *strWhere = strWhere & strfield & " <= " & Format(Me.EndDa te,
conDateFormat) & "' And "

* * * * * *End If

* * *If Not IsNull(Me.Start Date) And Not IsNull(Me.EndDa te) Then
'Both start and end dates.
* * * * * *strWhere = strWhere & strfield & " Between " & Format(Me.Start Date,
conDateFormat _
* * * * * * * *) & " And " & Format(Me.EndDa te, conDateFormat) & "'"
* * * * * End If

* *If Right(strWhere, 4) = " And" Then
* * * *strWhere = Trim(Left(strWh ere, Len(strWhere) - 4))
* *Else
* * * *strWhere = Trim(strWhere)
* *End If

* *DoCmd.OpenRepo rt RptName, acViewPreview, , strWhere

* * DoCmd.OpenForm "SelectReportCa tegory", , , , , acHidden

Exit_CmdPrevw_C lick:
* *Exit Sub

Prev_Err:
* If Err = 2501 Then

* * * Resume Exit_CmdPrevw_C lick
* * Else
* * * *MsgBox Err.Description
* * * *Resume Exit_CmdPrevw_C lick
* * End If
End Sub

Thanks in advance
Ceebaby

--
Ceebaby

Trying to be great atAccess

Message posted via AccessMonster.c omhttp://www.accessmonst er.com/Uwe/Forums.aspx/databases-ms-access/2008...
Ceebaby:

Try enclosing the input dates with #. For example: ..."Between #" &
Me.StartDate & "# and #"...

Milton - In rainy California, USA
Jan 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5263
by: AAVF | last post by:
Hi We have a problem with a query. An Access database links via ODBC to a UNIX server. To speed things, we use the ODBC to load the relevant tables to the local PC that runs Access so that all querying is done locally. One of the reports we run allows the user to list all invoices within a period. They are also allowed to select a customer code and a product set on
19
3534
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main form, the subform returns the records based on the two criteria. The criteria used on the main form are values selected in two list boxes. When the user clicks on the first list box (lstCollege), it returns values in the second list box...
3
3294
by: MLH | last post by:
Am repeating question with different subject heading, perhaps stating more clearly my problem... I have an A97 query (qryVehiclesNowners2) that has a table field in it named . Depending on the selections made in a number of criteria choices on a form, a field on the form will have string values in it like... Between #12/1/2004# And #12/31/2004# - or - >= #6/10/2005# - or -
8
2956
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. Now, let's say the user wants to be able to send mailings to people who have various combinations of membership and non-membership in those groups. Here's a medium-complex example: (Knitting Group or Macrame Group) and Active Contact and Mailing...
11
1281
by: grizduck | last post by:
Hi- I have created a .NET web application that uses 6 aspx pages. As the users goes from one page to the next several variables are set and displayed on future pages. I found out today that if one person is on Step 4, for instance, if another user goes to Step 3, all of the information the 1st user entered displays. Am I missing a setting in Webconfig or in IIS that allows each user to get their own instance of the program? Any help...
2
5730
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and icluding the selected date or all records on or after the selected date The user selects either "=", >=" or "<=" from a combo box and then a date from another combobox. The combination of thse two choices is then set in an unbound textbox so...
49
5991
by: martin DH | last post by:
Hello all, I'm back with another SQL related problem. The details are below, but in short: I am using Access 2003. I have a table whose structure may include four different associate names per record (full structure below). I have a query that should return records that have one or more entered names (as criteria from a form) belonging to them. TableName=COMPILE_HIST CompileID, autonumber, PK ResultsID, number Month, number, FK...
20
7494
by: anthonyk | last post by:
Hi there, im hoping someone might be able to painlessly tell me what im incorrectly doing here/expecting. Im using Access 2007 I am creating a query to show ongoing tasks in a database. in the fields I have Start date (dd/mm/yyyy) Duration (months) Basically i want the query to calculate is if start date + Duration is greater than today...
17
5686
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of 'Text' and has an input mask of '00/00', so all expiry dates are set out for example as 10/13 (which represents October 2013). I have hada brief go at trying to work it out, but I was unable to
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.