If you are using a TreeView control to view a database, populating the tree may take some time. In that instance, you can use the ProgressBar control to inform the user of the status of the operation.
In this scenario, the Biblio.mdb database is loaded into the TreeView control using a Do Until loop. Before the loop begins the ProgressBar control is unhidden. Through each iteration of the loop, the Value property of the ProgressBar control is updated with the PercentPosition property of the Recordset object. When the loop finishes, the ProgressBar control is hidden again.
The code examples in this topic are taken from the DataTree.vbp sample application, which is listed in the Samples directory.
The following example uses the following objects:
To show a ProgressBar while a TreeView control is being populated
A progress bar is most effective when it is visible only for the duration of the time-intensive process. To this end, you can use the Form object's Load event to set the Visible property of the control to False.
In this scenario, the Recordset object's PercentPosition property will be used. Since this property returns a number representing a percentage between 0 and 100, the Max property should be set to 100 when the form is initialized. (By default, the Min property is set to 0.) The following code hides the ProgressBar control and sets its Max property to 100.
Private Sub Form_Load()
prgLoad.Visible = False
prgLoad.Max = 100
End Sub
Note The following code builds upon the code found in "TreeView Scenario1: Binding the TreeView to the Biblio.MDB Database." The working code can also be found in the DataTree.vbp sample application.
To populate a TreeView control from a database, you can use a Do Until loop. Schematically, the code might look like this:
' Presuming a Recordset object variable named
' "rsTitles" has been set to a valid database
' table.
Do Until rsTitles.EOF
' Using the current record, create a Node
' object.
Set mNode = TreeView.Nodes.Add()
' Set properties of the Node.
mNode.Text = rsTitles!Fields(1).Value
' Move to the next record.
rsTitles.MoveNext
Loop
To update the ProgressBar control, use the PercentPosition property of the Recordset object. This property returns the position of the current recordset as a percentage of the total number of records. The code to update the ProgressBar should then be placed somewhere within the loop, as shown:
Do Until rsTitles.EOF
' Update the ProgressBar control.
prgLoad.Value = rsTitles.PercentPosition
Set mNode = TreeView.Nodes.Add()
' Set properties of the Node.
mNode.Text = rsTitles!Fields(1).Value
' Move to the next record.
rsTitles.MoveNext
Loop
Once the TreeView control has been entirely loaded with Node objects, you can hide the ProgressBar control, as shown:
Private Sub cmdLoad_Click()
' Show the ProgressBar control.
prgLoad.Visible = True
Do Until rsTitles.EOF
' Update the ProgressBar control.
prgLoad.Value = rsTitles.PercentPosition
Set mNode = TreeView.Nodes.Add()
' Set properties of the Node.
mNode.Text = rsTitles!Fields(1).Value
' Move to the next record.
rsTitles.MoveNext
Loop
' Hide the ProgressBar control.
prgLoad.Visible = False
End Sub