Farbschema: Hell
Sie befinden sich hier: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » Data views » WklTreeView » Bind a tree to a table

Bind a tree to a table

In this tutorial, you create a small table of regions, place a WklTreeView on a form, bind it in the property sheet without writing code, and update a second control when the user selects a node. It takes about fifteen minutes.

Prerequisites

  • Microsoft 365 Access or the Access Runtime, 32-bit or 64-bit, with the WKL Forms Object Library 1.23 or later installed and registered for the current Windows user (How to: Install the released package).
  • A new, blank database in a trusted location (File › Options › Trust Center), so that ActiveX controls and VBA can run in Form view.
  • Every WKL control opens its designer the same way: in Design view, open the property sheet (F4), go to the Other tab, and click [...] in the Custom row (Opening the designer).

Steps

1. Create the sample table

Open the Immediate window (Ctrl+G in the VBA editor) and run these lines one by one:

CurrentDb.Execute "CREATE TABLE tblRegions (RegionID COUNTER PRIMARY KEY, ParentID LONG, RegionName TEXT(50))"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (0, 'United States')"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (1, 'California')"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (2, 'Los Angeles')"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (2, 'San Fransisco')"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (1, 'Texas')"
CurrentDb.Execute "INSERT INTO tblRegions (ParentID, RegionName) VALUES (0, 'Canada')"

ParentID = 0 doesn't refer to any record, so United States and Canada become root nodes. The misspelling „San Fransisco“ is intentional; you fix it in step 5.

2. Insert the control

  1. Create a blank form in Design view.
  2. Choose Form Design › Controls › ActiveX Controls, select WKL TreeView, and click OK.
  3. Draw the control and name it Tree1 (property sheet, Other tab, Name).

Access adds a reference to the WKL Forms Object Library (with the version of the registered release) to the database.

3. Bind the tree

On the Other tab of the property sheet, set:

PropertyValue
RecordSourcetblRegions (value list)
KeyFieldRegionID
ParentFieldParentID
TextFieldRegionName
ExpandLevel2
DefaultImageFolder

Or click [...] in the Custom row to open the designer window. Its Record source page sets the same fields and shows the actual tree in its preview. The designer appears in the same language as the components.

4. Show the selection

Add a text box named txtSelected to the form. In the code window, choose Tree1 in the object list and NodeSelect in the procedure list, and then complete the procedure:

Private Sub Tree1_NodeSelect(ByVal Key As Variant)
    Me.txtSelected = Me!Tree1.Object.FullPath(Key)
End Sub

5. Try it in Form view

Switch to Form view. Click San Fransisco, and then press F2: renaming is turned off, so nothing happens. In Design view, set LabelEdit to Yes, switch back, rename the node to San Francisco, and press Enter. The tree writes the new name to tblRegions right away (AutoSave).

Expected result

  • The tree shows United States and Canada as root nodes; United States is expanded to show California and Texas, and California to show Los Angeles and San Fransisco, each with a folder icon.
  • Selecting San Fransisco shows United States\California\San Fransisco in txtSelected.
  • After the rename, the record in tblRegions contains San Francisco.

Next steps

en/wiki/msaccess/comctl/data-views/treeview/tutorial.txt · Zuletzt geändert: von Wolfgang van der Stille