如何在MS Access中安装功能MROUND?

此功能MRound在MS access中不存在,您不能只安装它。

我正在尝试在access中使用mround函数 报告。该函数未在表达式中列出 内置函数的构建器框。当我使用 计算,例如=mround([AmountDue],0.05),以及 运行报告,它要求一个参数mround

nanersan 回答:如何在MS Access中安装功能MROUND?

您可以在公共VBA模块中定义自己的对象,例如:

Function MRound(dblNum As Double,dblMtp As Double) As Double
    MRound = dblMtp * Round(dblNum / dblMtp,0)
End Function

或者,将对Microsoft Excel对象库的引用添加到您的VBA项目(工具>引用),然后在公共VBA模块中将MRound函数定义为:

Function MRound(dblNum As Double,dblMtp As Double) As Double
    MRound = Excel.WorksheetFunction.MRound(dblNum,dblMtp)
End Function
,

Access中没有MRound,因此请创建自己的舍入函数。

但是,切勿为此使用Round,因为它臭名昭著的越野车。因此,请使用普通数学和数据类型 Decimal 以避免错误:

' Rounds a value by 4/5 to the nearest multiplum of a rounding value.
' Accepts any value within the range of data type Currency.
' Mimics Excel function MRound without the limitations of this.
'
' Examples:
'
'   RoundAmount(-922337203685477.5808,0.05)    -> -922337203685477.6
'   RoundAmount( 922337203685477.5807,0.05)    ->  922337203685477.6
'   RoundAmount( 10,3)                         ->                9
'   RoundAmount(-10,-3)                         ->               -9
'   RoundAmount( 1.3,0.2)                      ->                1.4
'   RoundAmount( 122.25,0.5)                   ->              122.5
'   RoundAmount( 6.05,0.1)                     ->                6.1
'   RoundAmount( 7.05,0.1)                     ->                7.1

' 2009-05-17. Gustav Brock,Cactus Data ApS,CPH.
'
Public Function RoundAmount( _
    ByVal Value As Currency,_
    ByVal RoundValue As Currency) _
    As Variant

    Dim BaseValue   As Variant
    Dim Result      As Variant

    BaseValue = Int(Value / CDec(RoundValue) + CDec(0.5))
    Result = BaseValue * RoundValue

    RoundAmount = Result

End Function

例如,此函数将正确舍入:

Amount = RoundAmount( 122.25,0.5)
Amount -> 122.50

有关精度舍入的更多信息,请参考我在 GitHub 上的项目:

VBA.Round

和此处引用的文章。

本文链接:https://www.f2er.com/3170059.html

大家都在问