从中找出一个百分比并减去一个百分比

时间:2018-01-31 10:57:00

标签: ms-access access-vba ms-access-2010

我有一行简单的代码可以正常工作,如下所示:

SELECT *
FROM @Aanbest AS a
INNER JOIN @Attachments AS at ON a.AanbestID=at.AanbestID

我稍微修改了它,从它找到的数字中扣除了2%,如下所示(请注意以下是那个不起作用的那个:

代码:

If Availability >= DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(0, 176, 80) 'green

我也尝试过:

If Availability < DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'") - (Format(2, "percent")) Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red

表TBL_RAG数据已存储为%。

任何想法我可能做错了

3 个答案:

答案 0 :(得分:1)

你的第二次尝试是正确的,但也许DLookup会返回Null?如果是这样的话:

If Availability < Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0) - 0.02 Then 
    RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If

或者你使用的是Double而不是Currency。然后:

If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then 
    RAGAvailability.ForeColor = RGB(192, 0, 0) 'red
End If

修改: 要来回改变颜色:

Dim Color As Long

If Availability < CCur(Nz(DLookup("[Availability]", "[tbl_RAG]", "[Department] = 'Outbound'"), 0)) - CCur(0.02) Then 
    Color = vbRed
Else
    Color = vbBlack
End If
RAGAvailability.ForeColor = Color

答案 1 :(得分:0)

您可以在DLookUp内使用微积分。

尝试以下方法:

If Availability < DLookup("[Availability]-0.02", "[tbl_RAG]", "[Department] = 'Outbound'") Then RAGAvailability.ForeColor = RGB(192, 0, 0) 'red

答案 2 :(得分:-1)

感谢您的帮助。我似乎已使用以下代码更正了问题:

Dim db As Database, AvailRs As Recordset
Set db = CurrentDb
Set AvailRs = db.OpenRecordset("Select * from tbl_RAG")

Select Case Availability
    Case Is > Format(AvailRs!UpperAvailability, "percent")
        RAGAvailability.ForeColor = RGB(0, 176, 80)
    Case Is < Format(AvailRs!lowerAvailability, "percent")
        RAGAvailability.ForeColor = RGB(192, 0, 0)
    Case Else
        RAGAvailability.ForeColor = RGB(255, 192, 0)
End Select

发生了什么: 我的理解是代码无法从 Available 中读取字符串。我将其格式化为百分比,并使用Select Case

将其组合在一起