Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / July 2006

Tip: Looking for answers? Try searching our database.

Code VS query gives different average

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steeler - 07 Jul 2006 17:33 GMT
I am getting the averages of a field LT. When I run it in the query
designer:

SELECT tbl_LTandWait.EngModule, Avg(tbl_LTandWait.LT) AS AvgOfLT,
Avg(tbl_LTandWait.LT_Mfg) AS AvgOfLT_Mfg
FROM tbl_LTandWait
WHERE (((tbl_LTandWait.EngSN)<>' Median LT' And
(tbl_LTandWait.EngSN)<>'   Lead Time Goal' And (tbl_LTandWait.EngSN)<>'
Avg Lead Time'))
GROUP BY tbl_LTandWait.EngModule
HAVING (((tbl_LTandWait.EngModule)='Horizontal'));

I get the AvgOfLT as 3.916 which is correct.

When I do it in code:

lsSQL = "SELECT tbl_LTandWait.EngModule, Avg(tbl_LTandWait.LT) AS
AvgOfLT, Avg(tbl_LTandWait.LT_Mfg) AS AvgOfLT_Mfg " & _
"FROM tbl_LTandWait " & _
"WHERE (((tbl_LTandWait.EngSN)<>' Median LT' And
(tbl_LTandWait.EngSN)<>'   Lead Time Goal' And (tbl_LTandWait.EngSN)<>'
Avg Lead Time')) " & _
"GROUP BY tbl_LTandWait.EngModule " & _
"HAVING (((tbl_LTandWait.EngModule)='Horizontal'))"

Set dbs = CurrentDb
Set rstMods = dbs.OpenRecordset(lsSQL)

MsgBox rstMods!AvgOfLT
Exit Sub
I get 3.57 which is wrong. It is exactly the same query whether in code
or query so I don't understand why I get different results.

Anbody have any ideas.

Thanks,
Steeler
Lyle Fairfield - 07 Jul 2006 18:08 GMT
> I am getting the averages of a field LT. When I run it in the query
> designer:
[quoted text clipped - 33 lines]
> Thanks,
> Steeler

I suppose you could save the query created in the query designer as,
say, "query2", or whatever and then use something like:

Dim isSQL As String
isSQL = DBEngine(0)(0).QueryDefs("query2").SQL
Debug.Print DBEngine(0)(0).OpenRecordset(isSQL)!AvgOfLT

to ensure that no syntax error has been made.
salad - 07 Jul 2006 18:22 GMT
> I am getting the averages of a field LT. When I run it in the query
> designer:
[quoted text clipped - 33 lines]
> Thanks,
> Steeler

It almost sounds like 1 query is returning more records than the other
when avg'ing.  Maybe make a copy and remove the grouping/avging thus
making it a Select query instead of a totals query and see if they both
return the same number of records.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.