Problem with WHERE in SQL only in code
Hi Everyone, Thanks ahead of time for the help.
My Code:
[code:gozglk75] Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim Conn As New ADODB.Connection
Dim Rec As New ADODB.Recordset
Dim SQLqry As String
On Error GoTo btnExport_Error
'Build SQL Query
SQLqry = "SELECT ClockData.[Date], Employee.FirstName, Employee.LastName, ClockData.TimeIn, ClockData.TimeOut, ClockData.Comment" & _
" FROM (Employee INNER JOIN ClockData ON ClockData.EmployeeNumber = Employee.EmployeeNumber)" & _
" WHERE (ClockData.[Date] <= #4/2/2009#) AND (ClockData.[Date] >= #4/1/2009#) AND (Employee.FirstName = 'Shai')"
Debug.Print(SQLqry)
'Open connection to database delared on Form1_Activated
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & dbPath & " ;Persist Security Info=True;User ID=admin;Jet OLEDB:Database Password=twinvolcano"
Conn.Open()
Rec.Open(SQLqry, Conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
'Go through records
While Not Rec.EOF
'Do Something with records
Debug.Print(Rec.Fields("FirstName").Value)
'Goto next record in recordset
Rec.MoveNext()
End While
'Close database connections
Rec.Close()
Conn.Close()
Exit Sub
btnExport_Error:
' Describe the error to the user.
MsgBox("Unexpected error" & _
Str$(Err.Number) & _
" in subroutine DoSomething." & _
vbCrLf & _
Err.Description)
Exit Sub
End Sub[/code:gozglk75]
I can take the debug.print(SQLqry) output
[code:gozglk75]SELECT ClockData.[Date], Employee.FirstName, Employee.LastName, ClockData.TimeIn, ClockData.TimeOut, ClockData.Comment FROM (Employee INNER JOIN ClockData ON ClockData.EmployeeNumber = Employee.EmployeeNumber) WHERE (Employee.FirstName = 'Shai')[/code:gozglk75]
from the immediate window, put it in the VB query designer and it returns results:
Snippete of results:
[code:gozglk75]10/14/2008 12:00:00 AM Shai Perednik 10/14/2008 7:23:00 AM 10/14/2008 9:21:00 AM NULL
10/14/2008 12:00:00 AM Shai Perednik 10/14/2008 9:21:00 AM 10/14/2008 10:40:00 AM NULL
10/14/2008 12:00:00 AM Shai Perednik 10/14/2008 10:40:00 AM 10/14/2008 12:48:00 PM NULL
10/14/2008 12:00:00 AM Shai Perednik 10/14/2008 1:40:00 PM 10/14/2008 3:18:02 PM NULL
10/14/2008 12:00:00 AM Shai Perednik 10/14/2008 3:24:51 PM 10/14/2008 5:29:00 PM NULL
10/15/2008 12:00:00 AM Shai Perednik 10/15/2008 7:28:00 AM 10/15/2008 12:20:00 PM NULL
10/15/2008 12:00:00 AM Shai Perednik 10/15/2008 1:05:00 PM 10/15/2008 6:18:00 PM NULL
10/16/2008 12:00:00 AM Shai Perednik 10/16/2008 7:57:00 AM 10/16/2008 12:34:00 PM NULL
10/16/2008 12:00:00 AM Shai Perednik 10/16/2008 1:26:00 PM 10/16/2008 5:12:00 PM NULL
10/17/2008 12:00:00 AM Shai Perednik 10/17/2008 7:53:00 AM 10/17/2008 10:01:04 AM NULL
10/17/2008 12:00:00 AM Shai Perednik 10/17/2008 10:09:11 AM 10/17/2008 12:24:00 PM NULL
10/17/2008 12:00:00 AM Shai Perednik 10/17/2008 2:54:00 PM 10/17/2008 5:03:00 PM NULL
10/20/2008 12:00:00 AM Shai Perednik 10/20/2008 7:41:00 AM 10/20/2008 12:29:00 PM NULL
10/20/2008 12:00:00 AM Shai Perednik 10/20/2008 1:10:00 PM 10/20/2008 5:22:00 PM NULL
10/21/2008 12:00:00 AM Shai Perednik 10/21/2008 7:46:00 AM 10/21/2008 12:46:42 PM NULL[/code:gozglk75]
BUT, in code, watching the locals window for rec.EOF and rec.BOF both return true and therefore no results.
So whats wrong? Why does this work in VB Query Designer but not in code?
If I remove the WHERE statement than the results return fine as expected. But its returning ALL of the results which is no good.