我有一个程序,当点击搜索时,会显示一个产品列表。创建新产品时,会出现一个“活动”复选框,该复选框自动设置为True。用户可以编辑任何产品,如果需要,可以将产品更改为“非活动”。
在搜索表单上,有两个复选框:Active和Inactive。 当程序加载时,总是勾选“活动”复选框。如果用户想要查看非活动产品,则应该能够勾选两者。
当我有活动标记时,列表框会显示所有活动项目,当我有非活动状态时,仅勾选它会显示非活动项目。但是当我同时勾选时,只显示非活动产品。
这是我的代码:
Private Sub cmdProdSearch_Click()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim list_item As ListItem
Dim itm As ListItem
db_file = db_file & "ProductsDatabase1.mdb"
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ProductsDatabase1.mdb;" & _
"Persist Security Info=False" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open
Set rs = conn.Execute("Select * from Export where Product like '%" & _
txtProduct.Text & "%'")
If chkActive.Value = 1 Then
Set rs = conn.Execute("Select * from Export where Inactive = '0'")
Else
Set rs = conn.Execute("Select * from Export where Inactive = '1'")
ListView1.ListItems.Clear
With ListView1
.View = lvwReport
.FullRowSelect = True
Do While Not rs.EOF
Set itm = .FindItem(txtProduct.Text, lvwText, , lvwPartial)
Set list_item = .ListItems.Add(, , rs!Product)
list_item.SubItems(1) = rs!barcode & ""
list_item.SubItems(2) = rs!quantity & ""
list_item.SubItems(3) = rs!Department & ""
list_item.SubItems(4) = rs!Active & ""
list_item.SubItems(5) = rs!Inactive
rs.MoveNext
Loop
End With
End If
End Sub
此外,我使用Access数据库存储产品信息,并使用SQL语句查找信息。
当选中这两个复选框时,是否能够显示机器人活动和非活动项目的任何建议?
答案 0 :(得分:4)
您需要更新逻辑。由于书面ListView1
仅在chkActive.Value <> 1
时更新。
通常情况下,我会创建一个单独的字符串变量来保存我的SQL,然后根据条件构建我的搜索...这里是代码的编辑,它执行此操作并包含不同复选框的逻辑路径。它假设您的其他复选框名为chkInactive
:
Private Sub cmdProdSearch_Click()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim list_item As ListItem
Dim itm As ListItem
dim SQL as string
db_file = db_file & "ProductsDatabase1.mdb"
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ProductsDatabase1.mdb;" & _
"Persist Security Info=False" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open
SQL = "Select * from Export where Product like '%" & txtProduct.Text & "%'"
If chkActive.Value = 1 Then
If chkInactive.Value = 1 Then
'do nothing because criteria is already set.
Else
SQL = SQL & " AND Inactive = '0'"
End If
ElseIf chkInactive.Value = 1 Then
SQL = SQL & " AND Inactive = '1'"
Else
'Do something because neither are checked?
End If
Set rs = conn.Execute(SQL)
ListView1.ListItems.Clear
With ListView1
.View = lvwReport
.FullRowSelect = True
Do While Not rs.EOF
Set itm = .FindItem(txtProduct.Text, lvwText, , lvwPartial)
Set list_item = .ListItems.Add(, , rs!Product)
list_item.SubItems(1) = rs!barcode & ""
list_item.SubItems(2) = rs!quantity & ""
list_item.SubItems(3) = rs!Department & ""
list_item.SubItems(4) = rs!Active & ""
list_item.SubItems(5) = rs!Inactive
rs.MoveNext
Loop
End With
End Sub