473,407 Members | 2,546 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,407 software developers and data experts.

Me.Filter Date/Time field, exclude time

jonnycakes
Hello everyone,

Long story short, I have a button that i'm using on a form to allow the end user to filter by selection. This is working great for me, all except with my Date/Time fields.

My goal is to ignore the time in these fields and return every record that was entered that day, but I seem to be missing a key element.

For simplicity, both my field and my control are named "RecordSTS". Any ideas? You're help is greatly appreciated. Thank you in advance!

p.s. I know I need to add some more code to handle blank and null fields.

Expand|Select|Wrap|Line Numbers
  1. If Screen.PreviousControl = RecordSTS Then
  2. Me.Filter = "[RecordSTS] like " & Format(Me.RecordSTS, "\#mm\/dd\/yyyy\#")
  3. Me.FilterOn = True
  4. Else
  5. Screen.PreviousControl.SetFocus
  6. DoCmd.RunCommand acCmdFilterBySelection
  7. End If
  8.  
Dec 22 '12 #1

✓ answered by NeoPa

Hi there Jonny. Delays are never a problem. Let's have a look at your code, as you've posted it.
  1. When posting code, it is almost always necessary to post the procedure wrapper. This is almost always the most useful part of any procedure as far as passing information is concerned. Never post code that hasn't had a compile at least attempted, without including a clear warning that you are posting air-code. Line #8 indicates this code has not been copied from the VBA Editor window (or else you have somehow managed to teach your database that True is spelled true). Both possibilities indicate problems.
  2. Lines #3 & #4 both seem to be treating the string value returned from Format() as if it were a date. It can be converted for you automatically, but no reason to convert it just so that it can be converted back for use. I don't believe you need Date variables for this code.
  3. Line #6 compares two controls, but is really only comparing the values. If you want to check that the previous control actually is Me.RecordSTS then use the Is comparison.
  4. Line #7 seems to indicate your understanding of filtering dates in SQL has regressed. Post #1 illustrated the correct approach (See Literal DateTimes and Their Delimiters (#)).
  5. Lines #10 & #11 seem so obscure to me that I was unable to determine what it is you were trying to achieve by them - hence I cannot comment intelligently on them.
I would be looking to see something like (assuming Me.RecordSTS represents a date only value) :
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2.  
  3. strWhere = Replace("([RecordSTS] Between #%F# And #%T#)", _
  4.                    "%F", Format(CDate(Me.RecordSTS), "m\/d\/yyyy")
  5. strWhere = Replace(strWhere, "%T", Format(CDate(Me.RecordSTS) + 1, "m\/d\/yyyy")
  6. If Screen.PreviousControl Is Me.RecordSTS Then
  7.     Me.Filter = strWhere
  8.     Me.FilterOn = True
  9. Else
  10.     Screen.PreviousControl.SetFocus
  11.     DoCmd.RunCommand acCmdFilterBySelection
  12. End If

5 5557
NeoPa
32,556 Expert Mod 16PB
There are two options available :
  1. Add 1 day to the To DateTime value and use Between From and To.
  2. Use the DateValue() function to convert the value stored into just its date constituent.

Stangely, option #1, which is more complicated, would yield more efficient results if/when dealing with a very large dataset. Option #2 would only be noticeably slower on a very large dataset though. Functions called from SQL slow things down, but VBA defined functions slow it down much more so.
Dec 23 '12 #2
Yet again, NeoPa comes to the rescue. Please accept my apology for my delayed response. I hurt my back shortly after I posted this question. I went with option one, because this dataset will continue to grow. At least I think I went with option one, I've included the code that appears to be working. Do you see any issues with this approach, or did I use your suggestion correctly?

Thanks again!!

Expand|Select|Wrap|Line Numbers
  1. Dim StartDate as Date
  2. Dim EndDate as Date
  3. StartDate = Format(Me!RecordSTS, "mm-dd-yyyy")
  4. EndDate = DateAdd("D", 1, Format(Me!RecordSTS, "mm-dd-yyyy"))
  5.  
  6. If Screen.PreviousControl = RecordSTS then
  7. Me.Filter = "[RecordSTS] between #" & StartDate & "# AND #" & EndDate & "#"
  8. Me.filter = true
  9. Else
  10. Screen.PreviousControl.SetFocus
  11. DoCmd.RunCommand acCmdFilterBySelection
  12. End if 
  13.  
Jan 2 '13 #3
NeoPa
32,556 Expert Mod 16PB
Hi there Jonny. Delays are never a problem. Let's have a look at your code, as you've posted it.
  1. When posting code, it is almost always necessary to post the procedure wrapper. This is almost always the most useful part of any procedure as far as passing information is concerned. Never post code that hasn't had a compile at least attempted, without including a clear warning that you are posting air-code. Line #8 indicates this code has not been copied from the VBA Editor window (or else you have somehow managed to teach your database that True is spelled true). Both possibilities indicate problems.
  2. Lines #3 & #4 both seem to be treating the string value returned from Format() as if it were a date. It can be converted for you automatically, but no reason to convert it just so that it can be converted back for use. I don't believe you need Date variables for this code.
  3. Line #6 compares two controls, but is really only comparing the values. If you want to check that the previous control actually is Me.RecordSTS then use the Is comparison.
  4. Line #7 seems to indicate your understanding of filtering dates in SQL has regressed. Post #1 illustrated the correct approach (See Literal DateTimes and Their Delimiters (#)).
  5. Lines #10 & #11 seem so obscure to me that I was unable to determine what it is you were trying to achieve by them - hence I cannot comment intelligently on them.
I would be looking to see something like (assuming Me.RecordSTS represents a date only value) :
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2.  
  3. strWhere = Replace("([RecordSTS] Between #%F# And #%T#)", _
  4.                    "%F", Format(CDate(Me.RecordSTS), "m\/d\/yyyy")
  5. strWhere = Replace(strWhere, "%T", Format(CDate(Me.RecordSTS) + 1, "m\/d\/yyyy")
  6. If Screen.PreviousControl Is Me.RecordSTS Then
  7.     Me.Filter = strWhere
  8.     Me.FilterOn = True
  9. Else
  10.     Screen.PreviousControl.SetFocus
  11.     DoCmd.RunCommand acCmdFilterBySelection
  12. End If
Jan 2 '13 #4
NeoPa, I cannot thank you enough for the education that you've provided me, thank you! I have noted everything that you have shared with me. I've also learned a valuable lesson, and that is, never type out your code on your iPhone :)

The only thing that I can add to your great solution is that, lines 3 and 5 require an extra right parentheses to close the replace function.

NeoPa, you're doing a great service to this community, thanks again.
Jan 6 '13 #5
NeoPa
32,556 Expert Mod 16PB
JonnyCakes:
The only thing that I can add to your great solution is that, lines 3 and 5 require an extra right parentheses to close the replace function.
Touché Jonny :-D You caught me out in breaking my own cardinal rule! And expressed it so delicately too. I couldn't laugh out loud as I would have had to explain to all the people around, but it fixed a big smile on my face nevertheless. I'm afraid I do post air-code quite a lot if truth be told. Nevertheless, I do go to great pains to ensure it is as accurate as I can without the necessity to build a matching project for every thread I answer (which could get old after a while).

PS. If you need to type it out on the iPhone then do, but always be as careful as you can, and do please indicate that you had to when you post. We may sometimes respond with a comment suggesting you post again later when you have full access to the project. Sometimes it really is absolutely necessary. Sometimes we can work with an approximation though, if we know it to be one.
Jan 6 '13 #6

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

Similar topics

1
by: compusup3000 | last post by:
Hi, I have an orders database and I need to be able to write a query that groups sales by: 1. Date 2. Time of day (both am and pm) I currently have a date/time field named "Submitted" that...
2
by: JP SIngh | last post by:
Can someone please suggest the correct way to store the date & time in SQL Server? I want to store the date and time a record was created. What should the field type be in SQL Server? How can...
3
by: Atreju | last post by:
I have the need to import logs into an Access table. The first field is a date & time field, represented in this example: 05/15/2004 17:58:55.336 This is how the logs are reported. DateTime....
11
by: Dixie | last post by:
How can I programatically, take some Date/Time fields present in a table in the current database and change their type to text? dixie
3
by: matturbanowski | last post by:
Hi, I have a date/time field in a SQL2000 database, and what I would like to do is to filter on a specific part of the field, for example the time or hour. Supposing I have a set of data for...
7
by: Techhead | last post by:
I have a date/time field with a sql format of "datetime" The actual date/time data format is MM/DD/YYYY^hh:mm:ss:pm or "1/25/2007 12:00:16 AM" Both the date and time are combined on the same field...
9
by: Rotorian | last post by:
Good Morning, I have a table which has a "Date" field and a "Time" field. The "Date" field is format "Short Date" with a default value of "Date()" The "Time" field is format "Short Time" and the...
2
by: Bre035 | last post by:
The following is the code I am using in an attempt to add a Date and Time stamp to new records saved in main table. The date returns but the Time doesn't. I have tried various fixes like creating a...
3
by: tasawer | last post by:
Hi, I am practising SQL Serverso my question is from the basics. I could not get the right answer from other search engines but i am sure i will be rightly guided here. date and time are stored...
0
by: dowlingm815 | last post by:
I currently importing a csv file with an import specification declaring the field values. one field is a date/time field. when the csv file is imported, it clears the date field with null values....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...
0
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...

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.