【问题标题】:Adding a row to a table with data from report?使用报告中的数据向表中添加一行?
【发布时间】:2012-10-21 11:05:00
【问题描述】:

现在,我有一个数据库,它允许用户根据使用参数的表中的查询创建基本报告。很简单。我现在要做的是每次创建报表时使用 VBA 将记录添加到单独的表中。每个报告都包含来自查询的信息以及一些新信息(连接的 ID、日期等)。新表(“摘要”)将包括一些新信息以及来自原始查询的一些来源。这将是一种创建报告的动态日志。

有没有什么方法可以使用 VBA 将来自两个来源的数据(显示在原始查询的报表上的数据和原生报表数据)合并到一个表中的一条记录中?

这是我目前得到的代码。

Option Compare Database

Public Sub Log_Report()

'System definitions
Dim dbs As DAO.database
Dim rs As DAO.Recordset
Dim rep As [Report_Custom MARS Report]

'Original report sources
Dim Text267 As String
Dim TableName As String
Dim Company_Name As String
Dim ReportID As String

'Summary table destination
Dim ID As Integer
Dim Date_Created As Date
Dim Source As String
Dim Title As String
Dim report_ID As String
Dim Attachment As Attachment

End Sub

我可能已经离开了,所以如果我必须重新开始,我可以接受。无论如何,我都不是 VBA 方面的专家,所以到目前为止,我经历了很多尝试和错误。

如果需要,我可以澄清一下。

【问题讨论】:

  • 如果您有查询,为什么不使用记录集从查询中获取数据?
  • 如果您有查询,为什么不存储用于生成报告的查询?

标签: ms-access automation vba report


【解决方案1】:

如果您的报告的记录集是单行,则使用加载事件读取字段并将它们分配给变量会相对容易。在您的 Report_Load 事件触发时,所有计算都已完成,然后您可以将它们用作将值写入汇总表的函数的输入。

'Code to placed in a public function
'strVar = text267 'is ReportID = report_ID ? 
'Unfortunately I have no experience with attachments, sorry

function writeReportSummary(intID as Integer, dtmDate_Created as Date, strSource as String, strTitle as string, strReportID as string, strVar as string, strTableName as string, strCompanyName as string, attAttachment as attachment) AS boolean

Dim strSQL as string
On error goto Err_Handler

strSQL = "INSERT INTO summary( ID, Date_Created, Source, Title, report_ID, Text267, tableName, Company_Name, ReportID) SELECT """ & intID & """ , """ & dtmDate & """;" 'etc
CurrentDb.execute strSQL, dbFailOnError
Debug.print CurrentDB.recordsAffected & ": Record(s) Inserted at " & now()
writeReportSummary = True
Exit function

Err_Handler:
debug.print err.number
debug.print err.description
writeReportSummary = false
end function

'Code to be placed in Report_load'
Sub Report_Load

if Not(writeReportSummary(intID, dtmDate, etc)) then debug.print "Failed to write report to summary table"

End Sub

【讨论】:

  • 好的,我尝试使用您的代码,但将其放入 OnLoad 事件时出现错误。就是说“赋值左侧的函数调用必须返回变量或对象”。错误发生在“writeReportSummary = True”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 2021-11-09
  • 1970-01-01
相关资源
最近更新 更多