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: - Private Sub cmdApplyFilter_Click()
-
Dim varItem As Variant
-
Dim strCategory As String
-
Dim strPayment As String
-
Dim strFilter As String
-
Dim strReport As String 'Name of report to open.
-
Dim strField As String 'Name of your date field.
-
Dim strWhere As String 'Where condition for OpenReport.
-
-
-
' Check that the report is open
-
If SysCmd(acSysCmdGetObjectState, acReport, "repExpense") <> acObjStateOpen Then
-
MsgBox "You must open the report first."
-
Exit Sub
-
End If
-
' Build criteria string from lstCategory listbox
-
For Each varItem In Me.lstCategory.ItemsSelected
-
strCategory = strCategory & ",'" & Me.lstCategory.ItemData(varItem) _
-
& "'"
-
Next varItem
-
If Len(strCategory) = 0 Then
-
strCategory = "Like '*'"
-
Else
-
strCategory = Right(strCategory, Len(strCategory) - 1)
-
strCategory = "IN(" & strCategory & ")"
-
End If
-
' Build criteria string from lstPayment listbox
-
For Each varItem In Me.lstPayment.ItemsSelected
-
strPayment = strPayment & ",'" & Me.lstPayment.ItemData(varItem) _
-
& "'"
-
Next varItem
-
If Len(strPayment) = 0 Then
-
strPayment = "Like '*'"
-
Else
-
strPayment = Right(strPayment, Len(strPayment) - 1)
-
strPayment = "IN(" & strPayment & ")"
-
End If
-
' Build criteria string for date
-
-
strReport = "repExpense"
-
strField = "Date"
-
-
If IsNull(Me.txtStartDate) Then
-
If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
-
strWhere = strField & "<=#" & Me.txtEndDate
-
End If
-
Else
-
If IsNull(Me.txtEndDate) Then 'Start date, but no End.
-
strWhere = strField & ">=#" & Me.txtStartDate
-
Else 'Both start and end dates.
-
strWhere = strField & " Between #" & Me.txtStartDate _
-
& "# And #" & Me.txtEndDate & "#"
-
End If
-
End If
-
Debug.Print strWhere 'For debugging purposes only.
-
DoCmd.OpenReport strReport, acViewPreview, , strWhere
-
' Build filter string
-
strFilter = "[Category] " & strCategory & _
-
" AND [PmtType] " & strPayment & _
-
" AND [uDate] " & strWhere
-
-
' Apply the filter and switch it on
-
With Reports![repExpense]
-
.Filter = strFilter
-
.FilterOn = True
-
End With
-
End Sub
-
2 2584
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: - Private Sub cmdApplyFilter_Click()
-
Dim varItem As Variant
-
Dim strCategory As String
-
Dim strPayment As String
-
Dim strFilter As String
-
Dim strReport As String 'Name of report to open.
-
Dim strField As String 'Name of your date field.
-
Dim strWhere As String 'Where condition for OpenReport.
-
-
-
' Check that the report is open
-
If SysCmd(acSysCmdGetObjectState, acReport, "repExpense") <> acObjStateOpen Then
-
MsgBox "You must open the report first."
-
Exit Sub
-
End If
-
' Build criteria string from lstCategory listbox
-
For Each varItem In Me.lstCategory.ItemsSelected
-
strCategory = strCategory & ",'" & Me.lstCategory.ItemData(varItem) _
-
& "'"
-
Next varItem
-
If Len(strCategory) = 0 Then
-
strCategory = "Like '*'"
-
Else
-
strCategory = Right(strCategory, Len(strCategory) - 1)
-
strCategory = "IN(" & strCategory & ")"
-
End If
-
' Build criteria string from lstPayment listbox
-
For Each varItem In Me.lstPayment.ItemsSelected
-
strPayment = strPayment & ",'" & Me.lstPayment.ItemData(varItem) _
-
& "'"
-
Next varItem
-
If Len(strPayment) = 0 Then
-
strPayment = "Like '*'"
-
Else
-
strPayment = Right(strPayment, Len(strPayment) - 1)
-
strPayment = "IN(" & strPayment & ")"
-
End If
-
' Build criteria string for date
-
-
strReport = "repExpense"
-
strField = "Date"
-
-
If IsNull(Me.txtStartDate) Then
-
If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
-
strWhere = strField & "<=#" & Me.txtEndDate
-
End If
-
Else
-
If IsNull(Me.txtEndDate) Then 'Start date, but no End.
-
strWhere = strField & ">=#" & Me.txtStartDate
-
Else 'Both start and end dates.
-
strWhere = strField & " Between #" & Me.txtStartDate _
-
& "# And #" & Me.txtEndDate & "#"
-
End If
-
End If
-
Debug.Print strWhere 'For debugging purposes only.
-
DoCmd.OpenReport strReport, acViewPreview, , strWhere
-
' Build filter string
-
strFilter = "[Category] " & strCategory & _
-
" AND [PmtType] " & strPayment & _
-
" AND [uDate] " & strWhere
-
-
' Apply the filter and switch it on
-
With Reports![repExpense]
-
.Filter = strFilter
-
.FilterOn = True
-
End With
-
End Sub
-
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 :)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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
|
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
|
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...
| |
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 ...
|
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.,...
|
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...
|
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, ...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |