使用报表中的数据向表中添加行?

时间:2012-10-31 20:54:33

标签: ms-access automation access-vba report

现在,我有一个数据库,允许用户使用参数根据表中的查询创建基本报表。非常直截了当。我现在要做的是每次创建报告时使用VBA将记录添加到单独的表中。每个报告都有来自查询的信息PLUS一些新信息(连接的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的专家,所以到目前为止已经进行了大量的反复试验。

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

1 个答案:

答案 0 :(得分: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