2012-09-14 111 views
0

我有一个程序,当点击搜索时,显示一个产品列表。创建新产品时,会出现一个“有效”复选框,该复选框会自动设置为True。用户可以编辑任何产品,并且如果需要,可以将产品更改为“不活动”。有多个复选框勾选VB6?

在搜索表单上,有两个复选框:活动和非活动。 当程序加载时,激活复选框始终打勾。如果用户想要查看不活动的产品,应该可以同时打勾。

在那一刻,我有积极的勾选,列表框将显示所有活动项目,当我有不活动只打勾它显示无效项目。但是,当我打勾时,只显示不活动的产品。

这是我的代码:

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语句查找资料我。

当两个复选框都打勾时,能够显示bot有效和无效项目的任何建议?

回答

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 
+0

为什么你会如果chkInactive.Value = 1,则什么也不做做点什么? – Fred

+0

@Fred如果chkActive和chkInactive都是真的,那么像txtProduct.Text所有记录应当返还,所以没有必要参数添加到提到了无效标志,因为它的价值是不相关的where子句。 –

+0

那么为什么没有如果chkInactive.Value = 0那么SQL = SQL&“AND Inactive ='0'”End If。没有其他条款? – Fred