Instead of showing only records that have entries in the child table, you may want your statement to include all records, including those that are null. To get this result, you would create an outer join. You have three options.
A left outer join produces all records of the child table, also called the right table. The records of the child table that don't have an entry in the foreign key column are marked as NULL. To create a left outer join, you can replace the TypeOfJoin factor of our formula with LEFT JOIN or LEFT OUTER JOIN. Here is an example: Private Sub cmdDataSource_Click() RecordSource = "SELECT Persons.PersonID, " & _ " Persons.FirstName, " & _ " Persons.LastName, " & _ " Genders.GenderID, " & _ " Genders.Gender " & _ "FROM Persons " & _ "LEFT OUTER JOIN Genders " & _ "ON Persons.GenderID = Genders.GenderID" txtPersonID.ControlSource = "PersonID" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtGendersGenderID.ControlSource = "GenderID" txtGender.ControlSource = "Gender" End Sub This would produce: Notice that the result includes all records of the Persons (also called the right) table and the records that don't have an entry in the GenderID column of the Persons (the right) table are left empty. To create a left outer join in the Data In Table window, after establishing a relationship between both tables, you can right-click the line that joins them and click Join Properties: In the Join Properties dialog box, read and click the 2 radio button and click OK: This would change the join into a left join.
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() RecordSource = "SELECT Persons.PersonID, " & _ " Persons.FirstName, " & _ " Persons.LastName, " & _ " Genders.GenderID, " & _ " Genders.Gender " & _ "FROM Persons " & _ "RIGHT OUTER JOIN Genders " & _ "ON Persons.GenderID = Genders.GenderID" txtPersonID.ControlSource = "PersonID" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtGendersGenderID.ControlSource = "GenderID" txtGender.ControlSource = "Gender" End Sub This would produce: Notice that the query result starts with the first record of the parent table, also called the left table (in this case the Genders table), and lists the records of the child table, also called the right table (in this case the Persons table), that have the entry corresponding to that first record. Then it moves to the next GenderID value. To create a right outer join in the Query window, after establishing a relationship between both tables, you can right-click the line that joins them and click Join Properties. Then, in the Join Properties dialog box, click the 3 radio button: And click OK.
As demonstrated so far and in previous lessons, the main reason for creating queries is to isolate records. This is done using conditions and criteria. Joins enhance this capability because they allow you to consider records from different tables and include them in a common SQL statement. In the joins we have created so far, we considered all records and let the database engine list them using only the rules of joins built-in the SQL. To make such a list more useful or restrictive, you can pose your own conditions that should be respected to isolate records, like a funnel. As done in previous sections about queries, to include a criterion in a SELECT statement, you can create a WHERE clause.
To create a criterion in a query, first select a column to display it in the lower section. To specify a criterion, in the Criteria box corresponding to the column, type the condition. Here is an example: After specifying the criteria, the database engine would automatically include it in the SQL statement: And you can use it as a record source for a report or a form. Here is an example: Private Sub cmdDataSource_Click() RecordSource = "SELECT Persons.FirstName, " & _ " Persons.LastName, " & _ " Genders.Gender " & _ "FROM Persons " & _ "INNER JOIN Genders " & _ "ON Persons.GenderID = Genders.GenderID " & _ "WHERE Genders.Gender = 'Female';" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtGender.ControlSource = "Gender" End Sub Notice that only the Persons records with a Female entry display. |
|
|||||||||||||||||
|