Access not always returning expected values from SQL statement in Excel

Posted on 2011-05-12
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
    LVL 44

    Expert Comment

    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 33

    Accepted Solution

    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
    Are you passing the dates in US format? Access expects them that way.
    LVL 1

    Author Comment

    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

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

    Expert Comment

    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

    LVL 1

    Author Closing Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now