i'm learning Access by few days and i'm having some problems running into a double loop. I'm doing something wrong because i'm not expert with Access. It seems i'm stuck in an infinite loop (or some other error). My goal is to retrieve some data from the same table. Here's an example:
Table1
Field1 Field2
AAA 1
BBB 2
CCC 3
CCC 4
AAA 5
BBB 6
i want to retrieve all the records in field 2 where Field1= x (but not repeating the action if the same record is found)
Here's my code:
Dim strSQL as String, rs as DAO.Recordset, rs2 as Dao.Recordset, result as String
strSQL = "SELECT * FROM Table1"
strSQL2 = "SELECT DISTINCT Field1 FROM Table1"
Set rs = Currentdb.OpenRecordset(strSQL)
Set rs2 = Currentdb.OpenRecordset(strSQL2)
rs2.movefirst
While not rs2.EOF
rs.movefirst
result = ""
While not rs.EOF
If rs.Fields("Field1") = rs2.Fields("Field1") Then
result = result & rs.fields("Field2") & " "
rs.movenext
End if
Wend
rs2.movenext
Debug.print result
Wend
Set rs=Nothing
Set rs2 = Nothing
The result i want should be:
Result(first loop) = 1 5
Result(second loop) = 2 6
Result(third loop) = 3 4
EDIT: i'm running loops because i need to generate emails with the data i found. I know how to generate emails but i'm only stacked with this loop. An example of emails should be like this:
number of emails = number of unique values in [table1].[field1] (so 3 in the before mentioned example)
for each email a list of all records in field2 that has the value-in-loop in field1.