Results 1 to 8 of 8

Thread: Like statement not working [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68

    Like statement not working [resolved]

    i've done some searching and i know my SQL is right, but i'm getting strange results, has something changed with the LIKE keyword when using OLE? heres what i got

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         LB.Items.Clear()
    3.         Dim strConn As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source ="
    4.         strConn += Request.PhysicalApplicationPath & "CalTime.mdb"
    5.         Dim strSQL As String = "Select * from CalTime where( '7am' Like " & "'" & "%" & txtCriteria.Text & "%" & "'" & ");"
    6.         Dim conn As New OleDbConnection(strConn)
    7.         Dim Cmd As New OleDbCommand(strSQL, conn)
    8.         Dim objDR As OleDbDataReader
    9.  
    10.         If DDEmp.SelectedItem.Text <> " - All - " Then
    11.             strSQL += " and empName = '" & DDEmp.SelectedItem.Text & "'"
    12.         End If
    13.        ' msgErr.Text = strSQL
    14.  
    15.         Dim sName As String
    16.         Try
    17.             conn.Open()
    18.             objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    19.             Do While objDR.Read()
    20.                 sName = objDR("empName") & " ,  " & objDR("calDate") & ""
    21.                 LB.Items.Add(New ListItem(sName))
    22.             Loop
    23.             conn.Close()
    24.         Catch ex As OleDb.OleDbException
    25.             msgErr.Text += (ex.Message)
    26.         End Try
    27.  
    28.     End Sub

    i dont catch any exceptions, if i have txtCriteria blank then i get the right results, when i add an A i should get 3 results but i get none, i added a intCounter in there to see if i was getting the results but not loading them right into ListBox, but its just not grabbing anything, any ideas?
    Last edited by NeonBurner; Feb 3rd, 2004 at 12:59 PM.

  2. #2
    Lively Member
    Join Date
    Sep 2002
    Posts
    66
    I thinks its the () and ; in the string when you first declare the variable. if you were to select an option from the list box, you would get an error.

    Try this instead
    VB Code:
    1. Dim strSQL As String = "Select * from CalTime where [7am] Like '%" & txtCriteria.Text & "%'"
    Jason Meckley
    Database Analyst
    WITF

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    that worked, i had took out the () so it had to be the brackets, is it because my field started with a number? because without the ticks around 7am it would give me syntax error, anyways thanks

  4. #4
    Lively Member
    Join Date
    Sep 2002
    Posts
    66
    by putting single quotes around text the query interprets that as text, not a column name so '7am' would literally be 7am, not the column 7am.

    It's also good practice to refrain from using numbers, spaces and special charaters in field names. If you do you will probally need brackets
    Jason Meckley
    Database Analyst
    WITF

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    while i got ya >=), now that is working but when i change by dropdown box to list a certain employee, its not filtering out, i tried the [empName] and still not working its grabbing all results

  6. #6
    Lively Member
    Join Date
    Sep 2002
    Posts
    66
    this would be an issue with the coding not the sql statement. If it's grabbing all results then your if statement is always interpreted as false and never appending the addtional criteria.

    If the " - All - " option is always the first option in your list then use this logic
    VB Code:
    1. If Not DDEmp.SelectedIndex = 0 Then strSQL += " and empName = '" & DDEmp.SelectedItem.Text & "'"

    If it's always the last one then the code would look like this
    VB Code:
    1. If Not DDEmp.SelectedIndex = DDEmp.Items.Count - 1 Then strSQL += " and empName = '" & DDEmp.SelectedItem.Text & "'"
    Jason Meckley
    Database Analyst
    WITF

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    its default is " - All - " and i'm getting the right SQL statement, but still not getting results
    heres my SQL

    VB Code:
    1. Select * from CalTime where [7am] Like '%%' and [empName] = 'Kris test'

    but its grabbing all the empNames not just Kris test? i've tried (), [], everything i can think of I've never had so much trouble on a simple SQL statement

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    US
    Posts
    68
    never mind i'm dimmed my OLECommand to soon, thanx for all your help

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