Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2007, 05:11 AM
Authorized User
 
Join Date: Jan 2007
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??

 
Old October 18th, 2007, 06:50 AM
Friend of Wrox
 
Join Date: Mar 2004
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
 
Old October 18th, 2007, 09:09 AM
Authorized User
 
Join Date: Jan 2007
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.

 
Old October 18th, 2007, 10:10 AM
Friend of Wrox
 
Join Date: Mar 2004
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





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





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