I have a database that is based on having recipes, I have a Many to Many relationship set up. I have following:
RecipeTable:
RecipeID
RecipeName
RecipeCategoryTable:
RecipeID
CategoryID
CategoryTabe:
CategoryID
CategoryName
When I run a query of RecipeName I get multiple results, the name is still the same but each result will have a different category next to it.
For example:
Recipe Name
BBQ Pork
Category Name
Chinese
Recipe Name
BBQ Pork
Category Name
BBQ
Recipe Name
BBQ Pork
Category Name
Dinner
Is there a way to join these to display as one result as below?? Any help would be greatly appreciated. :)
Recipe Name
BBQ Pork
Category Name
Chinese, BBQ, Dinner
3 934
It will require a bit of VBA which I will let you have this evening.
In the mean time, I think you need an additional field SortOrder in your RecipeCategory Table because if you want the result
Chinese, BBQ, Dinner
not
BBQ, Dinner, Chinese
we need to tell the program the order to print these out.
So those 3 fields need to be a unique key.
Phil
I hope I get this right, as my version is using an almost identical layout but I have to change names to coincide with your tables.
First we must create a query. -
SELECT Recipe.RecipeName, Recipe.RecipeID, Ccat(Recipe.RecipeID,[CategoryName]) AS InterestStg
-
FROM CategoryTable INNER JOIN (Recipe INNER JOIN RecipeCategoryTable ON Recipe.RecipeID = RecipeCategoryTable.RecipeID) ON Recipe.RecipeID = RecipeCategoryTable.RecipeID
-
GROUP BY Recipe.RecipeName, Recipe.RecipeID,Ccat(Recipe.RecipeID,[CategoryName])
-
ORDER BY RecipeName;
-
Now for the VBA. What we are doing here is to read all the categories that apply to each recipe, and concatenating them (adding them together) with a comma and space. -
Function Ccat(ID As Long, StgToConcat As String)
-
-
Dim MyDb As Database
-
Dim Rst As Recordset
-
Dim SQLStg As String
-
Dim OutStg As String
-
-
SELECT Recipe.RecipeName, Recipe.RecipeID
-
FROM CategoryTable INNER JOIN (Recipe INNER JOIN
-
RecipeCategoryTable ON Recipe.RecipeID =
-
-
RecipeCategoryTable.RecipeID) ON Recipe.RecipeID = RecipeCategoryTable.RecipeID
-
GROUP BY Recipe.RecipeName, Recipe.RecipeID,Ccat(Recipe.RecipeID,[CategoryName])
-
ORDER BY RecipeName;
-
Set MyDb = CurrentDb
-
Set Rst = MyDb.OpenRecordset(SQLStg)
-
-
With Rst
-
Do Until .EOF
-
OutStg = OutStg & !Interest & ", " ' Agg the strings with a comma & space
-
.MoveNext
-
Loop
-
.Close
-
Set Rst = Nothing
-
Ccat = Left(OutStg, Len(OutStg) - 2) ' Remove last Comma space
-
-
End With
-
-
End Function
-
Whoops, sorry, for some reason the reply was posted before I had finished it.
Please ignore the previous VBA, I will try again. -
Function Ccat(ID As Long, StgToConcat As String)
-
-
Dim MyDb As Database
-
Dim Rst As Recordset
-
Dim SQLStg As String
-
Dim OutStg As String
-
-
SELECT Recipe.RecipeName, Recipe.RecipeID
-
FROM CategoryTable INNER JOIN (Recipe INNER JOIN
-
RecipeCategoryTable ON Recipe.RecipeID =
-
RecipeCategoryTable.RecipeID) ON Recipe.RecipeID =
-
RecipeCategoryTable.RecipeID
-
ORDER BY SortOrder;
-
-
Set MyDb = CurrentDb
-
Set Rst = MyDb.OpenRecordset(SQLStg)
-
-
With Rst
-
Do Until .EOF
-
OutStg = OutStg & !Category & ", " ' Add the strings with a comma & space
-
.MoveNext
-
Loop
-
.Close
-
Set Rst = Nothing
-
Ccat = Left(OutStg, Len(OutStg) - 2) ' Remove last Comma space
-
-
End With
-
-
End Function
-
Phil
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Francisco |
last post by:
I have this question:
I have a simple search to a db, something like:
"select description from games where year = '1990'"
suppose I get 300 results, I would like to display this in pages of 30...
|
by: Charlie |
last post by:
Hello,
I have data in an Access table that I would like to export to multiple
HTML tables. I would like to split the data in the Access table
(about 92,000 records) into multiple HTML...
|
by: Jeremy |
last post by:
I have a datasheet subform that is based off an ADO recordset. All is
fine excpet the query results are displayed oddly. Rather than
displaying the results as say 95.43, it displays it as...
|
by: Stef |
last post by:
Hi people,
I'd like to know if it's possible to get DataRows containing data
from different tables that are part of one single dataset?
Example:
Dim ds As New DataSet("myDataSet")
'Load 3...
|
by: kiqyou_vf |
last post by:
I'm trying to pull data from 2 different tables and do a loop to
retrieve more than one row. I'm having problems with aligning the
information. Can someone lead me in the right direction? I've done...
|
by: simon |
last post by:
hello,
i have a form where there are multiple dropdown lists that will all be
populated from the same initial data query.
i have a vb class defined to make the stored proc call and that
returns a...
|
by: Brad Baker |
last post by:
I'm completely new to ASP.NET programming, so please accept my apologies in
advance for asking what is probably an obvious question. :-)
I'm trying to write a page which will display the...
|
by: dympna |
last post by:
Hi
I have created a form which is derived form a query the query has a
prompt box which prompts the user to input the criteria they wish to
see.
I have worked out how to put a command button into...
|
by: Del |
last post by:
Hello and thanks for any and all assistance!
I have a database that is used by several users on several different
machines.
The backend database is housed on a file server.
Each user has a...
|
by: CoreyReynolds |
last post by:
Hey all,
This may be the dumbest thing I've ever seen. I'm creating a queryDef in VBA for access. Here's a sample of the query it creates:
SELECT tblEquipment.equipmentID,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |