我在Access 2010中有一个Sub,它应该在三列之间拉出最低值。问题是它似乎没有阅读第一列。我已经检查了一千次拼写,我不知道还有什么可能错的。任何建议都将无法理解。
Private Sub UpdatePriority_Click()
Const Superior As Long = 9999
Dim MinGeoPri As Variant
Dim MinStrPri As Variant
Dim MinSOPri As Variant
MinGeoPri = Nz(DMin("[GeoPri]", "[Projects]", "Projects.ProjNo = Activity.ProjNo"), Superior)
MinStrPri = Nz(DMin("[StrPri]", "[Projects]", "Projects.ProjNo = Activity.ProjNo"), Superior)
MinSOPri = Nz(DMin("[SOPri]", "[Projects]", "Projects.ProjNo = Activity.ProjNo"), Superior)
Overall_Priority.Requery
Overall_Priority = IIf(((IIf([MinStrPri] < [MinGeoPri], [MinStrPri], [MinGeoPri]))) < [MinSOPri], ((IIf([MinStrPri] < [MinGeoPri], [MinStrPri], [MinGeoPri]))), [MinSOPri])
End Sub
答案 0 :(得分:1)
您的代码存在一些问题。
1)我不知道Activity.ProjNo
来自何处,但DMin调用可能应该是:
DMin("[GeoPri]", "[Projects]", "Projects.ProjNo = " & Activity.ProjNo)
使用WHERE字符串之外的参数。
2)如果你之后设置相同的(字段?变量?),Overall_Priority.Requery
应该做什么?
3)你的IIf声明是一场噩梦,我很确定括号有问题。 :)
好多了:使用VBA Min()函数,如下所示:http://www.mrexcel.com/forum/excel-questions/132404-max-min-visual-basic-applications.html
Public Function Min(ParamArray values() As Variant) As Variant
Dim minValue As Variant, Value As Variant
minValue = values(0)
For Each Value In values
If Value < minValue Then minValue = Value
Next
Min = minValue
End Function
ParamArray非常适合这类事情(参数数量未知)。
然后:
Overall_Priority = Min(MinGeoPri, MinStrPri, MinSOPri)
编辑:测试功能
Public Sub MyMinTest()
Debug.Print Min(47, 11, 88)
End Sub