Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 18th, 2007, 05:11 AM
Authorized User
 
Join Date: Jan 2007
Location: , , United Kingdom.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query a Sql Database Date Field

Hoping for some advice regarding an Access Form I'm designing.

When I do a select query from Query Analyser the date field comes back for example 2007-10-12 00:00:00.

Now when I run a select query from the front end using vb code, the select query is written with a where statement as 12/10/2007. This select query returns no results.

How do I get the front end to do a date query the backend will understand??

Reply With Quote
  #2 (permalink)  
Old October 18th, 2007, 06:50 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

What data type is this field in the SQL Server?

Can you post the select statement from the front end? Access SQL will use ## to indicate a date field, but SQL Server will not accept that.

Worst case you can change your select statement to:

Dim dtYear As Integer
Dim dtMonth As Integer
Dim dtDay As Integer

dtYear = DatePart("yyyy", Me.DateField)
dtMonth = DatePart("m", Me.DateField)
dtDay = DatePart("d", Me.DateField) 'that might be "dd"

"SELECT * FROM tblSQLYourTable WHERE DatePart("yyyy", [SQLDateField) = '" & dtYear & "' AND DatePart("m", [SQLDateField]) = '" & dtMonth & "' AND DatePart("d", [SQLDateField]) = '" & dtDay & "'"

Did that help any?



mmcdonal
Reply With Quote
  #3 (permalink)  
Old October 18th, 2007, 09:09 AM
Authorized User
 
Join Date: Jan 2007
Location: , , United Kingdom.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The data type used on the SQL Server is smallDateTime.

I have been advised by another user to change the format of the Date variable in VB using Format(strDate1,"mm/dd/yy"). This has had no effect. The Select Query I am using to open the recordset is

select vw.intVisitID,vw.FullName,Vw.dtmDateOfVisit From view_AllSchoolVisits As Vw Where (vw.intSchoolId =4) And (vw.DtmDateOfVisit Between 10/02/07 And 10/28/07)

Thank you for your suggestion but I cannot believe that it is so messy to query a date range. So at the moment I am no further forward.

Reply With Quote
  #4 (permalink)  
Old October 18th, 2007, 10:10 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am not sure where you would use: Format(strDate1,"mm/dd/yy")

The format of your SQL Server date is yyyy-mm-dd or yyyy-dd-mm, I am not sure. So you would want to convert the date to the proper format BEFORE passing it to SQL Server. mm and dd preserve leading zeros. SQL Server does not recognize the Format() finction. So that would be:

Dim dtStartDate As Date
Dim dtEndDate As Date

dtStartDate = Me.StartDateField
dtEndDate = Me.EndDateField

dtStartDate = Format(dtStartDate, "yyyy-mm-dd")
dtEndDate = Format(dtEndDate, "yyyy-mm-dd")

sSQL = "SELECT vw.intVisitID,vw.FullName,Vw.dtmDateOfVisit From view_AllSchoolVisits As Vw Where (vw.intSchoolId =" & iSchoolID & ") And (vw.DtmDateOfVisit Between " & dtStartDate & " And " & dtEndDate & ")"

Now that is messy too, but you write code so others don't have to.

Anyway, if there is still a problem, it may be because the Time portion of the DateTime field is causing a problem. Why is the field formatted this way if the time is not being taken? I can understand the date format if it is a string since that sorts them in order.

Anyway, did that help any?

mmcdonal
Reply With Quote
Reply


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
Error - Loading date field from flat file to sql carumuga SQL Server 2005 0 August 12th, 2008 09:55 AM
Using ORDER BY in SQL on date field in ASP page saifi4u Classic ASP Professional 3 March 1st, 2008 07:40 PM
Help with Unbound Date field to link with SQL VBA JackalBW BOOK: Access 2003 VBA Programmer's Reference 0 December 9th, 2007 06:29 AM
Need ONLY date in datetime field in Database Lucy SQL Server 2000 10 April 30th, 2007 09:20 AM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM



All times are GMT -4. The time now is 01:26 AM.


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