简体   繁体   中英

issue with ms access vba in listbox index and setfocus changing on click

Hopefully you can help me. I have a form bound to a query, in which navigation is directly related to a list box and a Next and Previous set of buttons I created. On Load I select the first row in the list box and the focus also is automatically being set on the same first row; consequently, when I select that first row, I take the unique ID of the record and clone the recordset so that I can display it on the form. As I update the records on the form, via a textboxes and drop-downs, when the record meets a certain criteria, then it's deleted from the table and added to another table. So, after deleting the record, I try for the list box to have the same row selected; which, after requerying, now should show the very next record. The problem is that the listindex changes and if I pull it up on the Immediate Window it shows the index number from the previously clicked-on row, with the dotted line around it, but not the actually selected and blacked out row. I know the listbox.setfocus command was giving me that issue, but I took all of the setfocuses out and it helped, however now users can't use the mouse wheel since the list box is not onfocus. Also, the dotted line appears again after you click on the scroll bar on the right side.

The AfterUpdate code moves the record when it's completed. There is a move_completed procedure in there but it's only used to run the sql needed to move the record to an archive table upon completion. If I can't get the listbox and the form synchronized then records don't move correctly due to the code not picking up the right unique ID in order to move them.

Any help is much appreciated; let me know if I'm missing any code ;)

     Private Sub Form_Load()
        With Me
        'I populate the hidden text box below in order to keep a reference as data is updated and use it when we need to do requeries
           .txtCurrentFile.Value = Forms!frmSelectPT.cboSelectPTFile.Value
           'QRecSel is the name of the listbox which is now being populated, onload
           .QRecSel.RowSource = "" & _
              "SELECT P.ID, P.[Member Number], P.[Member SSN], P.[Last Name] & ', ' & P.[First Name] AS [Mbr Name], P.DOB, " & _
                 "P.[Member Eff Date], P.HRID, P.[Date Completed], P.[Enrollment Rep], P.[Edit Worked], P.Comments " & _
              "FROM sqPTRecSource AS P " & _
              "WHERE (((P.[File Name])='" & .txtCurrentFile & "'));"
        End With
        'Here we move to the first row and subsequently will make the form's record to the first row selected
        Call MoveToFirstOnlist

     End Sub


     Sub MoveToFirstOnlist()
        Dim rs As DAO.Recordset, strEditRcdID As String
        'I will now select top row, since my listbox has no headders, then it's 0,0
        Me!QRecSel.Selected(0) = True
        'I noticed this line is a little redundant, since the listbox bound column is column "0" anyways, but it works for me when I pull the ID
        strEditRcdID = Nz(Me.QRecSel.Column(0), 0)
        Set rs = Me.Form.RecordsetClone
        rs.FindFirst "[ID] = " & strEditRcdID
        Me.Form.Bookmark = rs.Bookmark

     'Cleanup
        Set rs = Nothing
        strEditRcdID = ""
     End Sub

     Private Sub cmdNext_Click()
        With Me.QRecSel
           If IsNull(Me.QRecSel.Column(0, (.ListIndex) + 1)) = False Then
              Me.QRecSel.Selected((.ListIndex) + 1) = True
              QRecSel_Click
           Else
              MsgBox "The last edit is already selected!"
           End If
        End With
     End Sub

     Private Sub cmdPrevious_Click()
        Dim lngIndex As Long

        With Me.QRecSel
           lngIndex = .ListIndex

           If lngIndex > 0 Then
              .Selected(lngIndex - 1) = True
              QRecSel_Click

           ElseIf lngIndex = 0 Then
              MsgBox "The first edit is already selected!"
           End If
        End With
     End Sub

     Private Sub QRecSel_Click()
         Dim rst As DAO.Recordset, varID As Variant
         RunCommand acCmdSaveRecord
         varID = "'" & Me!QRecSel.Column(0) & "'"
         Set rst = Form.RecordsetClone
         rst.FindFirst "cstr([ID]) = " & varID
         Form.Bookmark = rst.Bookmark

        Set rst = Nothing
        varID = ""
     End Sub

Private Sub cboEditWorked_AfterUpdate()
   On Error GoTo Error_Handler
   Dim strCurrRecID As String, lngRow As Long, lngLastIndex As Long, strLastRowSource As String
   With Me
      strCurrRecID = .QRecSel.Column(0)
      If .cboEditWorked.Value = "Yes" Then
         If .cboEnrollmentRep.Value = "" Or IsNull(.cboEnrollmentRep) = True Then
            MsgBox "Please pick 'Enrollment Rep' from list", vbCritical, "No Enrollment Rep!"
            .cboEditWorked.Value = "No"
            RunCommand acCmdSaveRecord
         ElseIf .cboEnrollmentRep.Value <> "" Or IsNull(.cboEnrollmentRep) = False And .cboEditWorked.Value = "yes" Then
            .txtDateCompleted.Value = Format(Date, "MM/DD/YYYY")
            RunCommand acCmdSaveRecord
            lngLastIndex = .QRecSel.ListIndex
            strLastRowSource = .QRecSel.RowSource
            Move_Completed .cboEnrollmentRep, .cboEditWorked, .QRecSel.Column(0), "tbl_MEI_PT_ARCHIVE", _
               "tbl_MEI_PT_WORK", "ID"
            .QRecSel.Requery
            If .QRecSel.ListCount > 0 Then
               .QRecSel.Selected(lngLastIndex) = True
               QRecSel_Click
            Else
               If .QRecSel.ListCount > 0 Then
                  Call MoveToFirstOnlist
               End If
            End If
         End If
      End If
   End With

Exit_Here:
   strCurrRecID = "": lngRow = 0: lngLastIndex = 0
   Exit Sub

Error_Handler:
   Resume Exit_Here

End Sub

Well, after waiting patiently for a few days, and not one expert cared to pull me out of the water, I decided to read through about 100 pages of useless Microsoft documentation, and realized (after figuring out I was wasting my time) that I was missing one single line of code:

    Me.lstMyListBox = Null

Which resets the ListIndex value before anything else is done. My answer was in front of my face the whole time. At the end my AfterUpdate event looks like below, and it works beautifully:

        '1.Check if there are any records listed on the listbox
        '2.Check if it's possible to move to the next record on the list
        '3.If you couldn't move to next item then move to previous
        '4.If you coudn't move to either then move to first
        If .QRecSel.ListCount > 0 Then
           If IsNull(DLookup("[ID]", "[sqPTRecSource]", "[ID]=" & strNextID & "")) = False Then
              For lngRow = 0 To .QRecSel.ListCount - 1
                 If .QRecSel.Column(0, lngRow) = strNextID Then
                    .QRecSel = Null
                    .QRecSel.Selected(lngRow) = True
                    QRecSel_Click
                    Exit For
                 End If
              Next lngRow
           ElseIf IsNull(DLookup("[ID]", "[sqPTRecSource]", "[ID]=" & strPrevID & "")) = False Then
              For lngRow = 0 To .QRecSel.ListCount - 1
                 If .QRecSel.Column(0, lngRow) = strPrevID Then
                    .QRecSel = Null
                    .QRecSel.Selected(lngRow) = True
                    QRecSel_Click
                    Exit For
                 End If
              Next lngRow
           Else
              Call MoveToFirstOnlist
           End If
        End If
     End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM