Maybe not that incorrect. It rather sounds to me like you might have what's
known as an 'adjacency list' table via which the main table references
itself. This is most commonly used in manufacturing for bills of materials
or parts explosions, but is equally applicable to other entity types.
Simplify? As Einstein said, when asked how simple an explanation of the
Theory of General Relativity could be made, "As simple as possible, but no
more so".
Firstly add the following function to a standard module. A Standard module
is distinct form a Class Module, and is what you get if you go to the Modules
tab of the database window and add a new module. Essentially it’s a
contained for one or more functions or procedures. You can simply copy and
paste the code below into the module immediately below the couple of lines
you'll already find in the modules 'Declarations' area. That will add the
function to the module. Yiu can then append the table and/or column names as
necessary. Be sure to use a different name when saving the module, not the
same name as the function.
You haven't given your table names so I've called your other table
ReferencedPolicies. Also Its not clear whether Policy# is a policy being
referenced by the main table (i.e what you want included in your list), or
whether it’s a foreign key referencing the main table, so I'll assume it’s
former and that there is another column ReferencedPolicy# as a foreign key
referencing the main table; so if the main table's policy number is 42 and it
references policies 100 and 212 then the rows in ReferencedPolicies to model
this would be:
Policy# ReferencedPolicy#
100 42
212 42
Here's the function:
Public Function GetPolicies(lngPolicyNumber As Long) As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strPolicyNumbers As String
strSQL = "SELECT [Policy#] FROM [ReferencedPolicies] " & _
"WHERE [ReferencedPolicy#] = " & lngPolicyNumber & _
" ORDER BY [Policy#]"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL)
With rst
Do While Not .EOF
strPolicyNumbers = strPolicyNumbers & ", " & _
.Fields("[Policy#]")
.MoveNext
Loop
.Close
' remove leading comma and space
strPolicyNumbers = Mid$( strPolicyNumbers, 3)
End With
GetPolicies = strPolicyNumbers
End Function
The report's RecordSource will be a query on the main table, which I'll call
Policies. In this you'll call the function as I described in my earlier
post, passing the policy number from the main table into it, so the query
might go something like this:
PARAMETERS [Enter start date:] DATETIME,
[Enter end date:] DATETIME;
SELECT [Policy#], [PolicyName], [ReviewDate],
GetPolicies([Policy#]) AS [Referenced Policies]
FROM [Policies]
WHERE [ReviewDate] BETWEEN [Enter start date:]
AND [Enter end date:];
Note BTW that I've declared the parameters in this query. With date/time
parameters this is advisable as otherwise a date entered at the parameter
prompt in short date format could be interpreted as an arithmetical
expression and give the wrong result.
Base your report on this query and bind a text box to the [Referenced
Policies] column. Make sure the CanGrow property of both the text box and
the detail section is set to True (Yes in the properties sheet) so that if
there are more policy numbers than can be accommodated on one line it will
wrap to a second row.
Ken Sheridan
Stafford, England