Writing Procedures to Synchronize the Text Box with the Spin Button

The Info Survey form has a text box in front of the spin button control. To indicate a percent of time that the selected Hardware or Software item is used, the user can type a value in the text box or use the spin button. The initial value of the text box is set to zero (0). Suppose the user entered 10 in the text box and now wants to increase this value to 15 by using the spin button. To enable this action, the text box and the spin button have to be synchronized. Each of these objects requires a separate Change event procedure.

1. Right-click the spin button and choose View Code from the shortcut menu.

2. Enter the spPercent_Change procedure, as shown below:

Private Sub spPercent_Change()

txtPercent.Value = spPercent.Value End Sub

Using the spin buttons will cause the text box value to increase or decrease.

3. Enter the following txtPercent_Change procedure:

Private Sub txtPercent_Change() Dim entry As String

On Error Resume Next entry = Me.txtPercent.Value If entry > 100 Then entry = 0

Me.txtPercent.Value = entry End If spPercent.Value = txtPercent.Value End Sub

The txtPercent_Change procedure ensures that only values between 0 and 100 can be entered into the text box. The procedure uses the On Error Resume Next statement to ignore data entry errors. If the user enters a non-numeric value into the text box (or a number greater than 100), Visual Basic will reset the text box value to zero (0). Each time a spin button is pressed, the text box value is incremented or decremented by one.

0 0

Post a comment