Access VBADiscuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
I am having difficulty creating some business statistics reports - it seems very simple but I just can't figure it out. Here is what I am trying to do:
I have the following tables:
TABLE 1
Field 1: PrimaryKeyField
Field 2: Date (short date format)
TABLE 2
Field 1: TotalSalesForTheMonth (Euros)
Field 2: TotalNumberOfClientsForTheMonth (DoubleNumber)
Field 3: FieldToLinkToTable1PrimaryKey
My actual database has many more tables that are related to TABLE 1 and basically it is a database where I introduce numerous monthly business statistics. The below is an example of what the data would look like (business stats for the first three months of 2009 and 2010):
The difficulty I am having is in creating three different types of reports - I have an unbound form in which a user will select the month he wants the report for):
The idea appears so simple but I have not been able to figure it out - tried playing with PivotTables and CrossTab Queries, but nothing I try seems to work. Maybe the way I have structured my tables is not correct? Maybe the way I am using the date field is not correct? Can anyone guide me in the right direction?
SELECT "NumberDaysMonth" AS Category, StatisticDate AS MeasurementDate, NumberDaysMonth AS Measurement
FROM tblStatisticDate INNER JOIN tblStatistics ON tblStatisticDate.StatisticDateID = tblStatistics.StatisticDateID
UNION SELECT "NumberSeatsAvailableRestaurantMeal" AS Category, StatisticDate AS MeasurementDate, NumberSeatsAvailableRestaurantMeal AS Measurement
FROM tblStatisticDate INNER JOIN tblStatistics ON tblStatisticDate.StatisticDateID = tblStatistics.StatisticDateID
UNION SELECT "NumberSeatsAvailableTerraceMeal" AS Category, StatisticDate AS MeasurementDate, NumberSeatsAvailableTerraceMeal AS Measurement
FROM tblStatisticDate INNER JOIN tblStatistics ON tblStatisticDate.StatisticDateID = tblStatistics.StatisticDateID
UNION SELECT "NumberSeatsAvailableBarMeal" AS Category, StatisticDate AS MeasurementDate, NumberSeatsAvailableBarMeal AS Measurement
FROM tblStatisticDate INNER JOIN tblStatistics ON tblStatisticDate.StatisticDateID = tblStatistics.StatisticDateID
UNION SELECT "NumberRoomsAvailableMonth" AS Category, StatisticDate AS MeasurementDate, NumberRoomsAvailableMonth AS Measurement
FROM tblStatisticDate INNER JOIN tblStatistics ON tblStatisticDate.StatisticDateID = tblStatistics.StatisticDateID;
And then I created the CROSSTAB QUERY based on the above UNION QUERY:
TRANSFORM First(BasicStatistics01.Measurement) AS FirstOfMeasurement
SELECT BasicStatistics01.Category
FROM BasicStatistics01
WHERE (((Format([MeasurementDate],"yyyy"))=2010))
GROUP BY BasicStatistics01.Category
ORDER BY Format([MeasurementDate],"mmmm yyyy")
PIVOT Format([MeasurementDate],"mmmm yyyy");
Regarding the solution, there is one minor issue I am trying to overcome with the CROSSTAB QUERY:
TRANSFORM First(BasicStatistics01.Measurement) AS FirstOfMeasurement
SELECT BasicStatistics01.Category
FROM BasicStatistics01
WHERE (((Format([MeasurementDate],"yyyy"))=2010))
GROUP BY BasicStatistics01.Category
ORDER BY Format([MeasurementDate],"mmmm yyyy")
PIVOT Format([MeasurementDate],"mmmm yyyy");
Whenever I change the year, for example, from 2010 to 2009, the report adjusts to show me the months for that specific year. But if I change that line to:
WHERE (((Format([MeasurementDate],"yyyy"))=[Forms]![frmSelectYear]![SelectYear]))
so that it would be equal to the year inputted in a separate form, I get the following error:
"The Microsoft Office Access database engine does not recognise '[Forms]![frmSelectYear]![SelectYear]' as a valid field name or expression."
The field in the separate form is unbound and is configured as such:
SELECT DISTINCT Format([StatisticDate],"yyyy") AS SelectYear
FROM tblStatisticDate
ORDER BY Format([StatisticDate],"yyyy") DESC;