Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Professionals
|
Visual Basic 2008 Professionals For advanced Visual Basic coders working in version 2008. Beginning-level questions will be redirected to other forums,
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2009, 02:24 PM
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shaiss Send a message via MSN to shaiss
Angry Problem with WHERE in SQL only in code

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.
 
Old April 8th, 2009, 03:17 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

HUH??? I do *NOT* believe that this query:
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')
Results from the
Debug.Print(SQLqry)
of this code:
Code:
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')"

...
What happened to the text in RED there???

You must be looking at OLD debug output, not the current code.

By the way, that WHERE clause as written will *ONLY* find records where ClockData.Date is between April 1, 2009, Midnight and April 2, 2009, Midnight. Is that what you really wanted???

Last edited by Old Pedant; April 8th, 2009 at 03:17 PM.. Reason: typo
 
Old May 19th, 2009, 07:20 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Go and have a look at the MySQL website they have some good controls you can use!
__________________
Apocolypse2005, I'm a programmer - of sorts.





Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Code Help... Making it harder than needs to be brux2dc SQL Server 2005 5 June 4th, 2007 04:42 PM
T-SQL Code to get last 6 months midway11 SQL Language 1 October 15th, 2006 08:59 AM
asp.net 2 accessing sql database code problem... minstrel ASP.NET 2.0 Basics 1 August 31st, 2006 11:40 PM
VBA code convert to SQL omnicap1 Access VBA 2 August 23rd, 2004 03:03 AM
Beginning SQL, where is source code? docker All Other Wrox Books 1 January 7th, 2004 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.