I have 2 tables, joined in a one to many relationship. Is there any way I can test to see if every record in the main table has at least one record in the subtable before allowing the user to run the forms procedures?
To get a list of main records without child:
SELECT M.PrimaryKey, Count(C.ForeignKey) As CountOf Childs
FROM [Main table] AS M LEFT JOIN [Child Table] AS C ON M.PrimaryKey = C.ForeignKey
GROUP BY M.PrimaryKey
HAVING Count(C.ForeignKey) = 0
Another way:
SELECT M.*
FROM [Main table] AS M LEFT JOIN [Child Table] AS C ON M.PrimaryKey = C.ForeignKey
WHERE C.ForeignKey Is Null
Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
I think you need to decide what your question is:
' Is there any way I can test to see if every record in the main table has at least one record in the subtable before allowing the user to run the forms procedures?'
That is the question that has been answered and has nothing to do with form recordsources.
You now appear tbe saying that you want to open the form first. Please clarify your requirements.
Sorry let me give the a better explenation. Somewhere between opening the form and running a procedure under a command button I need to make sure every record in the main table has a subrecord.
Simply test the RecordCount property of a Recordset opened with one of the already posted SQL code.
Or, if you're not confident with recordsetset, have an hidden Listbox with RowSource set to the SQL code you prefer, and then in the Click event procedure of your button you may test the ListCount property and make the ListBox visible if not empty.
Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.