问题:我有一个直接链接到本地表的分组MS Access 2010报告。它每个记录有40个“绑定”字段,每组有152个计算字段。当我的vba脚本试图自动更新最后几个计算字段的控制源时(在运行时),它会抛出“查询太复杂”的错误!
背景 我正在创建一个直接链接到本地表的MS Access报表,该表包含Person字段/列,Project字段/列以及每个记录的38个数字字段/列。
报告按人员分组,每个人都有多个项目记录。
在每个人的组页脚中,我在38个数字字段/列的每个下面有4个计算控件。
总而言之,我每个记录总共有40个“绑定”/直接链接字段,每组有152个计算字段。
报告直接绑定到本地表,该表定期删除并通过可绑定查询重新创建,该查询绑定到动态SQL查询,该查询根据今天的日期输出不同的列。为了说明动态查询将提供的不同字段名称,我有一些VBA脚本执行与服务器上相同的计算(计算今天的日期-11个月和+6个月之间的所有月份以及匹配的格式预期的列名称,然后更改相关报告字段和列标签标题的控制源,以匹配来自动态查询的表的预期结果。
除了我最后几行代码更新组页脚中的一些计算字段外,这一切似乎都很有效。以下是脚本的工作部分(来自报告的On Open事件),所有这些部分似乎都正常工作,仅适用于上下文:
Dim NowDate As Date
Dim i, monthcalc As Integer
Dim Dates(1 To 18) As String
NowDate = Now()
monthcalc = -11
'The below creates variables of formatted dates to be used as control sources for the expected fields incoming from SQL
For i = 1 To 18
Dates(i) = Year(DateAdd("m", monthcalc, NowDate)) & Format(month(DateAdd("m", monthcalc, NowDate)), "00")
monthcalc = monthcalc + 1
Next i
monthcalc = -11
'The below sets the column captions to match the expected fields incoming from SQL
For i = 1 To 18
Me.Controls("Date" & Trim(Str(i))).Caption = MonthName(month(DateAdd("m", monthcalc, NowDate))) & " " & Year(DateAdd("m", monthcalc, NowDate))
monthcalc = monthcalc + 1
Next i
'The below sets the dynamic 'bound' numeric fields to match the expected fields incoming from SQL
For i = 1 To 18
Me.Controls("Date" & Trim(Str(i)) & "P").ControlSource = Dates(i) & " P"
Me.Controls("Date" & Trim(Str(i)) & "TS").ControlSource = Dates(i) & " TS"
Next i
'The below sets the first calculated column beneath each numeric field
For i = 1 To 18
Me.Controls("TotalDate" & Trim(Str(i)) & "P").ControlSource = "=Sum([" & Dates(i) & " P])"
Me.Controls("TotalDate" & Trim(Str(i)) & "TS").ControlSource = "=Sum([" & Dates(i) & " TS])"
Next i
现在有问题的代码(注意循环中的三行被注释掉):
For i = 1 To 18
Me.Controls("TotalPHrsPDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([Project] Like 'AS#*' Or [Project] Like 'ABG#*' Or [Project] Like 'ADG#*' Or [Project] Like 'AE#*' Or [Project] Like 'AI#*' Or [Project] Like 'AIN#*' Or [Project] Like 'AM#*' Or [Project] Like 'IMP#*',[" & Dates(i) & " P]), 0))"
' Me.Controls("TotalPHrsTSDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([Project] Like 'AS#*' Or [Project] Like 'ABG#*' Or [Project] Like 'ADG#*' Or [Project] Like 'AE#*' Or [Project] Like 'AI#*' Or [Project] Like 'AIN#*' Or [Project] Like 'AM#*' Or [Project] Like 'IMP#*',[" & Dates(i) & " TS], 0))"
' Me.Controls("TotalNPHrsPDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([Project] Not Like 'AS#*' And [Project] Not Like 'ABG#*' And [Project] Not Like 'ADG#*' And [Project] Not Like 'AE#*' And [Project] Not Like 'AI#*' And [Project] Not Like 'AIN#*' And [Project] Not Like 'AM#*' And [Project] Not Like 'IMP#*',[" & Dates(i) & " P],0))"
' Me.Controls("TotalNPHrsTSDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([Project] Not Like 'AS#*' And [Project] Not Like 'ABG#*' And [Project] Not Like 'ADG#*' And [Project] Not Like 'AE#*' And [Project] Not Like 'AI#*' And [Project] Not Like 'AIN#*' And [Project] Not Like 'AM#*' And [Project] Not Like 'IMP#*',[" & Dates(i) & " TS],0))"
Next i
我只能在循环中取消注释上述任何一行,只要我尝试运行其中的两个或更多行,就会在我尝试打开报告时抛出“查询过于复杂”错误。
我尝试在单独的循环中运行它们,但引发了同样的错误。这些是最后几行代码,因此我可能在一行运行后遇到某种限制。
有关修复的任何建议吗?
我考虑过移动小组总数和&计算到子报表,但是我可以将其包含在主报表组页脚中,以仅显示该组的总计吗?我打的任何限制会受到子报告的影响吗?
答案 0 :(得分:5)
但你真正需要做的是简化。
拿你的问题代码。您是否真的需要在整个基础记录集上对该复杂IIf()
语句进行72次求和才能得到答案?否。
我的修复:在表格中添加一个名为ProjectType
的数字字段。作为刷新的一部分,运行如下查询:
UPDATE MyLocalTable
SET [ProjectType] = IIf([Project] Like 'AS#*' Or [Project] Like 'ABG#*'
Or [Project] Like 'ADG#*' Or [Project] Like 'AE#*' Or [Project] Like 'AI#*'
Or [Project] Like 'AIN#*' Or [Project] Like 'AM#*' Or [Project] Like 'IMP#*',1, 0)
现在,你做了两件重要的事情:
IIf()
语句,使您的代码更易于理解和维护现在您的问题代码看起来更像是这样:
For i = 1 To 18
Me.Controls("TotalPHrsPDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([ProjectType]=1,[" & Dates(i) & " P]), 0)"
Me.Controls("TotalPHrsTSDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([ProjectType]=1,[" & Dates(i) & " TS], 0)"
Me.Controls("TotalNPHrsPDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([ProjectType]=0,[" & Dates(i) & " P],0))"
Me.Controls("TotalNPHrsTSDate" & Trim(Str(i))).ControlSource = "=Sum(IIf([ProjectType]=0,[" & Dates(i) & " TS],0))"
Next i
更加简洁。而且更有可能不会抛出错误。您可以采用这个想法,并从此代码中消除更多复杂性。