Using Next and Previous Buttons in Excel Userform

Next & Previous Buttons On Userform



         With the buttons that we added to the userform, the next - previous record can be selected between the listbox items,also the first item and the last item of the listbox can be easily selected.

The button codes for selecting the next record(Next button) :
If ListBox1.ListIndex = ListBox1.ListCount - 1 Then
     MsgBox "Last Record", vbCritical
     Exit Sub
Else
     TextBox6 = TextBox6 + 1
     With Me.ListBox1
     .ListIndex = .ListIndex + 1
      End With
End If

The button codes for selecting the previous record(Previous button) :
If TextBox6 = 0 Then
      Exit Sub
End If
If ListBox1.ListIndex = 0 Then
      MsgBox "First Record", vbCritical
      Exit Sub
Else
      TextBox6 = TextBox6 - 1
      With Me.ListBox1
      .ListIndex = .ListIndex - 1
      End With
End If

The button codes for selecting the first record(First button) :
ListBox1.ListIndex = 0

The button codes for selecting the last record(Last button) :
ListBox1.ListIndex = ListBox1.ListCount - 1

         ➽ The index number of the record selected from the listbox is displayed on the textbox control in the userform. Also, the textboxes on the left side are filled according to the data of the selected record (sheep address,sheep state,ship date,payment type).



No comments:

Post a Comment