Discussion Locked

Access 97 Address Book Report

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a database which has a single entry for each member of a family. In the report I use, I have managed to group all the familys with the same address together, but the report formats as follows
Surname ,Address, Phone
Firstname
Firstname
etc

I have seen reports that format as follows, but I don't know how to achive this.

Surname, Address, Phone, etc
Firstname, Firstname, Firstname, etc

Can anyone help.

Thanks

4 replies

by peyison

There are a few things you need to do to do this.

First, modify your report so that the Report Source is a query that selects only distinct fields, and does not include the Firstname field. It should look something like:

SELECT DISTINCT Surname, Address, Phone
FROM tblAddressBook
ORDER BY Surname, Address;

This will return only one record per unique Surname/Address combination.

Next, you need to add some VBA code to the Detail section:

* Right click in the Detail section and select Build Event...

* then select Code Builder

* Paste this code (exclduing the first and last lines, which should already be there)

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim db As Database
Dim rst As Recordset
Dim sSql As String
Dim sFirstName As String

'--select all the first names from records that match the current Surname and Address
sSql = "Select Firstname from tblAddressBook Where Surname = '" & tbxSurname & _
"' and Address ='" & tbxAddress & "' Order By FirstName"

Set db = CurrentDb
Set rst = db.OpenRecordset(sSql)

'--loop through all the records and add to a string
Do Until rst.EOF
sFirstName = sFirstName & rst!FirstName & ", "
rst.MoveNext
Loop

'--remove the trailing comma and space, and add to the text box
If Len(sFirstName) > 0 Then
tbxFirstName = Left$(sFirstName, Len(sFirstName) - 2)
End If

rst.Close
Set rst = Nothing
Set db = Nothing
End Sub

You will probably have to modify the table name and field names to match yours. Also, this code references some text boxes on the report, described below. Those names might need to be changed as well.

more to follow....

by peyison

To set up the report - add the bound text boxes for Surname, Address and Phone on one line. On the next line, add a new, unbound text box called tbxFirstname. This text box will receive the string created in the code above.

That should do it.Hope this helps.

This discussion is closed to new replies.