Access not always returning expected values from SQL statement in Excel

I have some code which reads the values added to an access db through sql using previous code.  This works well except for when certain dates are selected.  For instance there are records for a weekly timesheet between 07/02/2011 and the present.  Most of the time the correct record is returned and therefore the values appear however there are 4 dates where it jumps after the .eof.  These are 07/02/2011, 07/03/2011, 11/04/2011 and 02/05/2011.

I have tried cutting out the excel element of this and doing an SQL query directly in access and this doesnt return the records for the problem dates either so it appears to be an access issue however the format of the field is the same for all the dates (atleast it looks like it is!)
Sub read()
Dim aconn As New ADODB.Recordset
Dim bconn As New ADODB.Recordset
Dim conn1 As New ADODB.Connection
Dim c As Range
Dim newcol As String
Dim recordcount1 As Long
Dim searchdate As String

newrecord = True

stDB = ActiveWorkbook.Path & "\Timesheets.mdb"
stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & stDB & ";"

With conn1
.Open (stConn) 'Open the connection.

'searchdate = Mid(Sheets("Datasheet").Range("E5").Value, 4, 2) & "\" & Left(Sheets("Datasheet").Range("E5").Value, 2) & "\" & Right(Sheets("Datasheet").Range("E5").Value, 4)
searchdate = Left(Sheets("Datasheet").Range("E5").Value, 2) & "/" & Mid(Sheets("Datasheet").Range("E5").Value, 4, 2) & "/" & Right(Sheets("Datasheet").Range("E5").Value, 4)

searchID = Sheets("Datasheet").Range("O2").Value

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & searchdate & "# AND EmployeeID=" & searchID
a = InputBox("a", , sql2)
With bconn
If .State = adStateOpen Then .Close
.Open sql2, conn1, adOpenStatic, adLockReadOnly

Do Until .EOF
newrecord = False
For Each c In Sheets("Datasheet").Range("TimesBlock")

If Sheets("Datasheet").Range(c.Address).MergeCells Then
GoTo skip
End If
If .Fields(c.Name.Name).Value = "" Then
Sheets("Datasheet").Range(c.Name.Name).Value = 0
Sheets("Datasheet").Range(c.Name.Name).Value = .Fields(c.Name.Name).Value
End If
Next c

For i = 1 To 7
thischkbox = "CheckBox" & i
i2 = Choose(i, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
If .Fields(i2 & "ORide").Value = -1 Then
Sheets("Datasheet").OLEObjects("Checkbox" & i).Object.Value = True
Sheets("Datasheet").OLEObjects("Checkbox" & i).Object.Value = False
End If
Next i

End With
End With
If newrecord = True Then

End If
End Sub

Open in new window

Who is Participating?
NorieConnect With a Mentor VBA ExpertCommented:
What's in cell E5 on Datasheet that you need to use mid/left etc to convert it to a date?

Whatever it is that code won't actually put a date in searchdate, all of those string functions retunr strings, and you've declared searchdate as a string.

Access can be quite picky with dates, it might convert your string to a date but if it even does that it might not convert it to the right date.

You could try DateValue(searchdate) in the query to get a 'real' date
Try putting - Option Explicit - at the top of the module in which you have that code to get a better picture of how poor coding practices can make debugging a nightmare!
Rory ArchibaldConnect With a Mentor Commented:
Are you passing the dates in US format? Access expects them that way.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

simonwaitAuthor Commented:
E5's value is a date in the format dd/mm/yyyy which i have tried various ways of sending dd/mm/yyyy, datevalue(dd/mm/yyyy), datevalue(mm/dd/yyyy) and mm/dd/yyyy into the sql but with no decent result.  I also tried removing the # from the sql string.  It always jumps the .eof.  

Option Explicit didnt show anything obvious except for a couple of missing declerations which are resolved.

Cheers so far
Rory ArchibaldCommented:

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & format(searchdate, "mm/dd/yyyy") & "# AND
NorieVBA ExpertCommented:
How exactly did you try DateValue?

Also did you try just using the value from E5 rather than the concatenation?

If nothing suggested work then it might be worth checking searchID and the data type of the field EmployeeID.

Is it a numeric field?
searchdate = Sheets("Datasheet").Range("E5").Value) 

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & searchdate &  "# AND EmployeeID=" & searchID  

' or

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & searchdate & "# AND EmployeeID=" & searchID  

' or use Format to format as a 'long date' 

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & Format(searchdate, "dd mmmm yyyy") & "# AND EmployeeID=" & searchID

Open in new window

simonwaitAuthor Commented:
I must have been doing something funky because when I went back to it, suddenly it worked with both of your solutions.  Sorry for the wild goose chase
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.