简体   繁体   中英

Loop through entire Access 2010 listbox

I have a form with a list box and a date picker. When I enter a date and select 1 of 40 items from the list box and click my button i'm able to produce a single report. My question, how can loop through all 40 items and export each report to pdf? Any help will be greatly appreciated. Also I would like each report to save with the same name as it is in the listbox. Ex. the first report is titled RHM1 and so on.

Private Sub Command3_Click()

MyFilter = "rptIncidentsByOrg"
MyPath = "C:\ComplyTrack\"
MyFilename = "Test.pdf"

For Each varItem In Me.lstActivityOrgs.ItemsSelected
    Debug.Print Me.lstActivityOrgs.Column(0, varItem)

DoCmd.OpenReport "rptIncidentsByOrg", acViewPreview, , "([Activity Org] = " & Chr(34) & Me.lstActivityOrgs.Column(0, i) & Chr(34) & ")"
DoCmd.OutputTo acOutputReport, "rptIncidentsByOrg", acFormatPDF, MyPath & MyFilename, True
DoCmd.Close acReport, "Test"

Next varItem

End Sub

UPDATED Option 'B' for file name...

Try something like: (Change the COLUMN as needed)

A. To list only items selected:
Private Sub Command3_Click()
    For Each varItem In Me.lstActivityOrgs.ItemsSelected
        Debug.Print Me.lstActivityOrgs.Column(0, varItem)
        DoCmd.OpenReport "rptIncidentsByOrg", acViewPreview, , "([Activity Org] = "      & Chr(34) & Me.lstActivityOrgs.Column(XXX,varItem) & Chr(34) & ")"
    Next varItem
End Sub

Or B. List All Items:
Private Sub Command3_Click()
    MyFilter = "rptIncidentsByOrg"
    MyPath = "C:\ComplyTrack\"
    MyFilename = "<fromListBox>.pdf"
    For i = 0 To Me.lstActivityOrgs.ListCount
        MyFileName = Me.lstActivityOrgs.Column(0, i) & ".pdf"
        Debug.Print Me.lstActivityOrgs.Column(0, i)
        DoCmd.OpenReport "rptIncidentsByOrg", acViewPreview, , "([Activity Org] = "      & Chr(34) & Me.lstActivityOrgs.Column(XXX,i) & Chr(34) & ")"
        DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, MyPath & MyFileName
        DoCmd.Close acReport, "rptIncidentsByOrg"
    Next i
End Sub

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