1

现在,我有一个数据库,它允许用户根据使用参数的表中的查询创建基本报告。很简单。我现在要做的是每次创建报表时使用 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 专家,所以到目前为止,我经历了很多试验和错误。

如果需要,我可以澄清。

4

1 回答 1

0

如果您的报告的记录集是单行,则使用 on load 事件读取字段并将它们分配给变量会相对容易。在您的 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
于 2012-10-31T22:43:55.003 回答