I am using Visual Studio and have an Access database with a couple of tables that are related. They are using default drag/drop binding using WinForms to the dataset which was automatically generated as well.
I want the parent table to include a column with the count of the number of related records with that ID in the results. The tables look like this if it matters:
I added an additional query to the table adapter (as you can see from the screenshot) that does what it needs to do (without including the linked ID's) thinking that was how to do it. Am I at least on the right track? The query looks like this:
SELECT COUNT(*) AS WorkerCount
FROM (tblJobLaborTech INNER JOIN tblJobLabor ON tblJobLaborTech.JobLaborID = tblJobLabor.JobLaborID)
WHERE (tblJobLabor.JobLaborID = 494)
And of course I need to change 494
to something variable which will match the corresponding record when it runs, but
WHERE (tblJobLabor.JobLaborID = tblJobLaborTech.tblJobLaborTechID)
doesnt seem to preview at all (no value given for parameter)
so i also tried
WHERE (tblJobLabor.JobLaborID = @JLID)
which also doesnt preview.
But that's only the start of the problem. Let's say I can get that part working, how do I get the result of that Scalar query to run for each record in the calling query and return in the dataset as if it were another field in that table?
You don't need to do this using the DB. A datatable can count the records present along a child relation
In your tblJobLabor
add another datacolumn (right click, add>>column) and set its Expression property to:
Count(Child(XXX).JobLaborID)
Replace XXX with the name of this DataRelation I've highlighted:
More information can be found by looking at the fairly lengthy documentation on DataColumn.Expression at https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=netframework-4.8