Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probem with WHERE only in code

Status
Not open for further replies.

shaiss

IS-IT--Management
Mar 26, 2004
16
0
0
US
Hi Everyone, Thanks ahead of time for the help.

My Code:
Code:
    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     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:
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.
 

Are you sure you're copying the correct SQL from the immediate window? Your code has this:

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')"

but the SQL you copy is this:

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')

Note that the WHERE clause in the second SQL - the one you ran in the query designer - does not include (ClockData.[Date] <= #4/2/2009#) AND (ClockData.[Date] >= #4/1/2009#)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Access databases use # to surround dates. MSSQL uses quotes. Something along the lines of:
(ClockData.[Date] <= '2009-04-02') and (ClockData.[Date] >= '2009-04-01')
 
Just a suggestion that you should look toward datasets and datareaders, and move away from the recordsets BOF/EOF. It is faster , cleaner and more robust.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top