【发布时间】:2018-12-30 03:10:17
【问题描述】:
我正在使用 Access 2016 并有一个项目表,其中用户将每个项目的标签存储为用逗号分隔的字符串文本,每个标签都以 # 字符开头。例如:
ItemTag:#Tools、#Perishable、#Kit、#Tool Kit、#Screws、#drill bit
我正在尝试创建一个表单,允许用户从多选列表框中选择一个或多个标签,并让表单显示ItemTag 字段包含用户选择的标签的所有项目记录。我正在尝试在 VBA 中执行此操作,但我似乎赶上了# 字符。我可以创建一个查询并使用:
Like "*[#]drill bit*"
但这在 VBA 中不起作用。这是我目前正在使用的代码:
strsql = "SELECT * FROM [tmpTagSearch]"
With Me
'Filter by Tags selected in the Tags multi-select listbox
Dim varItm As Variant, strIN As String
If .cmbTags.ItemsSelected.count > 0 Then
For Each varItm In .cmbTags.ItemsSelected
If strIN = "" Then
strIN = "'" & .cmbTags.Column(0, varItm) & "'"
Else
strIN = strIN & ",'" & .cmbTags.Column(0, varItm) & "'"
End If
varItm = varItm + 1
Next varItm
If Len(strIN) > 0 And strIN <> "*" Then
strsql = strsql & " WHERE (([InventoryTag]) IN (" & strIN & "))"
Else
strsql = strsql & ")"
End If
Else
' strsql = strsql & ")"
End If
Me.RecordSource = strsql
End With
【问题讨论】:
-
首先分享代码生成的有问题的 SQL 字符串,而不是完整的代码。那应该更容易调试
-
在你的代码中没有Like..,错误消息是什么?