使用 VBA 构建 Access 2010 中的报表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19641458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:06:27  来源:igfitidea点击:

Reports in Access 2010 Building with VBA

vbams-access-2010

提问by ColdTeck

Ok So I have some questions about VBA Syntax to Build Access Report.

好的,所以我对构建访问报告的 VBA 语法有一些疑问。

I know of the following functions to insert information CreateReportControlFunction, and to delete information DeleteReportControl, and even to modify sections using section
EX: rpt.section(acDetail).Height=0.

我知道以下函数可以插入信息CreateReportControl函数,删除信息DeleteReportControl,甚至使用
EX部分修改部分:rpt.section(acDetail).Height=0

  1. I want to know how to toggle off the page header and footer, not just set Visable to False. I want it so it cannot be seen in Design View.

  2. How to duplicate a report in Vba and assign it a new name / add a Sub Report to a main report and move it's placement around. Or at lease duplicate it and leave it open because I have a code the renames it as shown here:

    Public Function GetUniqueReportName() As String
    Dim intCounter As Integer
    Dim blnIsUnique As Boolean
    Dim rpt As Object
    
      For intCounter = 1 To 256
          GetUniqueReportName = "SubReport_" & Format(intCounter, "0000")
          blnIsUnique = True
          For Each rpt In CurrentProject.AllReports
              If rpt.Name = GetUniqueReportName Then blnIsUnique = False
          Next
          If blnIsUnique Then Exit Function
     Next
    
     GetUniqueReportName = ""
     End Function
    
  3. Also is there any more functions that would help me build a Access report via VBA, you don't have to explain what they do I just want to know what they are so I can search for them directly because there is not a lot of information on the web on how to do this.

  1. 我想知道如何关闭页眉和页脚,而不仅仅是将 Visable 设置为 False。我想要它,所以它不能在设计视图中看到。

  2. 如何在 Vba 中复制报告并为其分配新名称/将子报告添加到主报告并移动其位置。或者至少复制它并保持打开状态,因为我有一个代码将它重命名为如下所示:

    Public Function GetUniqueReportName() As String
    Dim intCounter As Integer
    Dim blnIsUnique As Boolean
    Dim rpt As Object
    
      For intCounter = 1 To 256
          GetUniqueReportName = "SubReport_" & Format(intCounter, "0000")
          blnIsUnique = True
          For Each rpt In CurrentProject.AllReports
              If rpt.Name = GetUniqueReportName Then blnIsUnique = False
          Next
          If blnIsUnique Then Exit Function
     Next
    
     GetUniqueReportName = ""
     End Function
    
  3. 还有其他功能可以帮助我通过 VBA 构建 Access 报告,您不必解释它们是做什么的,我只想知道它们是什么,以便我可以直接搜索它们,因为没有很多信息在网上关于如何做到这一点。

All this information would be of much help, and I am under the assumption that many other people could use this information as well as there is not a lot on this subject. Thanks in advance! :) If you cant answer all the questions that's ok any information at this point is a bonus.

所有这些信息都会有很大帮助,而且我假设许多其他人可以使用这些信息,而且关于这个主题的信息并不多。提前致谢!:) 如果你不能回答所有的问题,那么此时的任何信息都是额外的。

回答by jacouh

III. This will create rptTmp by calling rptCreateTmpReportSimple():

三、这将通过调用 rptCreateTmpReportSimple() 创建 rptTmp:

Function rptCreateTmpReportSimple()
'
  Dim lLeft As Long, lTop As Long, lWidth As Long, lHeight As Long
'
  Dim strFld As String, strRecordSource As String
  Dim strRpt As String
'
  Dim rpt As Access.Report
  Dim ctl As Access.control
'
  strRecordSource = "MyTableName"
  strRpt = "rptTmp"
'
  Set rpt = Application.CreateReport
'
  rpt.visible = False
'
' define report properties, unit is in twips.
'
  rpt.RecordSource = strRecordSource
  rpt.caption = "Special Report"
'
  rpt.DefaultView = acPreview
  rpt.Width = 9870
'
  rpt.visible = True
'
' save it with a name:
'
  DoCmd.Save acDefault, strRpt
'
' create a textbox for each field:
'
  strFld = "FieldName1"
  lLeft = 100
  lTop = 50
  lWidth = 2000
  lHeight = 250
'
  Set ctl = Application.CreateReportControl(strRpt, acTextBox, _
    acDetail, , strFld, lLeft, lTop, lWidth, lHeight)
  ctl.Name = strFld
  ctl.Fontsize = 8
'
' Create other controls...
'...
'
  DoCmd.Close acReport, strRpt, acSaveYes
'
' close ADO objects:
'
  Set ctl = Nothing
  Set rpt = Nothing
'
  rptCreateTmpReportSimple = strRpt
'
End Function

For your question:

对于您的问题:

I. You cannot delete page header and footer, but you can shrink them to 0 height.

一、页眉页脚不能删除,但可以缩小到0高度。

II. Copy rptTmp to rptTmp2:

二、将 rptTmp 复制到 rptTmp2:

DoCmd.CopyObject , "rptTmp2", acReport, "rptTmp"