A right outer join considers all records from the parent table and finds a matching record in the child table. To do this, it starts with the first record of the parent table (in this case the Genders table) and shows each record of the child table (in this case the Persons table) that has a corresponding entry. This means that, in our example, a right outer join would first create a list of the Persons records that have a 1 value for the GenderID column. After the first record, the right outer join moves to the second record, and so on, each time listing the records of the child table that have a corresponding entry for the primary key of the parent table. To create a right outer join, you can replace the TypeOfJoin factor of our formula with RIGHT OUTER JOIN. Here is an example: Private Sub cmdDataSource_Click() Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.DeptCode, " & _ " Departments.Department " & _ "FROM Employees " & _ "RIGHT OUTER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartmentCode.ControlSource = "Departments.DeptCode" txtDepartment.ControlSource = "Department" End Sub This would produce:
Notice that the query result starts with the first record of the parent table, also called the left table, and lists the records of the child table, also called the right table, that have the entry corresponding to that first record. Then it moves to the next value.
Just as done for a SQL statement that involves only one table, you can filter records using any field(s) for one or more of the tables in the statement. To do this, after the join statement, add your WHERE clause with the appropriate expression. Here is an example: Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE Employees.LastName = 'Klein';" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub
As mentioned already, you can use any available column(s) of the tables. Here is an example that uses a field of the parent table: Private Sub cmdUseJoin_Click()
RecordSource = "SELECT Employees.EmplNbr, " & _
" Employees.FirstName, " & _
" Employees.LastName, " & _
" Departments.Department " & _
"FROM Employees " & _
"INNER JOIN Departments " & _
" ON Employees.DeptCode = Departments.DeptCode " & _
"WHERE Departments.Department = 'Human Resources';"
txtEmployeeNumber.ControlSource = "Employees.EmplNbr"
txtFirstName.ControlSource = "FirstName"
txtLastName.ControlSource = "LastName"
txtDepartment.ControlSource = "Department"
End Sub
|
|
|||||
|