【问题标题】:In Access form, is there a way to use a search box to populate the form using VBA?在 Access 表单中,有没有办法使用搜索框来使用 VBA 填充表单?
【发布时间】:2019-04-05 21:18:12
【问题描述】:

我有两个解决方案我一直在努力解决这个问题......


尝试 #1

我在 Access 表单中有一个组合框,其中有两个选项,“全部”和“所有样本”。 Sample 是从我的表中选择的记录,这些记录被标记为使用 sample_record_id(文本字段)进行审查,以使用非零正数进行标识。 “所有”和“所有样本”都在我的行源中。我的组合框名为 myFilters。

更新后,此 VBA 运行:

Private Sub myFilters_AfterUpdate()
Debug.Print myFilters.Value
  If myFilters.Value = "All Sample" Then
    Me.FilterOn = True
    DoCmd.ApplyFilter , "sample_record_id <> '0'"
  Else
    Me.FilterOn = False
  End If
End Sub

所有记录都有一个 sample_record_id 条目。

我希望在选择“所有示例”时填充我的示例记录,否则将填充所有记录。 In fact, all records do populate when "All" is selected, but when "All Sample" is selected, a "Enter Paramaters Value" dialog box appears with the text "sample_record_id" with a space for entering text.

有趣的是,当我切换 IF 和 ELSE 时:

If myFilters.Value = "All" Then
    Me.FilterOn = False
Else
    Me.FilterOn = True
    DoCmd.ApplyFilter , "sample_record_id <> '0'"

...选择都没有按预期工作。


尝试 #2

我还尝试了以下 VBA:

Private Sub myFilters_AfterUpdate()
  DoCmd.SetWarnings False
  strSQL = "SELECT * FROM invoice_summary WHERE sample_record_id <> '0';"
  Debug.Print strSQL
  DoCmd.RunSQL strSQL
  DoCmd.SetWarnings True
End Sub

我希望这与之前的代码做同样的事情,但无论我选择哪个选项,都会弹出一个调试错误,说“一个 RunSQL 操作需要一个由 SQL 语句组成的参数。”

strSQL 调试返回:SELECT * FROM invoice_summary WHERE sample_record_id '0';

我试过有没有;

sql 语句在标准查询中起作用。

有没有办法使这些工作中的任何一个工作?

【问题讨论】:

    标签: vba forms ms-access


    【解决方案1】:

    已经为此工作了两天,在我发布后 30 分钟我弄明白了。叹息。

    使用我的第一次尝试,我只需要在我的字段名称周围加上括号:

    Private Sub myFilters_AfterUpdate()
      If myFilters.Value = "All Sample" Then
        Me.FilterOn = True
        DoCmd.ApplyFilter , "[sample_record_id] <> '0'"
      Else
        Me.FilterOn = False
      End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-08
      • 2013-07-24
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多