INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Problem querying using Now in a database table.

Problem querying using Now in a database table.

(OP)
I execute a simple DELETE on a table to remove old records earlier than 15 minutes ago
The date column (Scheduled) is formatted as a Date/Time and contains a date and time for each row.

dbS.Execute "DELETE Scheduled From MyTable WHERE Scheduled < DateAdd('n',-15,Now());"

This works fine for times from 00:01 am to 11:59 pm but does not remove any records when executed just past midnight if there are any entries still there from the previous day such as -
Say there are 5 records either side of midnight.
3/4/2015 11:55 pm
3/4/2015 11:58 pm
4/4/2015 0:03 am
4/4/2015 0:05 am
4/4/2015 0:10 am

If I run the query 4/4/2015 0:19 am it should remove only the first three entries - but it doesn't remove any!
It works fine around midday (am's & pm's reversed).

What am I doing wrong in my execute statement? Do I have to somehow separately consider Date and Time rather than use Now?

This is used in a bus departure sign running 24/7 but luckily it does not affect the end result because the bus has gone by then and removed from the screen anyway but it worries me that I can't find the reason.

RE: Problem querying using Now in a database table.

(OP)
I worked out another way of doing it that works using IIF.

CODE -->

dbS.Execute "DELETE IIf([Scheduled] > DateAdd('n',-15,Now()),0,1) AS Expr " _
& "From MyTable " _
& "WHERE ((IIf([Scheduled] > DateAdd('n',-15,Now()),0,1))=1);" 

This removes all Schedule entries more than 15 minutes ago

I can't see why the earlier one doesn't!

RE: Problem querying using Now in a database table.

I'm surprised either one works at all. The syntax should be:

CODE

DELETE From MyTable WHERE Scheduled < DateAdd('n',-15,Now()) 

... assuming Scheduled is a DATE column.

No idea why that wouldn't work every time.

See DELETE Statement (Microsoft Access SQL)

RE: Problem querying using Now in a database table.

(OP)
That was my exactly first example that doesn't always work. As I said it does not work when the times span either side of midnight.
Maybe it is because I use International date format dd/mm/yyyy but how do I put #'s around Now!

The second example has to be the other way around because it is creating a 0 if before [Scheduled] or 1 if later. The where-ing is then on the 1 instead of the date. Thats why it works.

I probably only needed

CODE -->

dbS.Execute "DELETE From MyTable WHERE ((IIf([Scheduled] > DateAdd('n',-15,Now()),0,1))=1);" 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close