0

我有一个程序,当单击搜索时,它会显示产品列表。创建新产品时,有一个“活动”复选框,该复选框自动设置为 True。用户可以编辑任何产品,如果需要,可以将产品更改为“非活动”。

在搜索表单上,有两个复选框:活动和非活动。当程序加载时,Active 复选框总是被勾选。如果用户也想查看非活动产品,则应该能够同时勾选。

在我选中活动的那一刻,列表框显示所有活动项目,当我选中非活动时,它只显示非活动项目。但是,当我同时勾选时,只显示非活动产品。

这是我的代码:

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 语句查找信息。

当两个复选框都被勾选时,有什么建议能够显示机器人活动和非活动项目?

4

1 回答 1

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
于 2012-09-14T14:17:10.117 回答