Partager via


Visual Basic Concepts

StatusBar Scenario: Show Various Properties of a Database

When editing records of a database, a possible application of the StatusBar control would be to inform the user of various properties such as the name of the table being edited, its creation date, and the date of the last update.

The code below uses the following objects:

  • Form named "frmDataviewer"

  • StatusBar Control named "sbrData"

  • Data control named "datData"

To add a StatusBar that shows database properties

  1. Use the Add method to create a collection of Panel objects.

  2. Configure each Panel object with the AutoSize property.

  3. Use the Panel object's Text property to display database properties.

  4. Reset the properties using the Select Case statement in the PanelClick event.

Use the Add Method to Create a Collection of Panel Objects

To create a collection of Panel objects at run time, use the Add method. First, declare a variable as type Panel. As you add each Panel object, the variable will contain the reference to the newly created object. The code below uses the Form object's Load event to create three Panel objects.

Private Sub Form_Load()
   Dim pnlX As Panel
   Dim i As Integer
   For i = 1 to 3 ' First panel already exists.
      Set pnlX = sbrData.Panels.Add()
   Next i
End Sub

Note that after adding three Panel objects to the collection, there are really four panels on the control because the control creates one by default.

Configure Each Panel Object with the AutoSize Property

One of the features of the StatusBar control is the ability of panels to automatically resize according to their contents. The example iterates through all the Panel objects, and sets the AutoSize property for each to sbrSpring (1). With this setting, each panel "springs" to share the total width of the control.

Private Sub Form_Load()
   Dim pnlX As Panel
   Dim i As Integer
   For i = 1 to 3 ' First panel already exists.
      Set pnlX = sbrData.Panels.Add()
   Next i

   ' Change the AutoSize of all panels.
   For i = 1 to 4                ' < -- New code
      sbrData.Panels(i).AutoSize = sbrSpring ' New
   Next i                               ' New
End Sub

Use the Panel Object's Text Property to Display Database Properties

To change the information displayed in any panel, set the Text property of the Panel object. The code below displays information about a database that has been opened with Data Access Objects.

In the Form object's Load event, database variables are first created and assigned to an open database ("Biblio.mdb") and a recordset ("Authors"). The code then assigns the Name, DateCreated, LastUpdated, and LockEdit properties to each Panel object's Text property.

' Declare database variables.
Dim myDB As Database, myRs As Recordset
' Set the Database to the BIBLIO.MDB database.
Set myDB = DBEngine.Workspaces(0). _
OpenDatabase("BIBLIO.MDB")
' Set the recordset variable to the Authors table.
Set myRs = _
myDB.OpenRecordset("Publishers", dbOpenTable)
' Set Text properties with recordset properties.
sbrData.Panels(1).Text = "name: " & myRs.Name
sbrData.Panels(2).Text = "Date Created: " & _
myRs.DateCreated
sbrData.Panels(3).Text = "Last Updated " & _
myRs.LastUpdated
sbrData.Panels(4).Text = "Lockedits: " & myRs.LockEdits

Reset Properties Using the Select Case Statement in the PanelClick Event

The StatusBar control can also be used to reset the properties that are being displayed. In the present scenario, a DataGrid control is bound to the Data control. For more information on how to data-bind controls, see "Using the ADO Data Control" in "Using Visual Basic's Standard Controls" of the Programmer's Guide. Among the properties displayed in the StatusBar, only the LockEdits property can be reset. To accomplish this, use a Select Case statement within the PanelClick event to determine which Panel object was clicked. The PanelClick event contains a reference to the Panel that the user clicked. Using this reference, you can reset the Text property of the clicked Panel object.

The code below first creates a variable of type Recordset and sets it to the recordset opened with the Data control. The Select Case statement is then used to test the Panel object's Index property. If the Index is 4, the LockEdits property is toggled between -1 (True) and 0 (False). Finally, the Panel object's Text property is updated with the new information.

Private Sub sbrData_PanelClick(ByVal Panel As Panel)
   Dim myRs As Recordset ' Declare Recordset variable.
   ' The Data control is named "datData"
   Set myRs = datData.Recordset ' Set variable.

   Select Case Panel.Index
   Case 1 to 3
      ' Can't set these panels.
   Case 4 ' Updateable Property is settable.
      ' Toggle the property.
      myRs.LockEdits = Abs(myRs.LockEdits) - 1
      ' Update the Panel object's Text property.
      sbrData.Panels(4).Text = "LockEdits: " _
      & myRs.LockEdits
   End Select
End Sub