如何在 MS Access 报表宏中使用 VBA 更改记录源
How to Change Record Source with VBA in MS Access Report Macro
全部,
在 MS Access 2010 中,我有一个 table(今天的 Settled Jrnls)linked 到报告中。我 运行 下面的 VBA 代码将报告导出为共享驱动器上的 pdf。
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Dan\" & Format(Date, "mm-dd-yyyy") & "\"
If Dir(mypath, vbDirectory) = "" Then MkDir mypath
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT distinct [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
这行得通,但我想将 [今天的已解决 Jrnls] table 更改为不同的 table [新 Jrnls]。新的 table 具有完全相同的列和设置。但是,当我在上面的select语句中更改table时,代码运行s但是报告是空白的。我假设这是因为报告(结算报告)仍然 linked 到旧的 table。你知道我如何 link 向新 table 报告 VBA 吗?
段
您可以轻松地将 Reports 记录源 属性 设置为 SQL 字符串或将其绑定到 table
您甚至可以使用 open 等事件来设置
Me.RecordSource ="SELECT * FROM TBL"
这里检查这个 link:https://docs.microsoft.com/en-us/office/vba/api/access.report.recordsource
对于帕法特:
只需在第一次打开报告后添加一个 RecordSource
调用以指向新的 table:
' OPEN REPORT
DoCmd.OpenReport "Settlement Report", acViewReport
' ADJUST SOURCE
Reports![Settlement Report].Report.RecordSource = "SELECT * FROM [New Jrnls]"
' FILTER AND OUTPUT
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
全部,
在 MS Access 2010 中,我有一个 table(今天的 Settled Jrnls)linked 到报告中。我 运行 下面的 VBA 代码将报告导出为共享驱动器上的 pdf。
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Dan\" & Format(Date, "mm-dd-yyyy") & "\"
If Dir(mypath, vbDirectory) = "" Then MkDir mypath
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT distinct [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
这行得通,但我想将 [今天的已解决 Jrnls] table 更改为不同的 table [新 Jrnls]。新的 table 具有完全相同的列和设置。但是,当我在上面的select语句中更改table时,代码运行s但是报告是空白的。我假设这是因为报告(结算报告)仍然 linked 到旧的 table。你知道我如何 link 向新 table 报告 VBA 吗?
段
您可以轻松地将 Reports 记录源 属性 设置为 SQL 字符串或将其绑定到 table
您甚至可以使用 open 等事件来设置
Me.RecordSource ="SELECT * FROM TBL"
这里检查这个 link:https://docs.microsoft.com/en-us/office/vba/api/access.report.recordsource
对于帕法特:
只需在第一次打开报告后添加一个 RecordSource
调用以指向新的 table:
' OPEN REPORT
DoCmd.OpenReport "Settlement Report", acViewReport
' ADJUST SOURCE
Reports![Settlement Report].Report.RecordSource = "SELECT * FROM [New Jrnls]"
' FILTER AND OUTPUT
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"