Access not always returning expected values from SQL statement in Excel

Posted on 2011-05-12
Medium Priority
Last Modified: 2012-08-14
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

Question by:simonwait
  • 2
  • 2
  • 2
  • +1
LVL 44

Expert Comment

ID: 35750307
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!
LVL 35

Accepted Solution

Norie earned 1000 total points
ID: 35750440
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
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 1000 total points
ID: 35750573
Are you passing the dates in US format? Access expects them that way.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35760083
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
LVL 85

Expert Comment

by:Rory Archibald
ID: 35761122

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

Expert Comment

ID: 35762001
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


Author Closing Comment

ID: 35770397
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question