有人可以帮助我解决这个问题,我是编程的初学者。
有两个组合框是S.Y.(学年)和Sem(学期),我想使用这两个组合框在下面的列表视图中获得更多特定数据。
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
此代码仅使用sem组合框,即cboSearchby,所以我现在需要知道的是如何使组合框S.Y也能运行,如果还使用该texbox搜索名字和姓氏。
答案 0 :(得分:1)
您只需在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
然后如果你想添加搜索lastname和firstname,只需在IF语句中添加另一个条件。
注意:在使用逻辑运算符时,如果所有条件都为真,则 AND 为真,而如果至少其中一个条件为真,则 OR 为真。