简体   繁体   中英

Strange behavior of a calculated field in an MS Access query

This question is curious for me.

I've created the following function in VBA

Public Function ContajeDeMaterialAUnaFecha(DFechaDeContaje As String, _
                                            StrDescMatProveedor As String) As Single
Dim rst As DAO.Recordset
Dim rstResultado As DAO.Recordset
Dim strSQL As String
Dim sngUnidadesContadas As Single
Dim intCuentas As Integer
    strSQL = "SELECT materialproalbsub.nalbpro, materialproveedor.DESC_MAT_PROVEEDOR, " & _
                "materialproalbsub.TOTALCANTIDAD, materialproalbsub.precioud, materialproalbsub.falbaran " & _
                "FROM materialproveedor INNER JOIN materialproalbsub ON materialproveedor.idmaterialemp = " & _
                "materialproalbsub.idmaterialemp " & _
                "ORDER BY materialproalbsub.falbaran;"

    Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot, dbSeeChanges)

    rst.Filter = "materialproalbsub.falbaran<=#" & DFechaDeContaje & "# " & _
            "AND materialproveedor.DESC_MAT_PROVEEDOR='" & StrDescMatProveedor & "'"

    Set rstResultado = rst.OpenRecordset

    If rstResultado.RecordCount > 0 Then

        intCuentas = 0
        sngUnidadesContadas = 0

        rstResultado.MoveLast
        rstResultado.MoveFirst

            Debug.Print rstResultado.RecordCount, StrDescMatProveedor

            Do Until rstResultado.EOF

                sngUnidadesContadas = sngUnidadesContadas + rstResultado.Fields("TOTALCANTIDAD")

                rstResultado.MoveNext

                intCuentas = intCuentas + 1

            Loop

        Debug.Print DFechaDeContaje, sngUnidadesContadas

        ContajeDeMaterialAUnaFecha = sngUnidadesContadas

    End If

    rst.Close
    Set rst = Nothing

    rstResultado.Close
    Set rstResultado = Nothing

End Function

That function is called in a Query from Access, as that (in SQL mode):

SELECT materialproalbsub.nalbpro, materialproveedor.DESC_MAT_PROVEEDOR, materialproalbsub.TOTALCANTIDAD, materialproalbsub.precioud, materialproalbsub.falbaran, ContajeDeMaterialAUnaFecha([falbaran],[materialproveedor]![DESC_MAT_PROVEEDOR]) AS TotalesAFecha
FROM materialproveedor INNER JOIN materialproalbsub ON materialproveedor.idmaterialemp = materialproalbsub.idmaterialemp
WHERE (((materialproveedor.DESC_MAT_PROVEEDOR)<>"- Seleccione uno de la lista -"))
ORDER BY materialproalbsub.falbaran;

Function works fine, and also the Query from MS Access, but there is a serious problem: the query is constantly recalculated once executed (any cell selection in the result, any scroll).

Is it possible to execute it only once?

Thanks in advance

The reason seems to be from your where clause

WHERE (((materialproveedor.DESC_MAT_PROVEEDOR)<>"- Seleccione uno de la lista -"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM