Search code examples
excelvbaadodbaverage-precision

Wrong result with AVG() time when hour >24 in ADODB.Recordset


someone can help me to understand why, when I run this code I retreive wrong result (the sample table is an extract of 25K recod) each time when in the range of data we have records with Kpi1-2 > 24H.

enter image description here

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

DBPath = ThisWorkbook.FullName
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"";"    

Strsql:Select [DataAttesa] as Data, avg([Kpi1-2]) as nr FROM [db_In$] Where TypeTrasp  = 'STD' and [1-2_ESITO] in ('90%','100%','Out of Time','On Going') group by [DataAttesa] Order by [DataAttesa]asc

    objRecordset.Open strsql, objConnection, adOpenStatic, adLockReadOnly, adCmdUnspecified
       Do Until objRecordset.EOF
         debug.print objRecordset.Fields.item("nr")
         objRecordset.MoveNext
      Loop
    objRecordset.Close

UPDATE: I test this code:

debug.print objRecordset.Fields.item("nr")
in the immediate windows I see: 21/01/1900  11:41:00
if I test tu put the result into a cell:

Cells(20, 7) = objRecordset.Fields.item("nr") 
the cells.value will be 22/01/1900  11:41:25
Cells(21, 7) = Format(objRecordset.Fields.item("nr"), "dd/mm/yyyy hh:mm;@")
the cells.value will be 21/01/1900  11:41:25

Only the second it's correct. Why?!?! wath append?!?


Solution

  • the solution to this problem was discussed in this post: Difference time view from Excel to VBA

    I insert the link if it could help any other user

    fabrizio