Link to home
Start Free TrialLog in
Avatar of simonwait
simonwaitFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Else
Sheets("Datasheet").Range(c.Name.Name).Value = .Fields(c.Name.Name).Value
End If
skip:
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
Else
Sheets("Datasheet").OLEObjects("Checkbox" & i).Object.Value = False
End If
Next i

.MoveNext
Loop
End With
End With
If newrecord = True Then

Range("TimesBlock").ClearContents
End If
End Sub

Open in new window

Avatar of GRayL
GRayL
Flag of Canada image

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!
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simonwait

ASKER

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
Try

sql2 = "SELECT * FROM Hours WHERE TimesheetDate=#" & format(searchdate, "mm/dd/yyyy") & "# AND
Avatar of Norie
Norie

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

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