Command button to go to previous record in an Unbound combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aviqq
    New Member
    • Apr 2023
    • 3

    Command button to go to previous record in an Unbound combo box

    Hi,

    I have an unbound combo box that I would like to navigate back and forth through the records populated in the combo box. I currently have an advance button thanks to this post here: https://bytes.com/topic/access/answers/649962-advancing-unbound-combo-box. Does anyone know how to do the opposite and create an previous record VBA code?

    This is the code I am using for my advance:
    Code:
    Function AdvanceCombo(strForm As String, strControl As String)
    'This function clears all entries on all listboxes on the form strForm
    Dim c As Control
    Dim F As Form
    Dim I As Long
    Dim cValue As String
    Set F = Forms(strForm)
    Set c = F(strControl)
    cValue = c.Value
    For I = 0 To c.ListCount - 1
    If c.ItemData(I) = cValue Then c.Value = c.ItemData(I + 1)
    Next I
    End Function
    and the command button:

    Code:
    Private Sub cmdnext_Click()
    AdvanceCombo Me.Name, "cboName"
    cboName_AfterUpdate
    End Sub
    Thank you!
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32663

    #2
    Hi Aviqq.

    Welcome to Bytes.com :-)

    You could simply change the code on line #11 to :
    Code:
    If c.ItemData(I) = cValue Then c.Value = c.ItemData(I - 1)
    Notive, neither version handles coming to the end of the list very tidily.

    Comment

    • Aviqq
      New Member
      • Apr 2023
      • 3

      #3
      Thank you it worked!

      However, I get a Run-time error '94': Invalid use of Null. This occurs when nothing has been selected yet in the combo box. When I click debug, "cValue = c.Value" is highlighted. Could I make it so when nothing is selected and I click the advance button, it goes to first record? and when I click previous button it goes to the last?

      Also, Is there any way I could make it so that the advance and previous buttons wrap to begging or end?

      Thanks so much for the help.

      Comment

      Working...