本文中的範例程式代碼使用 Microsoft 資料存取物件。 若要讓此程式代碼正常執行,您必須參考 Microsoft DAO 3.6 物件庫。 若要這樣做,請按兩下Visual Basic 編輯器 中 [工具] 選單上的 [參考],並確定已選取 [Microsoft DAO 3.6 物件庫] 複選框。
Dim txtNew As Access.TextBox
Dim labNew As Access.Label
Dim lngTop AsLongDim lngLeft AsLongDim lblCol AsLongDim rpt As Report
Dim reportQuery AsStringDim rs As DAO.Recordset
Dim i AsIntegerDim prevColwidth Aslong
lngLeft = 0
lngTop = 0' Open the report to design.' To make changes in the number of columns that appear at run time.
DoCmd.OpenReport "AccessColumnBuilder", acViewDesign
Set rpt = Reports![AccessColumnBuilder]
' Change the number of columns required as per your requirement.
reportQuery = "SELECT FirstName, LastName FROM Employees"' Open the recordset.Set rs = CodeDb().OpenRecordset(reportQuery)
' Assign the query as a record source to report control.
rpt.RecordSource = reportQuery
' Set the value to zero so that the left margin is initialized.
prevColwidth = 0
lblCol = 0' Print the page header for the report.For i = 0To rs.Fields.Count - 1Set labNew = CreateReportControl(rpt.Name, acLabel, acPageHeader, _
, rs.Fields(i).Name, lblcol, , , lngTop)
labNew.SizeToFit
lblCol = lblCol + 600 + labNew.Width
Next' Create the column depending on the number of fields selected in reportQuery.' Assign the column value to new created column.For i = 0To rs.Fields.Count - 1' Create new text box control and size to fit data.Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
acDetail, , , lngLeft + 15 + prevColwidth, lngTop)
txtNew.SizeToFit
txtNew.ControlSource = rs(i).Name
' Modify the left margin depending on the number of columns' and the size of each column.
prevColwidth = prevColwidth + txtNew.width
Next'To save the modification to the report, uncomment the following line of code:'DoCmd.Save' View the generated report.
DoCmd.OpenReport "AccessColumnBuilder", acViewPreview
' This opens the report in preview.
儲存然後執行表單。
若要預覽報表,請按下您在步驟 10 中新增的命令按鈕。
下列記錄會出現在第一頁:
主控台
First NameLast Name
NancyDavolio
MargaretPeacock
...............
If Me![PageBreak].Visible = True Then
Me![TotalPrice].Visible = True
Me![TotalPrice_label].Visible = True
Else
Me![TotalPrice].Visible = False
Me![TotalPrice_label].Visible = False
End If
發生 PageBreak 時,[TotalPrice] 數據行隨即出現。
預覽報表。 下列記錄會出現在第一頁:
主控台
Product NameUnit PriceUnit in Stock
Cahi$18.0039
Chang$19.0017
注意
報表的第一頁包含三個數據行。
下列記錄會出現在第二頁:
主控台
Product NameUnit PriceUnit in StockTotal Amount
Aniseed Syrup $10.0013 130
Chef A... $22.00 53 1166
Do you want to learn how to add a layout to a report in Microsoft Dynamics 365 Business Central? In this module, you'll learn about the client report definition (RDLC) layout and Word layout formats and how to create them.