R
Rob Parker
Hello all, especially the MVPs ;-)
First, a reason for the crosspost, for what I think is really a query
problem:
The query I'm having a serious problem with is the recordsource for a
report, which uses several of the fields in Sum textboxes in group and
report footers in the report, so I think I need to get all my data into a
single query, rather than using subreports. But if there's a solution to my
problem which uses sub-reports, please suggest it.
Now for the nitty-gritty (and apologies in advance for the long post), with
a general description first, and then a (simplified for this post) listing
of my current query:
My source data is financial data for a project. The project has Control
Accounts and Work Packages (CA and WP in the query shown below); each WP has
a budget (actually initial and revised budgets, but simplified here via a
query which produces only the original budget). The SQL for this query
(qryBudget) is:
SELECT WP, Budget
FROM tblBudget
WHERE tblBudget.IsOriginal=True;
The database contains data for orders against each WP in the project, with
Commit and Spend data for each order. This data is stored in tblOrders,
which contains a Dataset field (a long integer), which represents the
month/year of the project. Order data is "rolled over" each month via an
append query on this table, which copies the previous dataset's data to the
new dataset; all user input is done via forms which operate on the current
dataset's records. Essentially, this give multiple records in tblOrders for
every order for each month, so that the financial history can be tracked,
using Dataset to separate individual month's data.. Part of this tracking
is a Financial Summary for any month - the report which is based on my
troublesome query - which shows total Commit and Spend for each Work
Package, together with the original Budget figure. The report show data for
every WP, with grouping and summing by CA, and also summing by whole
project.
The monthly commit and spend totals for each Work Package are obtained from
a totals query (qryWP_Totals), with the following SQL:
SELECT tblOrdersWP, tblOrders.DataSet, Sum(tblOrders.Commit) AS
[Commit], Sum(tblOrders.Spend) AS Spend
FROM tblOrders
GROUP BY tblOrders.WP, tblOrders.DataSet;
So far, so good - nothing at all difficult yet, and all seems fine.
The query which I have as the recordsource of my report
(qryFinancialSummary) is:
SELECT tblWP.CA,
tblWP.WP,
tblWP.FullName AS [WP Name],
qryBudget.Budget,
qryWP_Totals.Commit,
qryWP_Totals.Spend,
qryWP_Totals.DataSet
FROM (tblWP LEFT JOIN qryWP_Totals ON tblWP.WP = qryWP_Totals.WP)
LEFT JOIN qryBudget ON tblWP.WP = qryBudget.WP;
The report is opened with the following WHERE condition:
WHERE (Dataset Is Null OR Dataset = Me.txtDataset)
(ie. the dataset for the report (month/year) is selected in the txtDataset
control in the form from which the report is opened.)
The problem I have is that, if a WP has any entries in tblOrders, then this
query drops the budget data (which does not have a Dataset associated with
it) from the output of the query for every dataset other than those for
which data exists in tblOrders (in practice, for all preceding datasets
before the first dataset in which data is present in tblOrders). If there
are no entries for the WP in tblOrders, the Budget figure appears in the
query output (with a null value in the Dataset field).
So, if a Financial Summary is report is generated for a month before that in
which the first order for a WP appears, the budget record for that WP
disappears from the query's output, and the CA and project budget totals in
the report are incorrect.
How can I fix this? General approach answers will be sufficient - I don't
need detailed SQL for the final query SQL.
TIA,
Rob
PS. For the curious: this database application has been in use for over
four years, without the problem being noticed. That's because usually the
Financial Summary report is normally generated for the preceding month
immediately after rolling over the order data to the current month, before
any changes to orders in the new month; however, on this occasion there was
about a two-week period before the reconciliation/financial summary process
occurred, during which the first order was added for a WP - and BAM!!! -
suddenly a WP had disappeared from the Financial Summary report for the
preceding month.
First, a reason for the crosspost, for what I think is really a query
problem:
The query I'm having a serious problem with is the recordsource for a
report, which uses several of the fields in Sum textboxes in group and
report footers in the report, so I think I need to get all my data into a
single query, rather than using subreports. But if there's a solution to my
problem which uses sub-reports, please suggest it.
Now for the nitty-gritty (and apologies in advance for the long post), with
a general description first, and then a (simplified for this post) listing
of my current query:
My source data is financial data for a project. The project has Control
Accounts and Work Packages (CA and WP in the query shown below); each WP has
a budget (actually initial and revised budgets, but simplified here via a
query which produces only the original budget). The SQL for this query
(qryBudget) is:
SELECT WP, Budget
FROM tblBudget
WHERE tblBudget.IsOriginal=True;
The database contains data for orders against each WP in the project, with
Commit and Spend data for each order. This data is stored in tblOrders,
which contains a Dataset field (a long integer), which represents the
month/year of the project. Order data is "rolled over" each month via an
append query on this table, which copies the previous dataset's data to the
new dataset; all user input is done via forms which operate on the current
dataset's records. Essentially, this give multiple records in tblOrders for
every order for each month, so that the financial history can be tracked,
using Dataset to separate individual month's data.. Part of this tracking
is a Financial Summary for any month - the report which is based on my
troublesome query - which shows total Commit and Spend for each Work
Package, together with the original Budget figure. The report show data for
every WP, with grouping and summing by CA, and also summing by whole
project.
The monthly commit and spend totals for each Work Package are obtained from
a totals query (qryWP_Totals), with the following SQL:
SELECT tblOrdersWP, tblOrders.DataSet, Sum(tblOrders.Commit) AS
[Commit], Sum(tblOrders.Spend) AS Spend
FROM tblOrders
GROUP BY tblOrders.WP, tblOrders.DataSet;
So far, so good - nothing at all difficult yet, and all seems fine.
The query which I have as the recordsource of my report
(qryFinancialSummary) is:
SELECT tblWP.CA,
tblWP.WP,
tblWP.FullName AS [WP Name],
qryBudget.Budget,
qryWP_Totals.Commit,
qryWP_Totals.Spend,
qryWP_Totals.DataSet
FROM (tblWP LEFT JOIN qryWP_Totals ON tblWP.WP = qryWP_Totals.WP)
LEFT JOIN qryBudget ON tblWP.WP = qryBudget.WP;
The report is opened with the following WHERE condition:
WHERE (Dataset Is Null OR Dataset = Me.txtDataset)
(ie. the dataset for the report (month/year) is selected in the txtDataset
control in the form from which the report is opened.)
The problem I have is that, if a WP has any entries in tblOrders, then this
query drops the budget data (which does not have a Dataset associated with
it) from the output of the query for every dataset other than those for
which data exists in tblOrders (in practice, for all preceding datasets
before the first dataset in which data is present in tblOrders). If there
are no entries for the WP in tblOrders, the Budget figure appears in the
query output (with a null value in the Dataset field).
So, if a Financial Summary is report is generated for a month before that in
which the first order for a WP appears, the budget record for that WP
disappears from the query's output, and the CA and project budget totals in
the report are incorrect.
How can I fix this? General approach answers will be sufficient - I don't
need detailed SQL for the final query SQL.
TIA,
Rob
PS. For the curious: this database application has been in use for over
four years, without the problem being noticed. That's because usually the
Financial Summary report is normally generated for the preceding month
immediately after rolling over the order data to the current month, before
any changes to orders in the new month; however, on this occasion there was
about a two-week period before the reconciliation/financial summary process
occurred, during which the first order was added for a WP - and BAM!!! -
suddenly a WP had disappeared from the Financial Summary report for the
preceding month.