【问题标题】:MS Access: Subform Datasheet - Variable RecordSourcesMS Access:子表单数据表 - 变量 RecordSources
【发布时间】:2017-11-02 22:49:34
【问题描述】:

我有一个子表单数据表,它的 RecordSource 可以是可变的。我的数据库根据用户选择(每个查询的不同列集合)构造一个 SQL 查询。生成的查询旨在成为子窗体上数据表的 RecordSource。 (用户只读视图)

问题:

各种查询在单独运行时会产生所需的结果
将结果查询设置为数据表的 RecordSource 不会产生任何结果(无列/行)

我怀疑我需要将查询的属性插入到子表单中才能看到任何结果(很像菜单条中的“添加现有字段”)。

问题:

有什么建议可以让我摆脱困境吗?

谢谢!

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    从您的子表单对象中删除数据表表单并将源对象属性留空。 创建一个新查询(sql 无关紧要)并将其命名为 qryTemp(或您喜欢的任何名称)。 然后,每当您想为子表单设置源时,请使用此

    CurrentDb.QueryDefs("qryTemp").SQL = "<your new sql here>"
    <yoursubformobject>.SourceObject = "Query.qryTemp".
    

    【讨论】:

      【解决方案2】:

      这是我用来填充子表单的示例:

      Private Sub cmdFind_DisplayName_Click()
          Dim dbs As Database, rstPatient As Recordset
          Dim txtDisplayName, strQuote As String
          strQuote = Chr$(34)
          On Error GoTo ErrorHandler
      
          Me.OrderBy = "DISPLAYNAME"
          Me.OrderByOn = True
      
          Set dbs = CurrentDb
          Set rstPatient = Me.RecordsetClone
          txtDisplayName = Trim(InputBox("Please Enter Patient Name ", "Patient Find By Name"))
          txtDisplayName = UCase(txtDisplayName) & "*"
          If IsNull(txtDisplayName) Then
              MsgBox ("No Patient Name Entered - Please Enter a Valid Patient Name")
          Else
              rstPatient.FindFirst "[DISPLAYNAME] Like " & strQuote & txtDisplayName & strQuote
              If Not (rstPatient.NoMatch) Then
                  Me.Bookmark = rstPatient.Bookmark
                  Me.Refresh
              Else
                  MsgBox ("Patient Not Found - Please Enter a New Patient Name")
              End If
          End If
      
          GoTo Exit_cmdFind_Click
      
          ErrorHandler:
              MsgBox LTrim(RTrim(Me.NAME)) + "." + "Patient Find By Display Name - " + "Error: " + AccessError(Err.Number)
      
          Exit_cmdFind_Click:
            rstPatient.Close
            Set dbs = Nothing
            Set rstPatient = Nothing
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2017-03-30
        相关资源
        最近更新 更多