Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Form in Access from Cross Table Query

Tags:

ms-access

Here is the scenario. I have a database that is single function basically to manage documents. There are two tables that I am dealing with for this example.

Table 1: Table of Documents with the following fields DOCUMENT_NUM, PK; DOCUMENT_NAME; etc. One-to-many relationship to Table 1 with referential integrity based on DOCUMENT_NUM

Table 2: Table of revision history of documents with fields DOCUMENT_NUM, PK; REVISION_DATE; REVISION_NUM.

I have a form that enters in the information for Table 1, and another form that enters in information into Table 2 pulling in DOCUMENT_NUM off the criteria in Form 1.

My problem: I have a search form that I want to be able to open Form 1 that is bound to Table 1 by searching for not only parameters that are in Table 1 but also in Table 2. Example: Search for documents revised between ##/##/#### and ##/##/#### but will open Form 1 with the "DOCUMENT_NUM", "DOCUMENT_NAME", etc. However I cannot do the search since From 1 is bound to Table 1 and the information I am querying against is in Table 2.

The Search uses DoCmd.OpenForm "Documents",,,strQuery where strQuery = "1=1 AND [SOPS].[SOP_NUMBER] = 'QA-001' AND [SOP_REVISIONS].[REVISION_DATE] >= #12/02/2011# AND [SOP_REVISIONS].[REVISION_DATE] <= #12/02/2012#"

([SOPS] == Table 1 && [SOP_REVISIONS] == Table 2)

like image 547
B-Ray Avatar asked Dec 02 '25 09:12

B-Ray


1 Answers

How about:

strQuery = "1=1 AND [SOPS].[SOP_NUMBER] = 'QA-001' " _
    & "AND DOCUMENT_NUM IN (" _
    & "   SELECT DOCUMENT_NUM FROM [SOP_REVISIONS] " _
    & "   WHERE [SOP_REVISIONS].[REVISION_DATE] >= #12/02/2011# " _
    & "   AND [SOP_REVISIONS].[REVISION_DATE] <= #12/02/2012#)"

In other words, use a subquery to get the related DOCUMENT_NUM from [SOP_REVISIONS].

like image 102
Fionnuala Avatar answered Dec 04 '25 03:12

Fionnuala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!