动态构建的标准适用于表单

时间:2018-04-29 21:01:36

标签: sql vba dynamic listbox

我正在尝试构建一个sql语句来应用于表单的记录源;我试图定制一些我在网上找到的代码。此代码附加到命令按钮的onclick事件。但是,代码返回与字段相关的所有记录"纠正措施的类型"我试图分配给变量" TypeList" (以下代码)。有人可以帮我弄清楚为什么代码返回所有值而不是过滤在"类型的纠正措施中选择的值"场?

Dim filtersql1 As String
Dim qrySql As String
Dim EntriesList As String
Dim TypeList As String
Dim VarItem As Variant


For Each VarItem In Me.lstType.ItemsSelected
    TypeList = TypeList & ",'" & Me.lstType.ItemData(VarItem) & "'"
    Next VarItem

If Len(TypeList) = 0 Then
    TypeList = "Like '*'"

Else
    TypeList = Right(TypeList, Len(TypeList) - 1)
    TypeList = "IN(" & TypeList & ")"

End If



qrySql = "SELECT [Journal Voucher].TAS, [Journal Voucher].Category, [Journal Voucher].[Type of Corrective Action], [Journal Voucher].[VOUCHER#], [Journal Voucher].[Treas-GFMS Variance], " & _
"[Journal Voucher].[JV Reversal Amt], [Journal Voucher].[Original Variance Date], [Journal Voucher].[Corrective Action AP], [Journal Voucher].[Consolidated Log], [Journal Voucher].Offset, " & _
"[Journal Voucher].[AP Added to DB], [Journal Voucher].[Date Added to DB], [Journal Voucher].[Ready to Submit], [Journal Voucher].[Date Submitted], [Journal Voucher].[Date of Reversal JV], " & _
"[Journal Voucher].[Fiscal Year] FROM [Journal Voucher] "

filtersql1 = "WHERE [Journal Voucher].TAS= '" & Me.lstEntries.Value & "' AND [Journal Voucher].[Type of Corrective Action] & TypeList;"
qrySql = qrySql & filtersql1

Debug.Print qrySql

DoCmd.OpenForm "Journal Voucher", acFormDS
Forms![Journal Voucher].RecordSource = qrySql

End Sub

1 个答案:

答案 0 :(得分:0)

我认为filtersql1格式不正确,TypeList在" s内而不是在外面。我认为它应该是这样的:

filtersql1 = "WHERE [Journal Voucher].TAS= '" & Me.lstEntries.Value & "' AND [Journal Voucher].[Type of Corrective Action] " & TypeList & ";"