-1

Trying to do a OleDbCommand to find a row in an excel sheet. Each row has a low number and a high number in different cells. No rows have overlapping numbers. I am not getting any results nor errors. Though I know I am a row's range. 205221 is the sheet name. StartSerial and StopSerial are column headers. Do I need to use the column letter instead?

***Edit: Sample data:

Job Descript StartSerial StopSerial
55555 Job1 44334 44897
55556 Job2 44898 45001
55557 Job3 45002 46001
comm = New OleDbCommand("SELECT * from [205221$] WHERE (StartSerial) <= " & serial & " AND (StopSerial) >= " & serial, conn)
Try
    dr = comm.ExecuteReader
Catch ex As Exception
    MsgBox(ex)
End Try
While dr.Read
    Dim jobNum = dr("Job").ToString
    Dim jobName = dr("Descript").ToString
    TextBox1.AppendText(jobNum & " " & jobName)
End While
stefan_aus_hannover
  • 1,777
  • 12
  • 13

2 Answers2

1

I updated from what @David and @AndrewMorton both supplied. Thank you for the help.

Using cmd = New OleDbCommand("SELECT * from [205221$] WHERE [StartSerial] <= ? AND [StopSerial] >= ?", conn)
   cmd.Parameters.Add("?", OleDbType.Integer).Value = serial
   cmd.Parameters.Add("?", OleDbType.Integer).Value = serial
   Dim reader = cmd.ExecuteReader()
   While reader.Read()
       Dim jobNum = reader("Job").ToString
       Dim jobName = reader("Descript").ToString
       TextBox1.AppendText(jobNum & " " & jobName)
   End While
End Using
stefan_aus_hannover
  • 1,777
  • 12
  • 13
0

Try to do the following:

  1. Remove the parenthesis from the column names. If you want to escape the column names, use brackets.
  2. Try to cast the columns in your WHERE clause
  3. Use a parameterized query

Take a look at this example:

Using cmd = New OleDbCommand("SELECT * from [205221$] WHERE CAST([StartSerial] AS INT) >= @0 AND CAST([StopSerial] AS INT) <= @0", conn)
    cmd.Parameters.Add("@0", OleDbType.Integer).Value = serial

    Dim reader = cmd.ExecuteReader()
    While reader.Read()
        ' do something
    End While
End Using

If serial would equal 44898 then it would return the second record in your example dataset.

David
  • 5,877
  • 3
  • 23
  • 40