将MultiSelect ListBox值传递给使用repleast()的查询don' t更新结果

发布于 2025-02-13 03:29:54 字数 1026 浏览 3 评论 0 原文

我有一个带有多选择列表框的连续表单,称为statusListBox,然后将所有选定的值从框中传递给带有替换函数的SQL查询(“ FelterQuery”),更改查询的SQL代码。这效果很好并更新了查询的文本,并且在打开查询时可以看到更改,但是我的连续表格不会更新。我怀疑问题是在表格已经加载后,并且没有适当的参数(即 formsname!formName!control ),

我使用 me.requery 更新表单,该表单与 filterQuery sql代码中的参数非常有用,但在这种情况下不可能。 我是新手,我很困惑。 这是sub:

首先,我从文本框中制作一个字符串,

StatusListValues = ""
For Each StatusItem In StatusLIst.ItemsSelected
StatusListValues = StatusListValues & "'" & StatusLIst.ItemData(StatusItem) & "'" & ","
Next StatusItem
StatusListValues = Left(StatusListValues, Len(StatusListValues) - 1)
Debug.Print StatusListValues

然后我将当前的SQL代码保存为字符串,将所选值的先前字符串替换为当前字符串,然后使当前字符串 “上一个”

SQLstring = CurrentDb().QueryDefs("FilterQuery").SQL
SQLstring = Replace(SQLstring, StatusListValuesPrevious, StatusListValues)
CurrentDb.QueryDefs("FilterQuery").SQL = SQLstring
StatusListValuesPrevious = StatusListValues

I have a continuous form with a multiselect listbox, called StatusListBox, and I pass all the selected values from the box to an SQL query ("FilterQuery") with a Replace function, changing the SQL code of the query. This works well and updates the text of the query and I can see the changes when I open it, but my continuous form doesn't get updated. I suspect the problem is that I change the text of the query's SQL code after the form has already loaded and not with proper parameters (i.e. Forms!FormName!Control)

I use Me.Requery to update the form, which works great with textfields which are parameters in the FilterQuery SQL code, but not in this case.
I'm new to Access and I'm very puzzled.
Here's the sub:

first I make a string with all the values from the textbox

StatusListValues = ""
For Each StatusItem In StatusLIst.ItemsSelected
StatusListValues = StatusListValues & "'" & StatusLIst.ItemData(StatusItem) & "'" & ","
Next StatusItem
StatusListValues = Left(StatusListValues, Len(StatusListValues) - 1)
Debug.Print StatusListValues

then I save the current SQL code as a string, replace the previous string of selected values with current string, then make current string the
"previous" one

SQLstring = CurrentDb().QueryDefs("FilterQuery").SQL
SQLstring = Replace(SQLstring, StatusListValuesPrevious, StatusListValues)
CurrentDb.QueryDefs("FilterQuery").SQL = SQLstring
StatusListValuesPrevious = StatusListValues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2025-02-20 03:29:55

您应该避免编辑现有查询的SQL字符串。相反,您应该根据所选项目过滤表单。

创建一个辅助函数,该函数返回所选状态(如果有),如果'a','b','c'

Private Function SelectedStatus() As String
    Dim StatusListValues As String

    For Each StatusItem In StatusLIst.ItemsSelected
        StatusListValues = StatusListValues & "'" & StatusLIst.ItemData(StatusItem) & "'" & ","
    Next StatusItem

    SelectedStatus= Left(StatusListValues, Len(StatusListValues) - 1)

End Function

。 >列表框的事件,获取所选项目(如果有),然后过滤表单,或者如果没有选择,则清除过滤器。

Private Sub YourListBoxControl_AfterUpdate()

    Dim statuses As String
    statuses = SelectedStatus()

    'if no selection, clear the filter
    If statuses = vbNullString Then
        FilterOn = False
        Filter = vbNullString
        Exit Sub
    End if

    'selection made - apply filter
    Filter = "[Status] In (" & statuses & ")"
    FilterOn = True

End Sub

You should avoid editing the SQL string of an existing query. Instead, you should filter the form based on the selected items.

Create a helper function that returns the selected statuses (if any) if the form of 'a','b','c' etc.

Private Function SelectedStatus() As String
    Dim StatusListValues As String

    For Each StatusItem In StatusLIst.ItemsSelected
        StatusListValues = StatusListValues & "'" & StatusLIst.ItemData(StatusItem) & "'" & ","
    Next StatusItem

    SelectedStatus= Left(StatusListValues, Len(StatusListValues) - 1)

End Function

Then, on the AfterUpdate() event of the ListBox, get the selected items (if any) and either filter the form, or clear the filter if nothing has been selected.

Private Sub YourListBoxControl_AfterUpdate()

    Dim statuses As String
    statuses = SelectedStatus()

    'if no selection, clear the filter
    If statuses = vbNullString Then
        FilterOn = False
        Filter = vbNullString
        Exit Sub
    End if

    'selection made - apply filter
    Filter = "[Status] In (" & statuses & ")"
    FilterOn = True

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文