Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning VB.NET Databases
This is the forum to discuss the Wrox book Beginning VB.NET Databases by Thearon Willis; ISBN: 9780764568008
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning VB.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 22nd, 2007, 07:07 AM
Authorized User
Join Date: Jan 2006
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default DateDiff Question

Hello guys,

From the book I've been looking at comparing date/time values and currently I'm presented with a problem.

I have an application that processes and stores the time in an Access table. I use the Today() function so that 12:00 is prefixed to the time.

However the application also queries a given time value (on a form) against a specified time field in the table. The time value on the form of course doesn't contain the date so DateDiff doesn't work when comparing the minutes and seconds:


'Compare minutes
Select.....where DateDiff("n",[datefieldintable], [timefieldonform]) = 0

'and compare minutes

Select.....where DateDiff("s",[datefieldintable], [timefieldonform]) = 0

So what I've worked out is that my time form has to have the date appended to the time and the word 'am' appended to it for the query to work. To get the table date/time field's date I've attempted to use the Datepart() function to pull out the dd, mm, and yyyy, append the time and then the word's "am". Here's what I've got so far:

"DateDiff('n'," & strdtfield & ", #" & _
"DatePart('d'," & strdtfield & ")&""/""&" & _
"DatePart('m'," & strdtfield & ")&""/""&" & _
"DatePart('yyyy'," & strdtfield & ") " & _
"12:" & txtfldTime.Text & "am#) = 0")

Here's the SQL string output:

SELECT * FROM [CrewMembers] LEFT JOIN [FitnessTestHistory] ON [CrewMembers].[CrewMemberID] = [FitnessTestHistory].[CrewMemberID] WHERE (DateDiff('n',[FitnessTestHistory].[AbdominalHold], #DatePart('d',[FitnessTestHistory].[AbdominalHold])&"/"&DatePart('m',[FitnessTestHistory].[AbdominalHold])&"/"&DatePart('yyyy',[FitnessTestHistory].[AbdominalHold]) 12:00:00am#) = 0) ORDER BY [FitnessTestHistory].[AbdominalHold]

Here's the error I'm getting

"Syntax error in date in query expression"

Can someone help me correct the date part of the string please? Thanks.
Reply With Quote
  #2 (permalink)  
Old August 25th, 2007, 05:12 AM
Thearon's Avatar
Wrox Author
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts

It looks like there needs to be a space in your time. For example, you have "12:00:00am" and I believe it needs to be "12:00:00 am".

If that does not work, could you post two dates and times that you are trying to compare? Also a sample of the date and time values as it appears in your database would be helpful.

Reply With Quote
  #3 (permalink)  
Old August 25th, 2007, 10:11 PM
Authorized User
Join Date: Jan 2006
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Thearon,

That was the answer. I didn't have a space between the "00" and the "am".

Thanks for your help.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DateDiff zahyea .NET Framework 1.x 0 October 16th, 2007 04:38 AM
How to get Milliseconds from DateDiff ? ashu_from_india Pro VB 6 2 July 5th, 2007 03:27 AM
datediff ebburks Access 2 July 28th, 2006 06:58 PM
help with DateDiff isaac2004 Classic ASP Basics 9 January 16th, 2006 02:34 AM
Using DateDiff iancrabtree Access 2 November 27th, 2005 07:33 PM

All times are GMT -4. The time now is 11:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.