简体   繁体   English

Access 2010 VBA Select Case 语句

[英]Access 2010 VBA Select Case Statement

I am trying to build a function where based on a string comparison use a specific formula我正在尝试构建一个函数,其中基于字符串比较使用特定公式

The problem I have is that I get a Byref arguement type mismatch error我遇到的问题是我收到了Byref 争论类型不匹配错误

It must be something to do with passing the string for the comparison and also the numbers for the formulas.它必须与传递用于比较的字符串以及公式的数字有关。 Could someone please take a look and help?有人可以看看并提供帮助吗?

Public Function MyRateCalc(rateType As String, fixedAmount As Long, minAmount As Long, rateDollar As Long, valPerc As Long, rtValue As Long) As Double

    Select Case rateType

        Case "A1"

            MyRateCalc = fixedAmount * valPerc

        Case "A"

            MyRateCalc = rtValue * rateDollar * valPerc

        Case "B", "C", "D", "H", "L", "N", "R"

            MyRateCalc = IIf(rtValue * rateDollar > minAmount, rtValue * rateDollar * valPerc, minAmount * valPerc)

        Case "M", "U", "MS"

            MyRateCalc = rtValue * rateDollar * valPerc

        Case Else

            MyRateCalc = 0

    End Select

End Function

The function itself is fine.功能本身没问题。

However the problem will be in the way you are calling the function.但是,问题在于您调用该函数的方式。

In VBA, function arguments are passed by reference ( ByRef ) by default.在 VBA 中,函数参数默认通过引用 ( ByRef ) 传递。 This means that the data types in the caller have to be exactly the same as those in the function.这意味着调用者中的数据类型必须与函数中的数据类型完全相同。

Two choices:两种选择:

1) Adjust your function to 1)调整你的功能

Public Function MyRateCalc(ByVal rateType As String, ByVal fixedAmount As Long, ByVal minAmount As Long, ByVal rateDollar As Long, ByVal valPerc As Long, ByVal rtValue As Long) As Double

2) Check the caller variable types carefully; 2) 仔细检查调用者变量类型; they must match exactly.它们必须完全匹配。

I prefer approach (1) as it increases program stability (functions cannot unintentionally modify variables in the calling code);我更喜欢方法 (1),因为它增加了程序稳定性(函数不能无意中修改调用代码中的变量); even though it's at the expense of a string copy in your case.即使在您的情况下以牺牲字符串副本为代价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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