It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Problem with WHERE in SQL only in code

Moderator: dafunkymunky

Problem with WHERE in SQL only in code

Postby shaiss on Wed Apr 08, 2009 6:09 pm

Hi Everyone, Thanks ahead of time for the help.

My Code:
Code: Select all
    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


I can take the debug.print(SQLqry) output
Code: Select all
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')

from the immediate window, put it in the VB query designer and it returns results:
Snippete of results:
Code: Select all
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


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.
shaiss
 
Posts: 1
Joined: Wed Apr 08, 2009 6:06 pm

Who is online

Users browsing this forum: No registered users and 5 guests