Prompt selection to the last record in the list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluemoon9
    New Member
    • Oct 2008
    • 56

    #1

    Prompt selection to the last record in the list box

    Does anyone know how to write a code to promt the selection at the last record in the list box?. I have this code below in the "On Click" event, when user click on the code, it close a form, requery the list box, but then I would like the selection to be prompted at the last record.

    Code:
    DoCmd.Close acForm, "frmReview" 
    [Forms]![frmPatientAccount]![lstPtReview].Requery
    'woud like to add here a line of code to prompt selection of the listbox to the last record.
    thanks!
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Maybe something like:

    Me.lstPtReview = Me.lstPtReview. ItemData(Me.lst PtReview.ListCo unt - 1)

    Linq ;0)>

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      Indeed. Setting the record of a ListBox or ComboBox is done, rather bizarrely, by setting it to the value of one of them (as Linq's code does).

      Comment

      • bluemoon9
        New Member
        • Oct 2008
        • 56

        #4
        Linq,
        thanks alot. It works so well.

        bluemoon

        Comment

        • bluemoon9
          New Member
          • Oct 2008
          • 56

          #5
          Prompt selection to first record in the listbox

          Hi,
          I've tried to change from -1 to 0 to prompt record to the first added record in the list, but it did not work, anybody can help?

          <code>
          [Forms]![frmPatientAccou nt]![lstPtReview] = [Forms]![frmPatientAccou nt]![lstPtReview].ItemData([Forms]![frmPatientAccou nt]![lstPtReview].ListCount(0))

          </code>

          what I would like to do is prompt the listbox to the newly added record since my list is sorted from Z to A, so the new record will be the first record in the list.

          Thanks!
          Bluemoon

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by bluemoon9
            Does anyone know how to write a code to promt the selection at the last record in the list box?. I have this code below in the "On Click" event, when user click on the code, it close a form, requery the list box, but then I would like the selection to be prompted at the last record.

            Code:
            DoCmd.Close acForm, "frmReview" 
            [Forms]![frmPatientAccount]![lstPtReview].Requery
            'woud like to add here a line of code to prompt selection of the listbox to the last record.
            thanks!
            Assuming your List Box is named lstTest, to Select the 'Last' entry:
            Code:
            Me![lstTest].Selected(Me![lstTest].ListCount - 1) = True

            Comment

            • bluemoon9
              New Member
              • Oct 2008
              • 56

              #7
              Hi,
              Thanks for your help.
              That's was my previous question. Now, the user would like to move it to the First record instead of the last, I've tried to change from -1 to 0 or 1, but nothing works well. I've tried other way such as GoToRecord,,acF irst, but nothing happens.

              Any idea? Thanks alot!

              bluemoon

              Comment

              • DonRayner
                Recognized Expert Contributor
                • Sep 2008
                • 489

                #8
                Originally posted by bluemoon9
                Hi,
                Thanks for your help.
                That's was my previous question. Now, the user would like to move it to the First record instead of the last, I've tried to change from -1 to 0 or 1, but nothing works well. I've tried other way such as GoToRecord,,acF irst, but nothing happens.

                Any idea? Thanks alot!

                bluemoon

                Since you don't know the location of the last record you have to get the number from the count of records listed in the listbox.
                Code:
                (Me![lstTest].ListCount - 1)
                But when trying to move to the first record you already know where the record is, it's the first one.
                Code:
                Me![lstTest].Selected(1) = True
                Just be careful if it's a multiselect listbox as this won't deselect any previously selected items.

                Comment

                • bluemoon9
                  New Member
                  • Oct 2008
                  • 56

                  #9
                  Hi,
                  my listbox is not a mutiselection. It's a single select list box. so it worked perfectly.
                  Thank you for your help.

                  bluemoon.

                  Comment

                  Working...