用VBA创建动态报表-Big Young
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-报表


用VBA创建动态报表

发表时间:2017/10/28 21:21:18 评论(0) 浏览(12560)  评论 | 加入收藏 | 复制
   
摘 要:用VBA由用户定义的SQL SELECT语句创建动态报表。
正 文:
用VBA由用户定义的SQL Select语句创建动态报表:
Function CreateDynamicReport(strSQL As String)
Dim db As DAO.database ' database object
Dim rs As DAO.Recordset ' recordset object
Dim fld As DAO.Field ' recordset field
Dim txtNew As Access.TextBox ' textbox control
Dim lblNew As Access.Label ' label control
Dim rpt As Report ' hold report object
Dim lngTop As Long ' holds top value of control position
Dim lngLeft As Long ' holds left value of controls position
Dim title As String 'holds title of report
 
     'set the title
     title = "Title for the Report"
 
     ' initialise position variables
     lngLeft = 0
     lngTop = 0
 
     'Create the report
     Set rpt = CreateReport
 
     ' set properties of the Report
     With rpt
         .Width = 8500
         .RecordSource = strSQL
         .Caption = title
     End With
 
     ' Open SQL query as a recordset
     Set db = CurrentDb
     Set rs = db.OpenRecordset(strSQL)    
 
     ' Create Label Title
     Set lblNew = CreateReportControl(rpt.Name, acLabel, _
     acPageHeader, , "Title", 0, 0)
     lblNew.FontBold = True
     lblNew.FontSize = 12
     lblNew.SizeToFit
 
     ' Create corresponding label and text box controls for each field.
     For Each fld In rs.Fields
 
         ' Create new text box control and size to fit data.
         Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
         acDetail, , fld.Name, lngLeft + 1500, lngTop)
         txtNew.SizeToFit
 
         ' Create new label control and size to fit data.
         Set lblNew = CreateReportControl(rpt.Name, acLabel, acDetail, _
         txtNew.Name, fld.Name, lngLeft, lngTop, 1400, txtNew.Height)
         lblNew.SizeToFit        
 
         ' Increment top value for next control
         lngTop = lngTop + txtNew.Height + 25
     Next
 
     ' Create datestamp in Footer
     Set lblNew = CreateReportControl(rpt.Name, acLabel, _
     acPageFooter, , Now(), 0, 0)
 
     ' Create page numbering on footer
     Set txtNew = CreateReportControl(rpt.Name, acTextBox, _
     acPageFooter, , "='Page ' & [Page] & ' of ' & [Pages]", rpt.Width - 1000, 0)
     txtNew.SizeToFit
 
     ' Open new report.
     DoCmd.OpenReport rpt.Name, acViewPreview
 
     'reset all objects
     rs.Close
     Set rs = Nothing
     Set rpt = Nothing
     Set db = Nothing
 
End Function

Access软件网交流QQ群(群号:198465573)
 
 相关文章
[access报表]创建完美报表  【胡春林  2007/10/18】
[access报表]创建简单报表  【UMVSoft整理  2008/5/10】
[access报表]交叉表生成动态报表  【蟹仔  2012/1/16】
快速开发平台--创建动态查询实现统计/统计符合条件的记录个数  【风行  2015/3/9】
交叉表查询作动态报表  【半夜罗  2015/5/14】
常见问答
技术分类
相关资源
文章搜索
关于作者

Big Young

文章分类

文章存档

友情链接