Applies To Form, Report.
Description
You can use the RecordSource property to specify the source of the data for a form or report. You can display data from a table, query, or SQL statement. For example, to display and edit data from the Employees table in a form, you set the form's RecordSource property to Employees. Then you can bind controls on the form or report to specific fields in the Employees table by setting the control's ControlSource property to the name of a field in the table. For example, you can bind a control to the LastName field in the Employees table by setting the control's ControlSource property to LastName.
Setting
The RecordSource property setting can be a table name, a query name, or an SQL statement. For example, you can use the following settings.
Sample Setting | Description |
Employees | A table name specifying the Employees table as the source of data. |
SELECT Orders!OrderDate FROM Orders; | An SQL statement specifying the OrderDate field on the Orders table as the source of data. You can bind a control on the form or report to the OrderDate field in the Orders table by setting the control's ControlSource property to OrderDate. |
See Also ControlSource property, RowSourceType, RowSource properties.
Example The following example sets a form's RecordSource property to the Customers table:Forms!frmCustomers.RecordSource = "Customers"
The next example changes a form's record source to a single record in the Customers table, depending on the company name selected in the cmboCompanyName combo box control. The combo box is filled by an SQL statement that returns the customer ID (in the bound column) and the company name. The CustomerID has a Text data type.
Sub cmboCompanyName_AfterUpdate()
Dim strNewRecord As String
strNewRecord = "SELECT * FROM Customers " _
& " WHERE CustomerID = '" _
& Me!cmboCompanyName.Value & "'"
Me.RecordSource = strNewRecord
End Sub