根据用户表单中用户的一些输入,我构建代码以根据定义的标准自动创建计算列。但是,我在字段表达式中收到语法错误,下面提供了代码。
Option Compare Database
Private Sub Calculate_Click()
Dim db As Database
Dim rs As Recordset
Dim x As Integer
Dim Months As Integer
Dim WPmonthly As String ' field name for monthly written premium
Dim runningDate As Date
Dim useDateLower As Date
Dim useDateUpper As Date
Dim tdf As dao.TableDef
Dim fld As dao.Field2
Months = Me.YearsBack * 12 + Month(Me.ValDate)
If Me.Period = "monthly" Then
Set db = CurrentDb
Set tdf = db.TableDefs("tblEPdata")
For x = 1 To Months
runningDate = Format(DateAdd("m", -x + 1, Me.ValDate), "mm yyyy")
useDateLower = runningDate
useDateUpper = Format(DateAdd("m", -x + 2, Me.ValDate), "mm yyyy")
WPmonthly = "WP M" & Month(runningDate) & " " & Year(runningDate)
Set fld = tdf.CreateField(UPRmonthly)
fld.Expression = "iif([issuedate]<#" & useDateUpper & "#,iif([issuedate]>=#" & useDateLower & "#,[grossPremium]))" ' output gross premium if issue date is between usedateupper and usedatelower, otherwise 0
tdf.Fields.Append fld
Next
MsgBox "added"
End If
End Sub
答案 0 :(得分:0)
iif
没有足够的参数 - 它需要三个参数(参见here):
IIf ( expr , truepart , falsepart )
根据您在代码中的评论判断,您需要:
"iif([issuedate]<#" & useDateUpper & "#,iif([issuedate]>=#" & useDateLower & "#,[grossPremium],0),0)"
或者你可以简化这个,例如到:
"iif([issuedate]<#" & useDateUpper & "# and [issuedate]>=#" & useDateLower & "#,[grossPremium],0)"
答案 1 :(得分:0)
想出来。变量wpmonthly应该用于使用uprmonthly的地方。 uprmonthly没有定义。
干杯