I have a table in my access database called Table_Example_1. Currently I have a loop that loops through an executes code based on the [Flag] field in my table, so if everything is set to 'Flag_1' it loops through these records and executes code to create a string output.
However there are some items that have the same flag for example 'Flag_1' but they need to be executed separately to the other records with the same value. I have added a field called [Grouped] and what I want to do is use this field to loop through everything in that grouping for the same flag.
So if there are 2 records set as Flag_1 and both are Group 1 these will run in the same loop. And records with Flag_1 in Group 2 will run in a separate loop. (The results of these will then be combined at the end)
My table with relevant data is below:
Business Type TableName FieldName Flag Grouped
Red Residential RedTable [Field_1] Flag_1 1
Red Residential RedTable [Field_1] Flag_1 1
Red Residential RedTable [Field_1] Flag_1 1
Red Residential RedTable [Field_2] Flag_1 2
Red Residential RedTable [Field_2] Flag_1 2
My current code is below, this code currently only loops through records with the same Flag and does not take into account their grouping.
- It sets the record set and filters for everything with Flag_1
- It then loops through the record set and executes code to set the result variable.
So what I want to do to the below code is somehow add in a section that checks the grouping and looks through all groups and then my below loop will be nested in this.
I first need to get the distinct number of groups from the recordset. My example provided only has groups 1 and 2 but this will differ each time.
I then want to nest by below loop in this. I was thinking of using a for loop but I'm not sure how to check for the number of groups in the recordset and execute this for each of these groups.
Public Function CreateFilterString(Business As String, Type As String, TableName As String)
'Define variables
Dim resultString_1 As String
Dim resultStringFinal As String
Dim recSet As Recordset
'Flag_1 Related Flag RecordSet
Set recSet = CurrentDb.OpenRecordset("SELECT * FROM Table_Example_1 WHERE Table_Example_1.TableName = '" & TableName & "' AND Table_Example_1.Business = '" & Business & "' AND Table_Example_1.Type = '" & Type & "' AND Table_Example_1.Flag = 'Flag_1' ")
'If there is no data in the recordset, then set the resultString_1 to blank
If recSet.RecordCount = 0 Then
resultString_1 = ""
'Otherwise execut the following
Else
'If there is data in the recordset and the Flag field is Flag_1
If recSet.RecordCount > 0 And recSet.Fields(7).Value = "Flag_1" Then
'Set the resultString variable
recSet.MoveFirst ' recSet now contains first record from the query
While Not (recSet.EOF)
'If it is the first case in the recordset
If recSet.RecordCount = 1 Then
'code executed here that sets value of resultString_1
Else
'code executed here that sets value of resultString_1
End If
'moves to next record
recSet.MoveNext
Wend 'end while
recSet.Close 'close recordset when done
'End of Flag_1 Related Flag
End If
End If
'Set the result variable returned by the function
CreateFilterString = resultStringFinal
End Function
EDIT: So to summarize and hopefully clarify what I want:
I want to be able to get the distinct values from the GROUPING field and then loop through each of these values. I need a loop that runs through each unique value in the 'Grouped' field. So it will execute once for any record with a grouping of 1 and then reach the end of the loop and re-run for anything with a grouping of 2.
I understand that I can create a recordset usingCurrentDb.OpenRecordset("SELECT DISTINCT Grouped FROM Table_Example_1 WHERE ...
in order to get the unique values, but I don't know how to then use this information in order to create a loop to run through each value in the GROUPED field.
Thanks in advance
SELECT DISTINCT Grouped FROM Table_Example_1 WHERE ...
The number of different Groups would berecSet.RecordCount
. I don't really understand, what you want to do with the information – MGP