0

我正在对多选列表框中选择的字段运行“选择不同”查询(列表框填充有表中的字段)。代码在我进行多选之前运行良好,现在 SQL 查询不起作用。例如,当我从字段列表中选择字段“性别”和“界面”时,第二个列表框中的结果是:

选择不同的性别界面

理想情况下,我的结果将包括字段名称,然后是值,每个字段结果之间有一条线。

这是我的代码:

Dim strSQL As String
Dim strCriteria As String
Dim varItem As Variant

On Error GoTo Err_Command206_Click

For Each varItem In Me.ScrubbedList.ItemsSelected
strCriteria = strCriteria & ",[" & Me!ScrubbedList.ItemData(varItem) & "]"

Next varItem

strSQL = "SELECT DISTINCT " & Mid(strCriteria, 2) & " FROM Scrubbed"

'====== Testing
Debug.Print strSQL
'=====

Me.List316.RowSource = strSQL

Exit_Command206_Click:
    Exit Sub
Err_Command206_Click:
    MsgBox "Please select a field"
4

1 回答 1

0

您似乎在错误的地方放了一些东西:

Dim strSQL As String
Dim strCriteria As String
Dim varItem As Variant

On Error GoTo Err_Command206_Click

For Each varItem In Me.ScrubbedList.ItemsSelected
    strCriteria = strCriteria & ",[" & Me!ScrubbedList.ItemData(varItem) & "]"

Next varItem


strSQL = "SELECT DISTINCT " & Mid(strCriteria,2) & " FROM Scrubbed"

'====== Testing
Debug.Print strSQL
'======

Me.List316.RowSource = strSQL


Exit_Command206_Click:
    Exit Sub
Err_Command206_Click:
    MsgBox "Please select a field"
于 2012-07-31T14:35:00.483 回答