我有一行简单的代码可以正常工作,如下所示:
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数据已存储为%。
任何想法我可能做错了
答案 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
将其组合在一起