Question : Forms and listboxes default value

I have a list box in a form where I can select from multiple items.  when I move to a new record, i want the default for the list box in the new record to be the next one in the list boxafter the one previously selected...for example:

    I have a list box that lists "Dog, Cat, Apple"

    I create the first record and choose "Dog"
    When I move to the new record line, I want it to default to "Cat", then for the next record "Apple", and so on

It would essentially keep track each time a record is entered and then set the default to the next value in the list for the new record

Answer : Forms and listboxes default value

If you can select multiple items in the list, which one are we supposed to use to select the "next item"?

If the record you are viewing is an old record, and you go to a new record, which item do you want to highlight as the "default" for the new record?

How are you navigating to the "next record"?

Are you using the standard navigation buttons, or some custom record controls?

If you are using the standard navigation buttons, I would recommend using the forms BeforeUpdate event to determine which item is selected in the listbox.  You will need to:

1. create a private variable and declare it in the forms general declarations section.  
2. Then, in the Forms BeforeUpdate event, call the attached subroutine (FirstSelectedListItem) to determine the first selected item in your listbox
3.  Then, in the Forms Current event you should test to determine whether you are on a new record.  If so, unselect all the selected items in the list  (ClearListSelections), increment your variable, and select that item.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Public Function FirstSelectedListItem(lst As ListBox) As Long

    Dim varItem As Variant
    
    For Each varItem In lst.ItemsSelected
        FirstSelectedListItem = varItem
        Exit Function
    Next
    
End Function

Public Sub ClearListSelections(lst As ListBox)

    Dim intLoop As Integer
    
    For intLoop = 0 + Abs(lst.ColumnHeads) To lst.ListCount - 1 + Abs(lst.ColumnHeads)
        lst.Selected(intLoop) = False
    Next
    
End Sub
Random Solutions  
 
programming4us programming4us