473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Filter a Report Date field from a form

147 100+
Hi all,

Using Access 2007

I have a report called Sunday School Attendance based on a Query called qryAttendance the query is as follows:

[Sql]
SELECT tblSundaySchoolAttendance.StudentID, tblSundaySchoolAttendance.AttendanceDate, [Students Extended].[File As]
FROM tblSundaySchoolAttendance INNER JOIN [Students Extended] ON tblSundaySchoolAttendance.StudentID=[Students Extended].StudentID;
[/Sql]

I have a form called frmFiler that opens with the report on the reports OnOpen Event. In the form I have a combobox called Filter1 that filters the report by StudentID, this box works. I also have a txtbox Called Filter2 this is formated as a ShortDate and is supposed to filter the AttendanceDate field of the Query. On the form I have a command button called btnApplyFilter that runs the following code from the OnClick Event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnApplyFilter_Click()
  2. Dim strSQL As String, intCounter As Integer
  3.      'Build SQL String
  4.      For intCounter = 1 To 2
  5.        If Me("Filter" & intCounter) <> "" Then
  6.          strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & " And "
  7.        End If
  8.      Next
  9.  
  10.      If strSQL <> "" Then
  11.         'Strip Last " And "
  12.         strSQL = left(strSQL, (Len(strSQL) - 5))
  13.         'Set the Filter property
  14.         Reports![Sunday School Attendance].Filter = strSQL
  15.         Reports![Sunday School Attendance].FilterOn = True
  16.     End If
  17. End Sub
  18. [i]
[/I

The first filter works, no problem. The Date filter keeps returning a Run Time Error 3464 Data Type Mismatch in Criteria Expression error. I have confirmed that the AttendanceDate field on the report and in the query are formated for ShortDate. So I don't understand where the problem lies.

I have been trying to figure this out for days!!!!!
Thank you for all and any help
Dan
Feb 4 '08 #1
5 3991
Jim Doherty
897 Expert 512MB
Hi all,

Using Access 2007

I have a report called Sunday School Attendance based on a Query called qryAttendance the query is as follows:

[Sql]
SELECT tblSundaySchoolAttendance.StudentID, tblSundaySchoolAttendance.AttendanceDate, [Students Extended].[File As]
FROM tblSundaySchoolAttendance INNER JOIN [Students Extended] ON tblSundaySchoolAttendance.StudentID=[Students Extended].StudentID;
[/Sql]

I have a form called frmFiler that opens with the report on the reports OnOpen Event. In the form I have a combobox called Filter1 that filters the report by StudentID, this box works. I also have a txtbox Called Filter2 this is formated as a ShortDate and is supposed to filter the AttendanceDate field of the Query. On the form I have a command button called btnApplyFilter that runs the following code from the OnClick Event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnApplyFilter_Click()
  2. Dim strSQL As String, intCounter As Integer
  3. 'Build SQL String
  4. For intCounter = 1 To 2
  5. If Me("Filter" & intCounter) <> "" Then
  6. strSQL = strSQL & "[" & Me("Filter" & intCounter).Tag & "] " & " = " & Chr(34) & Me("Filter" & intCounter) & Chr(34) & " And "
  7. End If
  8. Next
  9.  
  10. If strSQL <> "" Then
  11. 'Strip Last " And "
  12. strSQL = left(strSQL, (Len(strSQL) - 5))
  13. 'Set the Filter property
  14. Reports![Sunday School Attendance].Filter = strSQL
  15. Reports![Sunday School Attendance].FilterOn = True
  16. End If
  17. End Sub
  18. [i]
[/I

The first filter works, no problem. The Date filter keeps returning a Run Time Error 3464 Data Type Mismatch in Criteria Expression error. I have confirmed that the AttendanceDate field on the report and in the query are formated for ShortDate. So I don't understand where the problem lies.

I have been trying to figure this out for days!!!!!
Thank you for all and any help
Dan
Hi Dan,

You say you're working with a 'date' and yet I see no hash delimiters (#) being used here in the sql build only quote marks char(34) you might want to look at that

Also it might benefit you to add the occasional debug.print statement to your code sequences and view them in the immediate window that way you,ll see your sql build is you assign the value of the strsql variable to the debug.print statement

You might want to also have a look at Neopa's howto article here

http://www.thescripts.com/forum/thread575420.html

Regards

Jim :)
Feb 4 '08 #2
DAHMB
147 100+
Hi Dan,

You say you're working with a 'date' and yet I see no hash delimiters (#) being used here in the sql build only quote marks char(34) you might want to look at that

Also it might benefit you to add the occasional debug.print statement to your code sequences and view them in the immediate window that way you,ll see your sql build is you assign the value of the strsql variable to the debug.print statement

You might want to also have a look at Neopa's howto article here

http://www.thescripts.com/forum/thread575420.html

Regards

Jim :)


Jim,

Thanks for the response. But I am a novice learning as I go and copying a lot of already invented wheels. I found the above code as an example for filtering text. I can not find a sample with dates included. Please point me in the correct direction in more beginners terms. I check the link you included and can not figure out the correct way to apply it here.

Thanks
Dan
Feb 5 '08 #3
Jim Doherty
897 Expert 512MB
Jim,

Thanks for the response. But I am a novice learning as I go and copying a lot of already invented wheels. I found the above code as an example for filtering text. I can not find a sample with dates included. Please point me in the correct direction in more beginners terms. I check the link you included and can not figure out the correct way to apply it here.

Thanks
Dan
Invented wheels are great if they have something in them to guide you like commented code 'lots do not' and unfortunately lots of beginners copy and paste stuff without understanding what the code is actually doing and then carry on on adding to the pile until we have a magical plate of spaghetti. I'm not saying that is happening in your case because you are wanting to understand the actual process involved here... so lets examine what is happening.

A FOR NEXT loop consisting of only 2 iterations is more or less a waste of time It is easier to type two end if statements and refer to your controls explicitly. That having been said, the principle here (for me) is not so much to be 'method' critical or to organise the program flow essentially... it is in this particular case, a question of knowing what the SQL is that is being built on the fly as the posted code illustrates, in order that we can fully understand if the compiler can deal with the built SQL and execute it.

In short the posted code is looping and grabbing the field name from the tag property of the control it references and then grabbing the 'value' presented by the control concatenating (appending) this to a string and stripping the last 5 characters from that string and rather hoping it will work as a valid SQL statement.

Fact is it does/did/probably excellently on the first control .....BUT.....I dare say the original syntax was built around something that did not include date fields as part of any looping process.

You have grabbed the code innocently without knowing the extent of its interaction with the potential data it might be faced with. I suppose thats the bottom line explanation.

Date fields in Access are wrapped in delimiting characters namely the HASH mark (#) which is CHAR(35) in the ansii character set. The posted code shows char(34) which is quotation marks.

The hash mark is the standard delimiter for dates. This is where your syntax problem lies. Now other systems might not have so much trouble and (can be more forgiving in this 'date' area) but one has to appreciate and understand what Access expects. (If you were using SQL Server I might not be emphasising this so much, various systems have various tolerances... please understand me)

To fully explain the ins and outs would require a book and here I sit with me in danger of reinventing the wheel in that area too. Much of what is understood is already written one simply needs to know where to target that material in order to gain a fuller understanding.

Now I know that you are using Access 2007 from a previous post of yours and that Access 2007 now has report on 'click events' from which you can navigate to screen forms to conduct anything that you wish to do ie: filter a form or go to a form at a specific record... and so on.

I cannot unfortunately replicate fully what you have there (on this box) because I do not personally use Access 2007 and have no need to presently as my scoped need for corporately supporting it is minimal. Suffice it to say this... in order to help you I have attached a zip file that shows where you need to be on this. It should open in Access 2007 ok. I hope it explains it a little to you, but if not get back to me. :))

Don't be put off by the use of commands like TYPEOF or the useage of the listbox they are included merely to try and make my illustrative example fit 'your' code (and at the same time maybe give you an idea of differences in method with different control) as opposed to me writing the solution to this for you.

Remember I do not have exactly what you have there in front of you, so its all guesswork my end so to speak. If you break apart the code and go into help the command useages are fully explained.

Regards

Jim :)
Attached Files
File Type: zip DanFilter.zip (26.7 KB, 185 views)
Feb 5 '08 #4
DAHMB
147 100+
Invented wheels are great if they have something in them to guide you like commented code 'lots do not' and unfortunately lots of beginners copy and paste stuff without understanding what the code is actually doing and then carry on on adding to the pile until we have a magical plate of spaghetti. I'm not saying that is happening in your case because you are wanting to understand the actual process involved here... so lets examine what is happening.

A FOR NEXT loop consisting of only 2 iterations is more or less a waste of time It is easier to type two end if statements and refer to your controls explicitly. That having been said, the principle here (for me) is not so much to be 'method' critical or to organise the program flow essentially... it is in this particular case, a question of knowing what the SQL is that is being built on the fly as the posted code illustrates, in order that we can fully understand if the compiler can deal with the built SQL and execute it.

In short the posted code is looping and grabbing the field name from the tag property of the control it references and then grabbing the 'value' presented by the control concatenating (appending) this to a string and stripping the last 5 characters from that string and rather hoping it will work as a valid SQL statement.

Fact is it does/did/probably excellently on the first control .....BUT.....I dare say the original syntax was built around something that did not include date fields as part of any looping process.

You have grabbed the code innocently without knowing the extent of its interaction with the potential data it might be faced with. I suppose thats the bottom line explanation.

Date fields in Access are wrapped in delimiting characters namely the HASH mark (#) which is CHAR(35) in the ansii character set. The posted code shows char(34) which is quotation marks.

The hash mark is the standard delimiter for dates. This is where your syntax problem lies. Now other systems might not have so much trouble and (can be more forgiving in this 'date' area) but one has to appreciate and understand what Access expects. (If you were using SQL Server I might not be emphasising this so much, various systems have various tolerances... please understand me)

To fully explain the ins and outs would require a book and here I sit with me in danger of reinventing the wheel in that area too. Much of what is understood is already written one simply needs to know where to target that material in order to gain a fuller understanding.

Now I know that you are using Access 2007 from a previous post of yours and that Access 2007 now has report on 'click events' from which you can navigate to screen forms to conduct anything that you wish to do ie: filter a form or go to a form at a specific record... and so on.

I cannot unfortunately replicate fully what you have there (on this box) because I do not personally use Access 2007 and have no need to presently as my scoped need for corporately supporting it is minimal. Suffice it to say this... in order to help you I have attached a zip file that shows where you need to be on this. It should open in Access 2007 ok. I hope it explains it a little to you, but if not get back to me. :))

Don't be put off by the use of commands like TYPEOF or the useage of the listbox they are included merely to try and make my illustrative example fit 'your' code (and at the same time maybe give you an idea of differences in method with different control) as opposed to me writing the solution to this for you.

Remember I do not have exactly what you have there in front of you, so its all guesswork my end so to speak. If you break apart the code and go into help the command useages are fully explained.

Regards

Jim :)

Jim,

Thank you very much!! I am still studying what you sent, it is great. I was able to get my filter working perfectly!!!

Thanks
Dan
Feb 8 '08 #5
Jim Doherty
897 Expert 512MB
Jim,

Thank you very much!! I am still studying what you sent, it is great. I was able to get my filter working perfectly!!!

Thanks
Dan

I am so pleased helped you!

Regards

Jim :)
Feb 9 '08 #6

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

Similar topics

3
by: arthur-e | last post by:
I can filter a query for a report on a field of a subform BUT now.... I'd like to be able to select more than one item in a multi-list box to select all the records. ALSO to use two fields (or...
0
by: CSDunn | last post by:
Hello, I have a problem with field filtering between an Access 2000 Project form (the application is called CELDT), and the report that shows the results of the filter. Both the form and the...
3
by: Nathan Bloomfield | last post by:
I am having difficulty filtering dates for a report. I have written the following code on a popup form which works with regular date fields , however, I am now trying to apply the same concept to...
3
by: Afton | last post by:
I would like to make a form that filters a report by Supervisor and by starting and ending date. I have the supervisors in a combo box, and that works. However, I do not know how to code to let...
1
by: lorirobn | last post by:
Hi, I have a report that works just fine. Now I would like to add the capability to choose selection criteria to limit what is displayed. I created several reports that do this, but they used...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
1
by: Cara Murphy | last post by:
Hi There! Hoping you are able to help me with a filtering scenario: I have created a pop up form (Filter) to filter a report (Open Issues), which currently has a number of combo boxes which...
3
by: franc sutherland | last post by:
Hello, I have a report which I filter using the me.filter command in the OnOpen event. Me.Filter = "OrderID=" & Forms!variable_form_name! Me.FilterOn = True I want to be able to open that...
3
by: Gord | last post by:
I'm trying to filter a report with a date in VB code. If I type an actual date bracketed with the pound symbol (i.e. #3-Jul-08#) I can get the filter to work. I can't seem to get it to work by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.