首页 > 解决方案 > 在计算字段中查找最小数字

问题描述

在 MSAccess 报告中,我有以下字段:CurrentHours、Insp1DueTime、Insp2DueTime...Inspn3DueTime 等。我想要一个计算字段,输出 (Insp1DueTime-CurrentHours)、(Insp2DueTime-Currenthours)、(Insp3DueTime-CurrentHours) 的最小值, ETC。

是否有一个 VBA 命令可以执行此操作,类似于' minimumvalue '((currenthours-insp1duetime), (Currenthours-insp2duetime),...(currenthour-InspnDueTime))

标签: vbams-access

解决方案


Nope, but you can make one really easily, using a ParamArray:

Public Function SmallestValue(ParamArray Values())
    Dim Value
    For Each Value In Values
        If IsEmpty(SmallestValue) Or SmallestValue > Value Then
            SmallestValue = Value
        End If
    Next
End Function

A paramarray takes any input, and puts it into an array. You iterate over that array, and test if things are larger or smaller.

This works with any input, e.g.: SmallestValue(2,1,3,4) returns 1, SmallestValue(Now(), Date(), #1/1/2001#) returns #1/1/2001#, SmallestValue("banana", "apple", "pear") returns "apple"


推荐阅读