如何显示所有值

时间:2011-03-01 18:38:21

标签: vb6

使用VB6

我在表格中使用复选框和组合框。

当我单击复选框时,组合框将启用,默认情况下组合框将被禁用。

代码。

Private Sub chkbox1_Click()
   combobox1.enable = true
End Sub

Private Sub chkbox2_Click()
   combobox2.enable = true
End Sub

输出代码

If chkbox1.Value = 1 Then
    sql2 = "Select * from table1 where value = '" & combobox1 & "' "
ElseIf chkbox2.Value = 1 Then

    sql2 = "Select * from table1 where value = '" & combobox2 & "'"
Else 
    sql2 = "Select * from table1"
End If

上面的代码正常工作,但是当我点击两个复选框时,则启用了两个组合框,然后我运行查询它显示了combobox1值。

例如

I selected the value = 50 from combobox1 (checkbox1 clicked)
I selected the value = 100 from combobox2 (checkbox2 clicked)

当我运行输出代码时,输​​出显示值为value = 50,它也没有显示值= 100。

它应该在输出代码中显示两个值

如何解决这个问题。

需要vb6代码帮助

2 个答案:

答案 0 :(得分:2)

If chkcombin1.Value = 1 And chkcombin2.Value = 0 Then
    sql2 = "Select * from table1 where value = '" & combobox1 & "' "
ElseIf chkcombin2.Value = 1 And chkcombin1.Value = 1 Then

    sql2 = "Select * from table1 where value = '" & combobox2 & "'"
ElseIf chckcombin1.Value = And chkcombin2.Value = 1
    sql2 = "Select * from table 1 Where value = '" & combobox1 & "' and value = '" & combobox2 & "'"
Else
    sql2 = "Select * from table1"
End If

发生的事情是,如果两者都被检查,则第一种情况属实,因为chkcombin1.Value = 1 因此,您需要检查以确保未选中其他框。

编辑操作评论

由于你有多个复选框,我建议你这样做:

这不是经过测试,也不是最佳的,但应该给你一个想法

Dim select As String = "Select * from table 1"

'This needs to be a field for the whole class
Dim where As String = ""

If chkcombin1.Value = 1 Then
   where += CreateCaluse(combobox1)
End If

'Then do that for each of your comboboxes

'Then
sql2 = select + where

Private Function CreateClause(ByVal comboboxValue As String) As String
    If where = "" Then
        Return " Where value = '" & comboboxValue & "'"
    Else
        Return " and value = '" & comboboxValue & "'"
    End If
End Function

那么它的作用是写下你的语句中没有复选框的部分,然后它准备where子句,使用一个函数来生成必要的部分,如果where子句是一个空字符串,它会写出它应该为1的内容在哪里,然后附加所有必要的and条款。最后,它将声明结合在一起。如果where子句中没有任何内容,那么您将获得Select * from table1

答案 1 :(得分:0)

你可以试试这个

sql2 = "SELECT * FROM table1 WHERE 0=1"
If chkbox1.Value = vbChecked Then
    sql2 = sql2 & " OR value = '" & Replace(combobox1.Text, "'", "''") & "'"
End If
If chkbox2.Value = vbChecked Then
    sql2 = sql2 & " OR value = '" & Replace(combobox2.Text, "'", "''") & "'"
End If
If chkbox3.Value = vbChecked Then
    ...

或者,如果您使用控制数组,代码将大大减少

sql2 = "SELECT * FROM table1 WHERE 0=1"
For i = 1 To 10
    If chkbox(i).Value = vbChecked Then
        sql2 = sql2 & " OR value = '" & Replace(ComboBox(i).Text, "'", "''") & "'"
    End If
Next