从多个多选列表框构建 SQL 时遇到困难
我有一个表单,其中大约有 6 个多选列表框。列表框由不同的表填充。我可以将列表框中的选择存储在变量中,然后在 SQL 查询中使用这些变量。该变量存储从 listBox 中选择的项目,如下所示:
If clientList.SelCount > 0 Then
For i = 0 To clientList.ListCount - 1
If clientList.Selected(i) Then
If Len(criteria_cl) = 0 Then
criteria_cl = Chr(39) & clientList.List(i) & Chr(39)
Else
criteria_cl = criteria_cl & "," & Chr(39) & clientList.List(i) & Chr(39)
End If
End If
Next
Else
End If
SQL 查询如下,带有更多称为 criteria_xx 的变量,
strsql = "select * from pmt_hist_dmart_step2 where dbr_portfolio in (" & criteria_pf & ") and DBR_CLIENT in (" & criteria_cl & _
") and DBR_ACCT_TYPE in (" & criteria_ac & ")..... ;"
如何处理用户未进行选择的情况。目前,criteria_xx 变量变为空白,并且 IN 子句包含 ('') 并引发错误。
请提供一些关于如何克服这个问题的建议?如果只有两个或三个列表框,我会编写不同的查询,但这是六个多选列表框,我不知道。
提前致谢。
I have a form in which there are around 6 multiselect listBoxes. the listBoxes are populated from different tables. I have no problem in storing the selections from listBoxes in variables and then using the variables in a SQL query. the variable stores the selected item from listBox as follows:
If clientList.SelCount > 0 Then
For i = 0 To clientList.ListCount - 1
If clientList.Selected(i) Then
If Len(criteria_cl) = 0 Then
criteria_cl = Chr(39) & clientList.List(i) & Chr(39)
Else
criteria_cl = criteria_cl & "," & Chr(39) & clientList.List(i) & Chr(39)
End If
End If
Next
Else
End If
the SQL query is as follows with more variables which are called criteria_xx
strsql = "select * from pmt_hist_dmart_step2 where dbr_portfolio in (" & criteria_pf & ") and DBR_CLIENT in (" & criteria_cl & _
") and DBR_ACCT_TYPE in (" & criteria_ac & ")..... ;"
How do I deal with a situation when the user makes no selection. Currently, the criteria_xx variable becomes blank and the IN clause contains ('') and throws an error.
Please provide some advice on how to overcome this? If it were only two or three listBoxes, I would written different queries but this is six multiselect listBoxes and I have no clue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种方法:
这里的想法是,仅当
...
非空白时才应包含dbr_portfolio in (...)
形式的每个子句。iif
是一个带有三个参数的内置函数;如果第一个参数为 true,则返回第二个参数;如果第一个参数为 false,则返回第三个参数。例如,iif(1 = 1, 5, 10)
返回5
,iif(1 = 2, 5, 10)
返回 <代码>10。 (注意,第二个和第三个参数始终都会被求值,即使其中一个随后被忽略。)由于像and 不是有效的 SQL,因此我添加了最终的1 = 1 子句,该子句没有实际效果。
where and
或and and
这样的东西不是有效的 SQL,我将每个and
包含在上一个子句的iif
控制代码中。然后,由于最终的(顺便说一句,抱歉,如果我的缩进方案很难看;自从我上次编写 VB6 以来已经有十几年了,所以我真的不记得 VB6 代码通常是如何格式化的。)
Here's one approach:
The idea here is that each clause of the form
dbr_portfolio in (...)
should only be included if the...
is non-blank.iif
is a built-in function that takes three arguments; if its first argument istrue
, then it returns its second argument, and if its first argument isfalse
, then it returns its third argument. For example,iif(1 = 1, 5, 10)
returns5
, andiif(1 = 2, 5, 10)
returns10
. (N.B. The second and third arguments are both always evaluated, even though one of them is ignored afterward.)Since something like
where and
orand and
would not be valid SQL, I include eachand
within theiif
-controlled code of the previous clause. Then, since a finaland
would not be valid SQL, I tacked on a final1 = 1
clause that has no actual effect.(By the way, sorry if my indentation scheme is ugly; it's been over a dozen years since I last wrote VB6, so I really don't remember how VB6 code is usually formatted.)