Vb6 如何显示所有值
使用VB6 我正在表单中使用复选框和组合框 单击复选框时,combobox将启用,默认情况下combobox将禁用 代码 输出代码为Vb6 如何显示所有值,vb6,Vb6,使用VB6 我正在表单中使用复选框和组合框 单击复选框时,combobox将启用,默认情况下combobox将禁用 代码 输出代码为 If chkbox1.Value = 1 Then sql2 = "Select * from table1 where value = '" & combobox1 & "' " ElseIf chkbox2.Value = 1 Then sql2 = "Select * from table1 where value = '"
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
上面的代码正在工作,但是当我单击两个复选框时,两个combobox被启用,然后我运行查询,它显示combobox1值
比如说
I selected the value = 50 from combobox1 (checkbox1 clicked)
I selected the value = 100 from combobox2 (checkbox2 clicked)
当我运行输出代码时,输出是显示value=50的值,而不是显示value=100
它应该在输出代码中显示这两个值
如何解决这个问题
需要vb6代码帮助吗
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 where写入它应该是什么,然后附加所有必要的和
子句。最后,它将语句组合在一起。如果where子句中没有任何内容,那么您可以从表1中获得Select*
您可以尝试此方法
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
在
If ElseIf
中,如果一个条件满足,则不会执行其余的条件语句。如果要同时获取这两个条件,请尝试使用If语句保留条件chkcombin.Value=1
。您的描述不太清楚。例如,“chkcombin1”是什么?我建议您添加一个表单截图,以便进一步澄清。我有10多个复选框,因此有任何替代代码,或者我必须这样做…?@Gopal那么这实际上完全改变了您应该如何做。我会做一些编辑和建议对不起,我改用了.Net来修复我的错误
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