Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I always get the error "In operator without () in query expression" using these codes:
(It works fine for Status and Client when I made everything else into a comment but when I enabled everything I get the error)

VB
'For Status
       Dim dt As DataTable
       Dim ds As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim statList As String = "("
       For item As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
           statList += "'" & CheckedListBox1.CheckedItems(item) & "',"
       Next
       statList = statList.Substring(0, statList.Length - 1) + ")"

       Dim sql = "select * from Status where Stat in " + statList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql, cnn)
           adptr.Fill(ds)
           adptr.Dispose()
           cnn.Close()

           dt = ds.Tables(0)
           Form5.DataGridView1.DataSource = dt

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True


       'For Client
       Dim dt1 As DataTable
       Dim ds1 As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim clientList As String = "("
       For item1 As Integer = 0 To CheckedListBox2.CheckedItems.Count - 1
           clientList += "'" & CheckedListBox2.CheckedItems(item1) & "',"
       Next
       clientList = clientList.Substring(0, clientList.Length - 1) + ")"

       Dim sql1 = "select * from NTIdb where Client in " + clientList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql1, cnn)
           adptr.Fill(ds1)
           adptr.Dispose()
           cnn.Close()

           dt1 = ds1.Tables(0)
           Form5.DataGridView1.DataSource = dt1

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True

       'For Usage
       Dim dt2 As DataTable
       Dim ds2 As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim usageList As String = "("
       For item2 As Integer = 0 To CheckedListBox3.CheckedItems.Count - 1
           usageList += "'" & CheckedListBox3.CheckedItems(item2) & "',"
       Next
       usageList = usageList.Substring(0, usageList.Length - 1) + ")"

       Dim sql2 = "select * from NTIdb where ProdUsage in " + usageList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql2, cnn)
           adptr.Fill(ds2)
           adptr.Dispose()
           cnn.Close()

           dt2 = ds2.Tables(0)
           Form5.DataGridView1.DataSource = dt2

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True


       'For Product Category
       Dim dt3 As DataTable
       Dim ds3 As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim categoryList As String = "("
       For item3 As Integer = 0 To CheckedListBox4.CheckedItems.Count - 1
           categoryList += "'" & CheckedListBox4.CheckedItems(item3) & "',"
       Next
       categoryList = categoryList.Substring(0, categoryList.Length - 1) + ")"

       Dim sql3 = "select * from NTIdb where ProdCategory in " + categoryList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql3, cnn)
           adptr.Fill(ds3)
           adptr.Dispose()
           cnn.Close()

           dt3 = ds3.Tables(0)
           Form5.DataGridView1.DataSource = dt3

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True


       'For Brand
       Dim dt4 As DataTable
       Dim ds4 As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim brandList As String = "("
       For item4 As Integer = 0 To CheckedListBox5.CheckedItems.Count - 1
           brandList += "'" & CheckedListBox5.CheckedItems(item4) & "',"
       Next
       brandList = brandList.Substring(0, brandList.Length - 1) + ")"

       Dim sql4 = "select * from NTIdb where Brand in " + brandList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql4, cnn)
           adptr.Fill(ds4)
           adptr.Dispose()
           cnn.Close()

           dt4 = ds4.Tables(0)
           Form5.DataGridView1.DataSource = dt4

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True


       'For Account Executive
       Dim dt5 As DataTable
       Dim ds5 As New DataSet
       cnn = New OleDbConnection(connectionString)
       Dim accList As String = "("
       For item5 As Integer = 0 To CheckedListBox6.CheckedItems.Count - 1
           accList += "'" & CheckedListBox6.CheckedItems(item5) & "',"
       Next
       accList = accList.Substring(0, accList.Length - 1) + ")"

       Dim sql5 = "select * from NTIdb where AccExec in " + accList

       Try
           cnn.Open()
           adptr = New OleDbDataAdapter(sql5, cnn)
           adptr.Fill(ds5)
           adptr.Dispose()
           cnn.Close()

           dt5 = ds5.Tables(0)
           Form5.DataGridView1.DataSource = dt5

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try
       Form5.DataGridView1.Visible = True
Posted
Updated 5-Mar-13 23:14pm
v2
Comments
CHill60 6-Mar-13 8:38am    
It would help if you pointed out which line gives you the error. For starters check all of your sql variables using debug to see what they actually say. Things like statList could be empty

1 solution

The error message tells you exactly what the problem is. One of your CheckedListBoxes is empty or does not have any checked items. It's is creating an SQL statement like this:
Select * from myTable where myField in ()

You need to handle that scenario.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900