Using Spinbutton on a Userform

As it is, the Hire Date field allows the user to enter the date in any format: 1/1/1 or January 1, 2001. This possible inconsistency can create problems later on if you need to use or search for dates. The solution? Force users to enter dates in a unified manner.

Spinbuttons allow the user to increment/decrement through a series of numbers. In this way the user is forced to enter numbers rather than text.

Draw a spinbutton for a Month entry on the form. In the properties, set the Min to 1 (for January) and the Max to 12 (for December). In the Value property, enter 1, the first month. Next, draw a text box next to the spinbutton. This is the text box that will reflect the value of the spinbutton (labels can also be used).

Private Sub SpBtn_Month_Change() tb_Month.Value = SpBtn_Month.Value End Sub

Finish building the form as shown in Figure 9.13. Use a Min of 1 and Max of 31 for days; Min of 1900 and a Max of 2100 for Year.

Private Sub btn_EmpOK_Click() Dim LastRow As Long, i As Integer

LastRow = Worksheets("Sheet2").Range("A65536").End(xlUp).Row + 1 Cells(LastRow, 1).Value = tb_EmpName.Value For i = 0 To lb_EmpPosition.ListCount - 1

If lb_EmpPosition.Selected(i) = True Then

Cells(LastRow, 2).Value = Cells(LastRow, 2).Value & _ lb_EmpPosition.List(i) & "," End If

Next i

'Concatenate the values from the textboxes to create the date

Cells(LastRow, 3).Value = tb_Month.Value & "/" & tb_Day.Value & _

Figure 9.13

Use spinbuttons to control the date entry.

Next i

'Concatenate the values from the textboxes to create the date

Cells(LastRow, 3).Value = tb_Month.Value & "/" & tb_Day.Value & _

Figure 9.13

Use spinbuttons to control the date entry.

Vba Spinbutton
0 0

Post a comment