Kostas,
It looks like what you need to do is an aggregate query where you Group by
the ComYear field. By the looks of your WHERE clause, and your looping
through the various years, it looks like you are attempting to generate
statics that relate to each of the years, but only include the records for
each year (ComYear) where the EventEndDate (is this field a string in the
format 'YYYYMMDD'?) is in the year following the ComYear. Is that
assessment correct?
The following query might meet your needs, and prevent you from having to
run this 17 times and concatenate the results. Try a SQL statement that
looks like:
SELECT Kam.ComYear,
Count (CountryCode) AS [VNB],
Avg(KAM.[Loa]) AS [ML],
StDev(KAM.[Loa]) AS [SDL],
Sum(KAM.[Ton Ref]) AS [SumGT],
Avg(KAM.[Power Main]) AS [MKW],
StDev(KAM.[Power Main]) AS [SDKW],
Avg(KAM.[Construction Year]) AS [MCY],
StDev(KAM.[Construction Year]) AS [SDCY]
FROM [KAM Prefecture] INNER JOIN
([KAM Port] INNER JOIN KAM
ON ([KAM Port].[Port Name] = KAM.[Port Name])
AND ([KAM Port].[Port Code] = KAM.[Port Code]))
ON [KAM Prefecture].Prefecture = [KAM Port].Prefecture
WHERE KAM.ComYear BETWEEN 1991 AND 2007
AND VAL(LEFT(KAM.EventEndDate, 4)) > [KAM].ComYear
GROUP BY KAM.ComYear
Now here is where you lose me. What are 'sport', 'sgear', 'slow', and 'shi'
values? If you are confident that these values contribute to the WHERE
clause, then by all means, insert them into the above query.
HTH
Dale
KostasP via AccessMonster.com said:
Well thanks for the advice but the problem is that i can't get with a
query
the amount of values that i want (1991 to 2007) but only a year's values.
So
i had to use 17 queries (each per year). That led me to make the following
code:
For nb = 1991 To 2007
sq = "SELECT Count (CountryCode) AS [VNB], "
sq = sq + "Avg(KAM.[Loa]) AS [ML], "
sq = sq + "StDev(KAM.[Loa]) AS [SDL], "
sq = sq + "Sum(KAM.[Ton Ref]) AS [SumGT],"
sq = sq + "Avg(KAM.[Power Main]) AS [MKW], "
sq = sq + "StDev(KAM.[Power Main]) AS [SDKW], "
sq = sq + "Avg(KAM.[Construction Year]) AS [MCY], "
sq = sq + "StDev(KAM.[Construction Year]) AS [SDCY], "
sq = sq + "" + Str(nb) + " As [Year]"
sq = sq + "FROM [KAM Prefecture] INNER JOIN ([KAM Port] INNER JOIN
KAM ON ([KAM Port].[Port Name] = KAM.[Port Name]) "
sq = sq + "AND ([KAM Port].[Port Code] = KAM.[Port Code])) ON [KAM
Prefecture].Prefecture = [KAM Port].Prefecture"
sq = sq + " WHERE "
sq = sq + sport
sq = sq + "AND KAM.ComYear<=" + Str(nb) + " AND KAM.EventEndDate>="
+
Str(nb) + "9999"
sq = sq + sgear
sq = sq + " AND KAM.Loa>=" + Str(slow) + "AND KAM.Loa<=" + Str(shi)
Set qst = dbs.OpenRecordset(sq)
sloaAvg = sloaAvg + ", " + Str(qst!ML)
sloaSD = sloaSD + "," + Str(qst!SDL)
sPowerAvg = sPowerAvg + "," + Str(qst!MKW)
sPowerSD = sPowerSD + "," + Str(qst!SDKW)
sConYear = sConYearSD + "," + Str(qst!MCY)
sConYearSD = sConYearSD + "," + Str(qst!SDCY)
Next nb
Right now i export these values ( sloaAvg , sloaSD e.c.t) on a form's a
textbox but it it hard for me to manipulate the values. I have to copy
them
to an excel and then make charts and everything.
Any suggestions?