【发布时间】:2015-04-21 18:26:39
【问题描述】:
在查询中,我有一个包含太多 iif 的 SQL iif 语句,因此我无法添加更多 iif,这是一个问题。
为了解决这个问题,我有编写 VBA 函数的想法,但我遇到了困难。这是我所拥有的,有一个简单的例子,我们在一个字段中有一个数字。如果数字Retrive()应该检索字段TheDate的值,如果>0,函数应该检索字段TheOtherDate的值:
Public Function Retrive(NumberToCheck As Integer) As Date
Dim db As Database
Dim r As Recordset
Dim rsCount As Integer
Dim TheDate As Field, TheOtherDate As Field
Dim i As Integer
Set db = CurrentDb()
Set r = db.OpenRecordset("Table")
Set TheDate = r.Fields("TheDate")
Set TheOtherDate = r.Fields("TheOtherDate")
rsCount = r.RecordCount
r.MoveFirst
For i = 1 To rsCount
If NumberToCheck < 0 Then
Retrive = TheDate.Value
End If
If NumberToCheck > 0 Then
Retrive = TheOtherDate.Value
End If
r.MoveNext
Next i
End Function
但这不起作用,因为它检索每一行的最后一条记录,而不是正确的行。
【问题讨论】:
-
你能发布这个打算替换的 SQL 吗?
-
您可以使用 switch 命令代替 IIF,这更容易理解。SQL 查询中的 VBA 函数可能会降低您的性能。