Date problems
I'm trying to get data from a recordset to drop into a spreadsheet. The data needs to fall between today and a specific date (specified from my Input box.
I am having great fun at the moment. The where clause is causing me no end of problems. I put in a date of 03/11/2005 in my input box and it sends records including 27/10/2005 along with any correct records to Excel. I have tried changing the date format to mm/dd/yyyy but it hasn't worked. Here's the code:
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim rst As Recordset
Dim iRow As Integer
Dim dateNOW, dateThen
Dim txtQryString
dateNOW = Date
dateThen = InputBox("Enter the date the report is to run from")
txtQryString = "1"
dateThen = CDate(dateThen)
txtQryString = "=" & CInt(txtQryString)
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = objXL.Workbooks.Open("M:\x")
Set objSht = objWkb.Worksheets("data")
iRow = 10
Set rst = CurrentDb.OpenRecordset("SELECT tblDelegate.Title, tblDelegate.FirstName, tblDelegate.Surname, tblDelegate.PCT, tblDelegate.WorkBase, tblDelegate.JobRole, tblDelegate.LogBookNo, tblCourseTimetable.Date, tblAssessmentDetails.Result, tblCourse.Name, tblAssessmentDetails.Mark, tblcourse.typeid " & _
"FROM (tblCourse INNER JOIN tblCourseTimetable ON tblCourse.CourseID = tblCourseTimetable.CourseID) INNER JOIN (tblAttendanceList INNER JOIN (tblAssessmentDetails INNER JOIN (tblDelegate INNER JOIN tblTestSession ON tblDelegate.DelegateID = tblTestSession.DelegateID) ON tblAssessmentDetails.TestSessionID = tblTestSession.TestSessionId) ON (tblDelegate.DelegateID = tblAttendanceList.DelegateID) AND (tblAttendanceList.DelegateID = tblTestSession.DelegateID) AND (tblAttendanceList.OccurrenceID = tblTestSession.OccurrenceID)) ON tblCourseTimetable.OccurenceID = tblAttendanceList.OccurrenceID " & _
"WHERE tblcourse.typeid " & txtQryString & " and tblCourseTimetable.Date >= " & dateThen & ";")
If rst.EOF = True Then
objWkb.Close (False)
objXL.Quit
MsgBox "There are no records to add", vbOKCancel + vbExclamation, "No Current record"
Else
rst.MoveFirst
End If
Do While Not rst.EOF
objSht.Cells(iRow, 1).Value = rst!Date
objSht.Cells(iRow, 2).Value = rst!WorkBase
objSht.Cells(iRow, 3).Value = rst!Name
objSht.Cells(iRow, 4).Value = rst!FirstName & " " & rst!Surname
objSht.Cells(iRow, 5).Value = rst!JobRole
objSht.Cells(iRow, 6).Value = rst!PCT
objSht.Cells(iRow, 7).Value = rst!Mark
objSht.Cells(iRow, 8).Value = rst!Result
iRow = iRow + 1
rst.MoveNext
Loop
Set objWkb = Nothing
Set objXL = Nothing
rst.Close
Set objSht = Nothing
Am I making an elementary mistake? or is it more sinister :) The tblcourse.date data type is date and the format is Short Date
__________________
Rob Carter
|