[英]How to use many checkbox values in a Query
My goal is to return the results of 5 textboxes into a SQL query, by incorporating the Query string with the variables.我的目标是通过将 Query 字符串与变量合并,将 5 个文本框的结果返回到 SQL 查询中。
How can I get my code to function so that when a checkbox is checked, the value (eg: ID, SC...) is recorded and placed into a Query?如何让我的代码发挥作用,以便在选中复选框时,记录值(例如:ID、SC...)并将其放入查询中? And if a checkbox is not checked, then it is not placed into the query.如果未选中复选框,则不会将其放入查询中。
The 5 checkboxes are as follows: 5个复选框如下:
The code I current have to record whether a textbox is selected, and to place the value (eg: ID, SC, AS...) into a variable is as follows:我当前必须记录是否选择了文本框,并将值(例如:ID、SC、AS...)放入变量中的代码如下:
If (Me.BoxID = False) And (Me.BoxSC = False) And (Me.BoxASSC = False) And (Me.BoxAS = False) And (Me.BoxEH = False) Then
MsgBox "You must check a Fonction Checkbox", 0
Exit Sub
Else
If (Me.BoxID= True) Then IDValue = Chr(34) & "ID" & Chr(34) Else IDValue = """"""
If (Me.BoxSC= True) Then SCValue = Chr(34) & "SC" & Chr(34) Else SCValue = """"""
If (Me.BoxASSC= True) Then ASSCValue = Chr(34) & "ASSC" & Chr(34) Else ASSCValue = """"""
If (Me.BoxAS= True) Then ASValue = Chr(34) & "AS" & Chr(34) Else ASValue = """"""
If (Me.BoxEH= True) Then EHValue = Chr(34) & "EH" & Chr(34) Else EHValue = """"""
End If
fonctionQryString = "(((tblF.f1)=" & IDValue & ") OR " + "((tblF.f1)=" & SCValue & ") OR " + "((tblF.f1)=" & ASSCValue & ") OR " + "(tblF.f1)=" & ASValue & ") OR " + "(tblF.f1)=" & EHValue & ")))"
The fonctionQryString goes into the WHERE section of the SQL Query. fonctionQryString 进入 SQL 查询的 WHERE 部分。
I know that the method I'm using is not efficient, even though it works.我知道我使用的方法效率不高,即使它有效。
My problem is that I don't know how to do this another way.我的问题是我不知道如何以另一种方式做到这一点。 I want my code to function so that when a checkbox is not checked, it doesn't go into the Query string.我希望我的代码能够正常工作,以便在未选中复选框时,它不会进入查询字符串。
Any help would be much appreciated.任何帮助将非常感激。
These two WHERE
clauses should produce equivalent results.这两个WHERE
子句应该产生等效的结果。 Consider switching to the second form.考虑切换到第二种形式。
WHERE tblF.f1 = "ID" OR tblF.f1 = "SC" OR tblF.f1 = "AS"
WHERE tblF.f1 IN ("ID","SC","AS")
Here is a rough and untested code sample to produce a similar WHERE
clause based on my understanding of what you're trying to achieve.这是一个粗略且未经测试的代码示例,它根据我对您要实现的目标的理解来生成类似的WHERE
子句。
Dim fonctionQryString As String
Dim lngLoopNum As Long
Dim strControlName As String
Dim strValueList As String
For lngLoopNum = 1 To 5
Select Case lngLoopNum
Case 1
strControlName = "ID"
Case 2
strControlName = "SC"
Case 3
strControlName = "ASSC"
Case 4
strControlName = "AS"
Case 5
strControlName = "EH"
End Select
If Me.Controls("Box" & strControlName) = True Then
strValueList = strValueList & "," & Chr(34) & _
strControlName & Chr(34)
End If
Next
If Len(strValueList) > 0 Then
fonctionQryString = "tblF.f1 IN (" & Mid(strValueList, 2) & ")"
Else
MsgBox "You must check a Fonction Checkbox"
End If
I assumed you didn't actually want to include the condition, WHERE tblF.f1 = ""
(an empty string).我假设您实际上不想包含条件WHERE tblF.f1 = ""
(空字符串)。 If I guessed wrong, you'll have more work to do, but hopefully this will still point you to something useful.如果我猜错了,您将有更多工作要做,但希望这仍然会为您指出一些有用的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.