LEFT JOIN, RIGHT JOIN Operations Example (DAO)

Microsoft Jet SQL Reference

LEFT JOIN, RIGHT JOIN Operations Example

This example assumes the existence of hypothetical Department Name and Department ID fields in an Employees table. Note that these fields do not actually exist in the Northwind database Employees table.

This example selects all departments, including those without employees.

This example calls the EnumFields procedure, which you can find in the SELECT statement example.

Sub LeftRightJoinX()

    Dim dbs As Database, rst As Recordset

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

    

    ' Select all departments, including those

    ' without employees.

    Set rst = dbs.OpenRecordset _

        ("SELECT [Department Name], " _

        & "FirstName & Chr(32) & LastName AS Name " _

        & "FROM Departments LEFT JOIN Employees " _

        & "ON Departments.[Department ID] = " _

        & "Employees.[Department ID] " _

        & "ORDER BY [Department Name];")

    

    ' Populate the Recordset.

    rst.MoveLast

    

    ' Call EnumFields to print the contents of the

    ' Recordset. Pass the Recordset object and desired

    ' field width.

    EnumFields rst, 20

    dbs.Close

End Sub