如何使用多个组合框来过滤数据

how to use multiple combo boxes to filter data

谁能帮我解决这个问题,我是编程的初学者。

有两个组合框,分别是 S.Y.(学年)和学期(学期),我想使用这两个组合框在下面的列表视图中获得更具体的数据。

Private Sub Search_Record()
    Dim conn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Dim sSQL As String = String.Empty


    Try

        conn = New OleDbConnection(Get_Constring)
        conn.Open()
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        sSQL = "SELECT edp_number, LastName + ', ' + FirstName as name, course as course, Address as address, syear as syear, Sem as sem FROM tblStudent"
        If Me.cboSearchBy.Text = "1st" Then
            sSQL = sSQL & " where Sem like '1st" & Me.txtSearch.Text & "%'"
        Else
            sSQL = sSQL & " where Sem like '2nd" & Me.txtSearch.Text & "%'"
        End If
        cmd.CommandText = sSQL
        da.SelectCommand = cmd
        da.Fill(dt)

        Me.dtgResult.DataSource = dt
        If dt.Rows.Count = 0 Then
            MsgBox("No record found!")
        End If

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        conn.Close()
    End Try
End Sub

此代码仅使用 cboSearchby 的 sem 组合框,所以现在我需要知道的是如何使组合框 S.Y 也能正常工作,以及是否还使用该 texbox 来搜索名字和姓氏。

您只需在 If 语句中添加另一个条件:

 If Me.cboSearchBy.Text = "1st"  and Me.cboSY.Text = "2014-2015" Then
    sSQL = sSQL & " where Sem like '1st" & Me.txtSearch.Text & "%' and SY like '" & Me.cboSY.Text & "%' "
 Else
    sSQL = sSQL & " where Sem like '2nd" & Me.txtSearch.Text & "%' and SY like '" & Me.cboSY.Text & "%' "
 End If

那么如果你想添加对姓氏和名字的搜索,只需在 IF 语句中添加另一个条件。

注意:在使用逻辑运算符时,如果所有条件都为真,则 AND 为真,而如果至少有一个条件为真,则 OR 为真条件为真。