How It Works

Using the AddItem method, you added the same values to the combo box and the list box so you could see the differences between the two controls side by side.

Private Sub Form_Load()

'populate the combo box with values from a 'specified list With cboAccounts

.RowSource = "" .ColumnCount = 2 .RowSourceType = "Value List" .LimitToList = True .BoundColumn = 0

.AddItem "Old National Bank,-Car Payment" .AddItem "Chase Manhattan,-MasterCard" .AddItem "Countrywide Home Loans,-House Payment" End With

'populate the list box with values from a 'specified list With lstAccounts

.RowSource = "" .ColumnCount = 2 .RowSourceType = "Value List" .BoundColumn = 0

.AddItem "Old National Bank,-Car Payment" .AddItem "Chase Manhattan,-MasterCard" .AddItem "Countrywide Home Loans,-House Payment" End With

End Sub

The LimitToList property of the combo box was set to True, so only values in the list could be selected. Various properties such as RowSource and RowSource type were specified to indicate that the values would be coming from a value list, as opposed to a table or other source.

You also added code to display the selected value from the combo box and list box. When you changed the value in the combo box, a message box shows users the value displayed in the text field.

Private Sub cboAccounts_Change()

'display a message to the user of the selected value MsgBox cboAccounts.Text

When you select a value from the list box, a message box shows users the value contained in the first selected column.

Private Sub lstAccounts_Click()

'display a message to the user of the first value in 'the selected column MsgBox lstAccounts.Column(0)

End Sub

Tab Controls and Subforms

When you have a lot of data that needs to be displayed on a single form, tab controls provide one way or organizing data on a single form but into separate components. A tab control allows you to place separate controls on each tab. For example, you may have one tab that contains checking account information and another tab that contains savings account information, as shown in Figure 6.9.

Figure 6.9

The case study in Chapter 12 illustrates using a tab control for different portions of the project record. You can then refer to the pages of the tab control in VBA code, as shown here.

'show the checking account register Page1.Visible = True

'hide the savings account register Page2.Visible = False

In the preceding code example, the checking account register is displayed to the user and the savings account is not. This may be useful in a scenario when one user is named on both accounts, but another user is named on only one of the accounts. You could, for example, control the visibility of the tabs based on the specified user ID.

Another way to handle forms that include complex amounts or types of data is to use a subform. A subform is a form within a form. To insert a subform, you simply drag and drop the Subform control from the Toolbox just as you would any other control. You then change the Source Object property of the subform to the name of the form you wish to display. An example of a subform is shown in Figure 6.10.

Figure 6.10

Figure 6.10 shows a transactions form displayed within an accounts form. Any form that you create can be displayed within another form. You then tie the subform to the parent form so that the data it displays relates to the data on the parent form (for example, for the same account). One way to tie a subform to a parent form is using the Link Child Fields and Link Master Fields properties.

Whenever you make changes to a subform you are changing the original form, so be careful.

0 0

Post a comment