Report.RecordSource 属性 (Access)

使用 RecordSource 属性可以指定报表的数据源。 读/写 String

语法

表达式RecordSource

表达 一个代表 Report 对象的变量。

备注

RecordSource 属性设置可以是表名称、查询名称或者 SQL 语句。 例如,可以使用下列设置。

设置示例 说明
员工 Employees 表指定为数据源的表名称。
SELECT Orders!OrderDate FROM Orders; 一个 SQL 语句,将 Orders 表上的 OrderDate 字段指定为数据源。 通过将控件的 ControlSource 属性设置为 OrderDate,可以将窗体或报表上的控件绑定到 Orders 表中的 OrderDate 字段。

注意

[!注释] 更改的已打开窗体或报表的记录源会自动对基础数据进行重新查询。 如果在运行时设置窗体的 记录集 属性,将更新该窗体的 记录源 属性。

创建窗体或报表之后,可以通过更改 记录源 属性来更改其数据源。 使用 RecordSource 属性还可以创建可重复使用的窗体或者报表。 例如,可以创建一个包含标准设计的报表,然后复制报表并更改 RecordSource 属性以显示其他表、查询或 SQL 语句中的数据。

示例

以下示例将窗体的 RecordSource 属性设置为 Customers 表。

Forms!frmCustomers.RecordSource = "Customers"

下一个示例将窗体的记录源更改为 Customers 表中的单个记录,具体取决于 在 cmboCompanyName 组合框控件中选择的公司名称。 该组合框的内容由一条 SQL 语句决定,该语句返回客户 ID(在绑定列中)和公司名称。 "CustomerID"的数据类型为"文本"。

Sub cmboCompanyName_AfterUpdate() 
    Dim strNewRecord As String 
    strNewRecord = "SELECT * FROM Customers " _ 
        & " WHERE CustomerID = '" _ 
        & Me!cmboCompanyName.Value & "'" 
    Me.RecordSource = strNewRecord 
End Sub

下面的示例演示如何使用结构化查询语言 (SQL) 语句建立报表的数据源,因为它没有打开。

Private Sub Report_Open(Cancel As Integer)

    On Error GoTo Error_Handler

    Me.Caption = ?My Application?

    DoCmd.OpenForm FormName:=?frmReportSelector_MemberList?, _
    Windowmode:=acDialog

    ?Cancel the report if ?cancel? was selected on the dialog form.

    If Forms!frmReportSelector_MemberList!txtContinue = ?no? Then
        Cancel = True
        GoTo Exit_Procedure
    End If
    Me.RecordSource = ReplaceWhereClause(Me.RecordSource, _
      Forms!frmReportSelector_MemberList!txtWhereClause)

Exit_Procedure:
    Exit Sub

Error_Handler:
    MsgBox Err.Number & ?: ? & Err.Description
    Resume Exit_Procedure
    Resume

End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。