Keith:
You can also do that dynamically by using a call-back function as the
RowSourceType (NB not the RowSource which is left blank) property of the list
box. This list all reports which have Description properties (entered via
the database window). The following function is added to the form's module:
Function ReportList(fld As Control, ID As Variant, row As Variant, col As
Variant, Code As Variant) As Variant
Dim dbs As DAO.Database
Dim doc As DAO.Document
Dim ctr As DAO.Container
Dim varReturnVal As Variant
Dim n As Integer
Static intEntries As Integer
Static aData() As Variant
Select Case Code
Case acLBInitialize
Set dbs = CurrentDb
Set ctr = dbs.Containers("Reports")
intEntries = ctr.Documents.Count
ReDim aData(intEntries, 1)
For Each doc In ctr.Documents
If Left(doc.Name, 3) = "rpt" Then
aData(n, 0) = doc.Name
On Error Resume Next
aData(n, 1) = doc.Properties("Description")
If Err = 0 Then
n = n + 1
Else
intEntries = intEntries - 1
End If
On Error GoTo 0
Else
intEntries = intEntries - 1
End If
Next doc
varReturnVal = True
Case acLBOpen
varReturnVal = Timer
Case acLBGetRowCount
varReturnVal = intEntries
Case acLBGetColumnCount
varReturnVal = 2
Case acLBGetColumnWidth
varReturnVal = -1
Case acLBGetValue
varReturnVal = aData(row, col)
Case acLBEnd
Erase aData
End Select
ReportList = varReturnVal
End Function
The RowSourceType property of the list box is set to ReportList; note that
for this no parentheses follow the function name as is usually the case. You
could of course limit the list to particular categories of report by adopting
some naming convention for them and amending the code slightly so that it
only returns a particular set of reports. e.g. where the name ends "_Sales"
for all sales reports, or "_Budget" for all budgetary reports. In the above
case the list is limited to those beginning "rpt" to distinguish them from
subreports, which I prefix "subrpt".
With later versions of Access its now also possible to iterate through the
AllReports collection to list the reports of course rather than using a
call-back function as above.
If a multi-select list box is used then multiple reports can be selected and
opened with one button click using the following in the button's Click event
procedure:
Dim varItem As Variant
Dim rpt As Report
If lstReports.ItemsSelected.Count > 0 Then
For Each varItem In lstReports.ItemsSelected
DoCmd.OpenReport lstReports.ItemData(varItem), acViewPreview
If Not Me.chkOpenAll Then
' wait for report to be closed
' before opening next one if
' check box unchecked
Do While True
On Error Resume Next
Set rpt = Reports(lstReports.ItemData(varItem))
If Err <> 0 Then
Err.Clear
Exit Do
End If
DoEvents
Loop
End If
Next varItem
End If
The reports can be viewed one by one by unchecking the chkOpenAll check box
on the form, or all together in separate windows if its checked, the default
in my case.
Ken Sheridan
Stafford, England