ms-accessvbams-access-2003continuous-forms

Continuous form selection deselected on command button click


I have a continuous access form with record selectors enabled. Here's the code for the btnPrintReceipt click event handler. I want it to get the ReceiptID of each selected record and open the report with those id's. The problem is, when you click the command button, it deselects all the records (I see it happen) and only keeps the top one available.

Private Sub btnPrintReceipt_Click()
    'Build filter string containing all selected receipt ids
    Dim FilterString As String

    'Move to first record
    Dim rsReceipts As Recordset
    Set rsReceipts = Me.RecordsetClone
    rsReceipts.Move Me.SelTop - 1

    'Cycle through and record
    Dim i As Integer
    For i = 0 To Me.SelHeight
        FilterString = FilterString & "([ReceiptNumber]=" & rsReceipts![ReceiptNumber] & ") OR "
        rsReceipts.MoveNext
    Next

    'Remove trailing or
    Dim NewStringLenth As Integer
    NewStringLenth = Len(FilterString) - 4
    If NewStringLenth > 0 Then
        FilterString = Left(FilterString, NewStringLenth)
    Else
        FilterString = ""
    End If

    'Open the report
    DoCmd.OpenReport "rptReceipt", acViewPreview, "", FilterString
End Sub

Solution

  • Microsoft has a fairly long article on How to enumerate selected form records in Access 2000, which will also work with later versions. The article includes code for running on a command button.