Results 1 to 8 of 8

Thread: SQL Select doesnt obey condition

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    SQL Select doesnt obey condition

    The following lines are supposed to display to a text box the phones numbers that match the condition in the sql statement, however it shows me all the phone numbers:

    Code:
    Dim SQL As String = "SELECT *, Phone FROM TblContacts WHERE '" & Event_query & Condition & Abs_value & "'"
            Dim myOleDbCommand As New OleDb.OleDbCommand(SQL, con)
    
            con.Open()
    
            Using myDataReader As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader()
                If myDataReader.Read() Then
                    While myDataReader.Read()
                        ' txtPhone.Text = 
                        numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
                        notfound = 0
    
                    End While
    I feel like this line is the problem but I am not sure if I am missing something:

    Code:
    numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
    Help would be much appreciated

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: SQL Select doesnt obey condition

    this would work:

    vb Code:
    1. Dim SQL As String = "SELECT * FROM TblContacts WHERE Phone = '" & Value & "'"

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SQL Select doesnt obey condition

    What's the values of Event_query, Condition, Abs_value variables?
    What's the value of the resulting query string?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL Select doesnt obey condition

    those are values from combo boxes. I am trying to make an application that allows the user to build their own queries from drop boxes. it works except it shows me all the phone numbers after the first one has met the condition, rather than just the ones that meet the condition

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: SQL Select doesnt obey condition

    that's why stanav asked you:

    What's the value of the resulting query string?
    because it's obviously wrong

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Select doesnt obey condition

    Oh, I can tell you exactly what's wrong... your "query" is being treated as literal text....
    Code:
    Dim SQL As String = "SELECT *, Phone FROM TblContacts WHERE '" & Event_query & Condition & Abs_value & "'"
    The ' in SQL denotes a string....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL Select doesnt obey condition

    Yeah cheers, removing the '' around the statement almost got it working perfectly. It seems to miss out the first one always that meets the criteria, but then displays all the rest that meet the critereia?

    Code:
    Dim SQL As String = "SELECT Phone FROM TblContacts WHERE " & Event_query & Condition & Abs_value
            Dim myOleDbCommand As New OleDb.OleDbCommand(SQL, con)
    
            con.Open()
    
            Using myDataReader As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader()
                If myDataReader.Read() Then
                    While myDataReader.Read()
                        ' txtPhone.Text = 
                        numbers.Text += myDataReader.Item("Phone").ToString + vbCrLf
                        notfound = 0
    
                    End While
    Any ideas why that may be? P.S I also remove the *, after SELECT but that didnt fix it

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Select doesnt obey condition

    Code:
    If myDataReader.Read() Then
                    While myDataReader.Read()
    You're calling .Read twice ... the first one loads up the first row... but before you do anything with it, you're loading up the second row with the second read...

    Try this:

    Code:
    If myDataReader.HasRows() Then
                    While myDataReader.Read()
    .HasRows is a function that returns True/False indicating... yup, if the reader has any rows to read...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width