在动态报告(MS Access)中创建组级别

时间:2019-02-25 14:58:20

标签: ms-access

我需要创建动态(动态)报告,因为字段的数量和名称会经常更改。除了无法弄清楚如何根据字段名称创建组标题之外,我已经全部解决了。

这是我的第一次尝试,返回“您用于引用表单或报告部分的数字无效。”

Dim rpt as Report
dim txtNew as Access.Textbox

set rpt = CreateReport

With rpt
.Width = 8500
.RecordSource = "IS_Subscales_GB_Final"
End With

Set txtNew = CreateReportControl(rpt.Name, acTextBox, acGroupLevel1Header, , "FriendlyName01", 0, 0)
txtNew.FontBold = True
txtNew.FontSize = 16
txtNew.SizeToFit

DoCmd.OpenReport rpt.Name, acViewPreview

因此,我接下来尝试使用CreateGroupLevel,但这会返回错误“在打开“组,排序和总计窗格”时,您无法调用此函数。”

Dim rpt as Report
dim txtnew as Access.Textbox
Dim vargrplvl As Variant

set rpt = CreateReport

With rpt
.Width = 8500
.RecordSource = "IS_Subscales_GB_Final"
End With

vargrplvl = CreateGroupLevel(rpt.Name, "FriendlyName", True, False)
rpt.Section(acGroupLevel1Header).Height = 400    

Set txtNew = CreateReportControl(rpt.Name, acTextBox, acGroupLevel1Header, , "FriendlyName01", 0, 0)
txtNew.FontBold = True
txtNew.FontSize = 16
txtNew.SizeToFit

DoCmd.OpenReport rpt.Name, acViewPreview

任何指导将不胜感激。

1 个答案:

答案 0 :(得分:1)

我犯了多个错误,但是Access有一个古怪的地方,它会意外引发2451错误(“打开“组,排序和总计窗格”时,您无法调用此函数。”)-仅发生这种情况如果上次您是在“设计”视图中查看报告的,则打开“组”,“排序”和“总窗格”。 Access可保存该设置,因此您需要在“设计”视图中返回报表并关闭面板,或者使用错误处理程序来解决该问题。因此,这是一个来自整个磨难的学习课程:

Dim rpt as Report
Dim vargrplevel As Variant 'holds grouping level of report
Dim txtNew as Access.Textbox ' textbox control
Dim lblNew As Access.Label ' label control

set rpt = CreateReport   'creates a report object
with rpt
.width = 8500 'sets width of report
.RecordSource = "IS_Subscales_GB_Final" ' your table or query
end with

'start setting your controls on the report using CreateReportControl, i.e.:

Set lblNew = CreateReportControl(rpt.Name, acLabel, acDetail, , , 2700, 0, 270, 315)
lblNew.Caption = "O"
lblNew.FontSize = 12
lblNew.FontBold = True

'To add a group level to the report, it MUST be in acViewDesign!
DoCmd.OpenReport rpt.Name, acViewDesign 

On Error GoTo ErrorHandler 
'if the Group, Sort, and Total pane was left "on" then this next line will throw error 2451
vargrplevel = CreateGroupLevel(rpt.Name, "FriendlyName01", True, False) 'Creates a group header, named "FriendlyName01"
rpt.Section(acGroupLevel1Header).Height = 400 'optional; sets the header height

Set txtNew = CreateReportControl(rpt.Name, acTextBox, acGroupLevel1Header, , "FriendlyName01", 0, 0) 'This actually inserts the field into the header as textbox control
txtNew.FontBold = True
txtNew.FontSize = 16
DoCmd.Save acReport, rpt.Name

Exit Sub

ErrorHandler:
If Err.Number = 2154 Then
    RunCommand acCmdSortingAndGrouping 'turns off the Sorting and Grouping pane
    Resume
Else
    Debug.Print "Error in creating report header (EH01- " & Err.Number & ")"
    Exit Sub
End If

干杯。