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

Home Posts Topics Members FAQ

Filtering Access report by dates

1 New Member
Hi,

I am a novice to VB, using Access 2003 on a XP-SP2 platform, and I have a small database for tracking expenses, and want to be able to filter my expense report by date. I currently have a form with two list boxes, purchase and payment type, and then two date fields txtStartDate and txtEndDate. Filter works fine when no dates are chosen, problem is with my date picker - if I choose dates I get a "syntax error (missing operator)" error in my strWhere statement (I have used the debugger, but can't pinpoint the syntax error.. I have posted the script for the "Apply FIlter" command, any assistance would be appreciated. By the way, my date field in my source table us uDate, and in my report is "repDate" to avoid reserved fields.

Script:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub cmdApplyFilter_Click() 
  2. Dim varItem As Variant
  3. Dim strCategory As String
  4. Dim strPayment As String
  5. Dim strFilter As String
  6. Dim strReport As String 'Name of report to open.
  7. Dim strField As String 'Name of your date field.
  8. Dim strWhere As String 'Where condition for OpenReport.
  9.  
  10.  
  11. ' Check that the report is open
  12. If SysCmd(acSysCmdGetObjectState, acReport, "repExpense") <> acObjStateOpen Then
  13. MsgBox "You must open the report first."
  14. Exit Sub
  15. End If
  16. ' Build criteria string from lstCategory listbox
  17. For Each varItem In Me.lstCategory.ItemsSelected
  18. strCategory = strCategory & ",'" & Me.lstCategory.ItemData(varItem) _
  19. & "'"
  20. Next varItem
  21. If Len(strCategory) = 0 Then
  22. strCategory = "Like '*'"
  23. Else
  24. strCategory = Right(strCategory, Len(strCategory) - 1)
  25. strCategory = "IN(" & strCategory & ")"
  26. End If
  27. ' Build criteria string from lstPayment listbox
  28. For Each varItem In Me.lstPayment.ItemsSelected
  29. strPayment = strPayment & ",'" & Me.lstPayment.ItemData(varItem) _
  30. & "'"
  31. Next varItem
  32. If Len(strPayment) = 0 Then
  33. strPayment = "Like '*'"
  34. Else
  35. strPayment = Right(strPayment, Len(strPayment) - 1)
  36. strPayment = "IN(" & strPayment & ")"
  37. End If
  38. ' Build criteria string for date
  39.  
  40. strReport = "repExpense"
  41. strField = "Date"
  42.  
  43. If IsNull(Me.txtStartDate) Then
  44. If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
  45. strWhere = strField & "<=#" & Me.txtEndDate
  46. End If
  47. Else
  48. If IsNull(Me.txtEndDate) Then 'Start date, but no End.
  49. strWhere = strField & ">=#" & Me.txtStartDate
  50. Else 'Both start and end dates.
  51. strWhere = strField & " Between #" & Me.txtStartDate _
  52. & "# And #" & Me.txtEndDate & "#"
  53. End If
  54. End If
  55. Debug.Print strWhere 'For debugging purposes only.
  56. DoCmd.OpenReport strReport, acViewPreview, , strWhere
  57. ' Build filter string
  58. strFilter = "[Category] " & strCategory & _
  59. " AND [PmtType] " & strPayment & _
  60. " AND [uDate] " & strWhere
  61.  
  62. ' Apply the filter and switch it on
  63. With Reports![repExpense]
  64. .Filter = strFilter
  65. .FilterOn = True
  66. End With
  67. End Sub
  68.  
Jan 21 '08 #1
2 2584
Jim Doherty
897 Recognized Expert Contributor
Hi,

I am a novice to VB, using Access 2003 on a XP-SP2 platform, and I have a small database for tracking expenses, and want to be able to filter my expense report by date. I currently have a form with two list boxes, purchase and payment type, and then two date fields txtStartDate and txtEndDate. Filter works fine when no dates are chosen, problem is with my date picker - if I choose dates I get a "syntax error (missing operator)" error in my strWhere statement (I have used the debugger, but can't pinpoint the syntax error.. I have posted the script for the "Apply FIlter" command, any assistance would be appreciated. By the way, my date field in my source table us uDate, and in my report is "repDate" to avoid reserved fields.

Script:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub cmdApplyFilter_Click() 
  2. Dim varItem As Variant
  3. Dim strCategory As String
  4. Dim strPayment As String
  5. Dim strFilter As String
  6. Dim strReport As String 'Name of report to open.
  7. Dim strField As String 'Name of your date field.
  8. Dim strWhere As String 'Where condition for OpenReport.
  9.  
  10.  
  11. ' Check that the report is open
  12. If SysCmd(acSysCmdGetObjectState, acReport, "repExpense") <> acObjStateOpen Then
  13. MsgBox "You must open the report first."
  14. Exit Sub
  15. End If
  16. ' Build criteria string from lstCategory listbox
  17. For Each varItem In Me.lstCategory.ItemsSelected
  18. strCategory = strCategory & ",'" & Me.lstCategory.ItemData(varItem) _
  19. & "'"
  20. Next varItem
  21. If Len(strCategory) = 0 Then
  22. strCategory = "Like '*'"
  23. Else
  24. strCategory = Right(strCategory, Len(strCategory) - 1)
  25. strCategory = "IN(" & strCategory & ")"
  26. End If
  27. ' Build criteria string from lstPayment listbox
  28. For Each varItem In Me.lstPayment.ItemsSelected
  29. strPayment = strPayment & ",'" & Me.lstPayment.ItemData(varItem) _
  30. & "'"
  31. Next varItem
  32. If Len(strPayment) = 0 Then
  33. strPayment = "Like '*'"
  34. Else
  35. strPayment = Right(strPayment, Len(strPayment) - 1)
  36. strPayment = "IN(" & strPayment & ")"
  37. End If
  38. ' Build criteria string for date
  39.  
  40. strReport = "repExpense"
  41. strField = "Date"
  42.  
  43. If IsNull(Me.txtStartDate) Then
  44. If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
  45. strWhere = strField & "<=#" & Me.txtEndDate
  46. End If
  47. Else
  48. If IsNull(Me.txtEndDate) Then 'Start date, but no End.
  49. strWhere = strField & ">=#" & Me.txtStartDate
  50. Else 'Both start and end dates.
  51. strWhere = strField & " Between #" & Me.txtStartDate _
  52. & "# And #" & Me.txtEndDate & "#"
  53. End If
  54. End If
  55. Debug.Print strWhere 'For debugging purposes only.
  56. DoCmd.OpenReport strReport, acViewPreview, , strWhere
  57. ' Build filter string
  58. strFilter = "[Category] " & strCategory & _
  59. " AND [PmtType] " & strPayment & _
  60. " AND [uDate] " & strWhere
  61.  
  62. ' Apply the filter and switch it on
  63. With Reports![repExpense]
  64. .Filter = strFilter
  65. .FilterOn = True
  66. End With
  67. End Sub
  68.  
Hi cc and welcome to the scripts!

Rather than expect people to wade through and debug your code why not simply post the results of a debug.print line examples like you have embedded there then it should be clearly obvious where your code is failing on the where clause. From briefly looking it seems you are not closing off your DATE delimiters with the hash symbol

Jim :)
Jan 22 '08 #2
NeoPa
32,568 Recognized Expert Moderator MVP
Jim makes a good point. It's also helpful to know where in the code something happens (like error message or Debug.Print lines).
Anyway, have a look in Literal DateTimes and Their Delimiters (#) for a fuller understanding of how this works.
Jan 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
4392
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows of students and columns of activities and the data are the students' scores in each activity. No problem, almost. The problem is that there are five classes at the moment and will be more classes (or courses) in future semesters. I don't want...
3
3421
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am working on. I am working with MS Access 2002. And I am having a problem with one of my charts. I will explain how everything is laid out then go into details.
1
5156
by: Brad | last post by:
Thanks for taking the time to read my question. I have a table of data that has Date, Data and Category. I need to show, in a report, each Categories Data by Date. The Date has to be it's own column across the top and Category down the left side. As data is entered, the number of unique dates increases. As a result the
9
5731
by: RMC | last post by:
Hello, I'm looking for a way to parse/format a memo field within a report. The Access 2000 database (application) has an equipment table that holds a memo field. Within the report, the memo field is printed within the detailed area. The problem is, the apllication is not setup properly, thus the users are entering data within the memo field as: location1 1/1/2005 1/1/2006
3
1867
by: iamguyster | last post by:
Hi, I have an exercise I need to give to my pupils (I'm a teacher!) and I am trying to get a query working preferably using the query design view, without having to edit the SQL. The query involves three tables; Admission, Ward & Patient. The query is to initially return a list of all the wards with the total number of patients on each one. This is fine, using the following SQL: SELECT Wards.Name, Wards., Count(Patients.) AS FROM Wards...
9
2379
by: Anneybo | last post by:
Alright, I give up! I'm asking the experts. I have created a database that calculates PTO for employees. I need to be able to cache the report by user entered dates and specific employee names. I have been able to get the dates to work with the following VBA script: .. Private Sub cmdOK_Click() Dim strReport As String 'Name of report to open. Dim strField As String 'Name of your date field. Dim strWhere As String ...
18
2530
by: mlcampeau | last post by:
I have a lengthy query that I am now trying to filter. The query calculates an employee's Anniversary Date in which they are eligible for the next level of Annual Vacation. (i.e. For 1-6 years of service, you qualify for 15 days vacation, etc.) I am trying to filter the report to show only those names whose anniversary date falls into the following year. Employees are entitled to their vacation Jan.1 of their upcoming anniversary date. (i.e.,...
2
1568
by: Big X | last post by:
Hi, I am having a little trouble with some data I have been sent. Seems they want me to remove all the date that have a 5 year expiry date. I receive the data in csv file so the properties of the fields I can set in access to text or date which ever is needed. Atm I ran a sql query not like "*2013" which give me every thing before that year. I was wondering in the criteria box in access in expiry date field how to do this. I would like to be...
3
1594
by: zandiT | last post by:
Hello I have an access report and im using a query to filter the report using Date parameters eg Start Date-12 May 2009 and End Date-30 September 2009. the query works perfectly. My problem is recently i formated the date values so they appear as May 2009 instead of 12 May 2009. When i tried to use the filtering query again it did not work. Instead the result set from the query only showed records from the two dates in the parameter, ...
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
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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?

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.