Here is the code I've tried:
Sub ZZs()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim rs3 As DAO.Recordset
Dim rs4 As DAO.Recordset
Dim strCounty As String
Dim strState As String
Dim sqlString As String
Call MakeNewTable 'Destroy then recreate ZZ table
Set db = CurrentDb
Set rs1 = db.OpenRecordset("SELECT T2.ID FROM T2 GROUP BY T2.ID ORDER BY T2.ID;")
Set rs4 = db.OpenRecordset("ZZ")
rs1.MoveLast
rs1.MoveFirst
Do Until rs1.EOF 'Loop through IDs in T2
'Get list of company STATES for the particular ID
Set rs2 = db.OpenRecordset("SELECT T2.State FROM T2 WHERE (((T2.ID) = '" & rs1!ID & "')) GROUP BY T2.State;")
rs2.MoveLast
rs2.MoveFirst
Select Case rs2.RecordCount
Case 1 'company in 1 STATE only
'Get list of company COUNTIES for the particular ID and STATE
Set rs3 = db.OpenRecordset("SELECT T2.County FROM T2 WHERE (((T2.State) = '" & rs2!State & "') And ((T2.ID) = '" & rs1!ID & "')) GROUP BY T2.County;")
'Add record showing values for same STATE but outside of company COUNTIES + values for outside of company STATES
strCounty = "("
Do Until rs3.EOF
strCounty = strCounty & rs3!County & ", "
rs3.MoveNext
Loop
strCounty = Left(strCounty, Len(strCounty) - 2) & ")"
rs4.AddNew
rs4!ID = rs1!ID
rs4!County = "ZZ-County"
rs4!Customers = DSum("CustomerCount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("CustomerCount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("VisitCount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("BillAmount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4.Update
Case Else 'company in >1 STATE
strState = "("
Do Until rs2.EOF 'Create list of STATES by ID
strState = strState & rs2!State & ", "
rs2.MoveNext
Loop
strState = Left(strState, Len(strState) - 2) & ")"
'Move back to the start of the recordset
rs2.MoveFirst
Do Until rs2.EOF 'Loop through STATES by ID
'Get list of company COUNTIES for the particular ID and STATE
Set rs3 = db.OpenRecordset("SELECT T2.County FROM T2 WHERE (((T2.State) = '" & rs2!State & "') And ((T2.ID) = '" & rs1!ID & "')) GROUP BY T2.County;")
strCounty = "("
Do Until rs3.EOF 'Create list of COUNTIES by STATE by ID
strCounty = strCounty & rs3!County & ", "
rs3.MoveNext
Loop
strCounty = Left(strCounty, Len(strCounty) - 2) & ")"
'Add record showing values for same STATE but outside of company COUNTIES
rs4.AddNew
rs4!ID = rs1!ID
rs4!County = "ZZ-County"
rs4!Customers = DSum("CustomerCount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!ID & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4.Update
rs2.MoveNext
Loop
'The following is for ZZ-State and only needs to run once per ID so it's outside the state loop
rs4.AddNew
rs4!ID = rs1!ID
rs4!County = "ZZ-State"
rs4!Customers = DSum("CustomerCount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT IN " & strState)
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT IN " & strState)
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!ID & "' AND CustomerState NOT IN " & strState)
rs4.Update
End Select
rs1.MoveNext
Loop
rs1.Close
rs2.Close
rs3.Close
rs4.Close
End Sub
T2 is the table with company information.
ZZ is the table where the results are appended.
Report is the table containing customer data.
'''code tags added and code reformatted
Code:
'reformatted by orange
Sub ZZs()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim rs3 As DAO.Recordset
Dim rs4 As DAO.Recordset
Dim strCounty As String
Dim strState As String
Dim sqlString As String
Call MakeNewTable 'Destroy then recreate ZZ table
Set db = CurrentDb
Set rs1 = db.OpenRecordset("SELECT T2.ID FROM T2 GROUP BY T2.ID ORDER BY T2.ID;")
Set rs4 = db.OpenRecordset("ZZ")
rs1.MoveLast
rs1.MoveFirst
Do Until rs1.EOF 'Loop through IDs in T2
'Get list of company STATES for the particular ID
Set rs2 = db.OpenRecordset("SELECT T2.State FROM T2 WHERE (((T2.ID) = '" & rs1!id & "')) GROUP BY T2.State;")
rs2.MoveLast
rs2.MoveFirst
Select Case rs2.RecordCount
Case 1 'company in 1 STATE only
'Get list of company COUNTIES for the particular ID and STATE
Set rs3 = db.OpenRecordset("SELECT T2.County FROM T2 WHERE (((T2.State) = '" & rs2!State & "') And ((T2.ID) = '" & rs1!id & "')) GROUP BY T2.County;")
'Add record showing values for same STATE but outside of company COUNTIES + values for outside of company STATES
strCounty = "("
Do Until rs3.EOF
strCounty = strCounty & rs3!County & ", "
rs3.MoveNext
Loop
strCounty = Left(strCounty, Len(strCounty) - 2) & ")"
rs4.AddNew
rs4!id = rs1!id
rs4!County = "ZZ-County"
rs4!Members = DSum("CustomerCount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("CustomerCount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("VisitCount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty) + DSum("BillAmount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT LIKE '" & rs2!State & "'")
rs4.Update
Case Else 'company in >1 STATE
strState = "("
Do Until rs2.EOF 'Create list of STATES by ID
strState = strState & rs2!State & ", "
rs2.MoveNext
Loop
strState = Left(strState, Len(strState) - 2) & ")"
'Move back to the start of the recordset
rs2.MoveFirst
Do Until rs2.EOF 'Loop through STATES by ID
'Get list of company COUNTIES for the particular ID and STATE
Set rs3 = db.OpenRecordset("SELECT T2.County FROM T2 WHERE (((T2.State) = '" & rs2!State & "') And ((T2.ID) = '" & rs1!id & "')) GROUP BY T2.County;")
strCounty = "("
Do Until rs3.EOF 'Create list of COUNTIES by STATE by ID
strCounty = strCounty & rs3!County & ", "
rs3.MoveNext
Loop
strCounty = Left(strCounty, Len(strCounty) - 2) & ")"
'Add record showing values for same STATE but outside of company COUNTIES
rs4.AddNew
rs4!id = rs1!id
rs4!County = "ZZ-County"
rs4!Members = DSum("CustomerCount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!id & "' AND CustomerState='" & rs2!State & "' AND CustomerCounty NOT IN " & strCounty)
rs4.Update
rs2.MoveNext
Loop
'The following is for ZZ-State and only needs to run once per ID so it's outside the state loop
rs4.AddNew
rs4!id = rs1!id
rs4!County = "ZZ-State"
rs4!Members = DSum("CustomerCount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT IN " & strState)
rs4!Visits = DSum("VisitCount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT IN " & strState)
rs4!BillAmt = DSum("BillAmount", "Report", "ID='" & rs1!id & "' AND CustomerState NOT IN " & strState)
rs4.Update
End Select
rs1.MoveNext
Loop
rs1.Close
rs2.Close
rs3.Close
rs4.Close
End Sub