The following piece of code does what you want. Just be sure to change
Services to the actual name of your table, and target path and file name to
your preference. Also, if your database does not have a DAO reference, you
will need to add it (from the VB editor window, menu Tools > References,
tick Microsoft DAO 3.X Object Library: DAO 3.51 for Access97, DAO 3.6 for
Access 2K or later).
Sub export_services_by_product()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim currProduct
Dim TargetFile As String
TargetFile = "C:\Temp\Services by Product.txt"
Set db = CurrentDb()
Set rst = db.OpenRecordset("Services")
On Error GoTo no_records
rst.MoveFirst
On Error GoTo 0
Open TargetFile For Output As #1
Print #1, "Product Service_attached"
Do Until rst.EOF
currProduct = rst.Fields(0)
pline = currProduct
Do Until rst.Fields(0) <> currProduct
pline = pline & "," & rst.Fields(1)
rst.MoveNext
If rst.EOF Then Exit Do
Loop
Print #1, pline
Loop
Close #1
rst.Close
Set rst = Nothing
Set db = Nothing
Exit Sub
no_records:
MsgBox "Sorry, no records found in table"
End Sub
HTH,
Nikos