Access Tip of the Day


Creating a list of the reports in a database (97/2000/2002)


As part of your efforts to make things simpler for end users, you'll often want to provide them with an easy way to select reports. To help simplify the process, you can display the report names associated with your database in a listbox or combobox control. Then, you can create code to act upon whichever item the user selects. Fortunately, you don't have to manually populate the control's list with the report names, or worry about updating the list when reports are renamed, added, or deleted. Instead, you can use VBA to loop through a collection of reports and build a value list using the report names.

For example, let's say that you have a form that contains a listbox named lstReports. If you're using Access 2000 or greater, the following code will populate the list when the form is opened.

Private Sub Form_Load()
Dim strList As String
Dim rpt As AccessObject
Dim rpts As Object
Set rpts = CurrentProject.AllReports
For Each rpt in rpts
strList = rpt.Name & ";" & strList
Next
Me.lstReports.RowSourceType = "Value List"
Me.lstReports.RowSource = strList
End Sub

If you're using Access 97, you can use the following procedure:

Private Sub Form_Load()
Dim strList As String
Dim x As Integer
For x = CurrentDb.Containers!Reports.Documents.Count - 1 To 0 Step -1
strList = CurrentDb.Containers!Reports.Documents(x).Name & ";" & strList
Next
Me.lstReports.RowSourceType = "Value List"
Me.lstReports.RowSource = strList
End Sub



(Content provided by Element K Journals)


(Note: Your browser is set to refuse cookies. As a result, you may frequently see previously-viewed tips)
 
PrintPrint CloseClose

Copyright © 2001 - 2024 MJ Technologies, LLC.  All rights reserved.