VBA代码生成PDF报告

时间:2018-01-18 16:02:41

标签: vba ms-access

您好我正在学习访问和VBA。最近创建了一份报告(Report1),显示了针对单个课程的调查评论。此报告的标准为[Course]。我还创建了一个查询,其中包含一系列课程(Course List),可以循环生成每门课程的报告。我正在寻找一些可以将这两者结合在一起并为每门课程创建单独的pdf的基本代码。

这是我到目前为止所做的:

Private Sub GetCourseName()
Dim rst As Recordset
Dim db As Database
Dim strSQL As String

Set db = CurrentDb()
Set rst = db.OpenRecordset("SELECT [Course] FROM [Course List]")

rst.MoveFirst
Do Until rst.EOF
DoCmd.OpenReport "rptReport1", acViewPreview, , "Course = " & rst!Course
  DoCmd.OutputTo acOutputReport, "rptReport1", acFormatPDF, "C:\Users\raj.jk\Documents\Course Evaluations" & rst!Course & ".pdf"

DoCmd.Close acReport, "rptReport1"
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
strSQL = ""
End Sub

2 个答案:

答案 0 :(得分:0)

大概Course是一个文本值(而不是数值),因此您需要在过滤器表达式中用单引号将其括起来:

"Course = '" & rst!Course & "'"

或嵌入双引号:

"Course = """ & rst!Course & """"

答案 1 :(得分:0)

我猜你的报告设计有问题。这不是一件容易解决的问题,但是出口报告的设计可能会有很大的帮助。查看此声明Application.SaveAsText acReport, "rptReport1", "C:\rptReport1_design_exported_as_text.txt"

我尝试重新创建您的数据库。我使用了这些说明:



Private Sub CodeToProduceTextReports__01()
' create table and insert some testing values
    CurrentDb.Execute "CREATE TABLE [Course List] (CourseID autoincrement, Course varchar(50), Course_Title varchar(50), Course_Location varchar(50))"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('alfa', 'first Greek lettre', 'aa')"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('beta', 'second Greek lettre', 'bb')"
    CurrentDb.Execute "INSERT INTO [Course] (Course, Course_Title, Course_Location) VALUES " & _
            "('gamma', 'thirth Greek lettre', 'cc')"
End Sub
Private Sub CodeToProduceTextReports__02() ' importing the design from the report from a saved version in text format Application.LoadFromText acReport, "rptReport1", "C:\rptReport1_design_exported_as_text.txt" End Sub
Private Sub CodeToProduceTextReports__03() ' --03-- print the displayed data of the imported report to a text-file ' for my testings I needed to switch from PDF to TXT DoCmd.OutputTo acReport, "rptReport1", acFormatTXT , "C:\Data Displayed By Report.txt", False, "" End Sub
Private Sub CodeToProduceTextReports__04() ' --04-- export the design of the report to a text-file Application.SaveAsText acReport, "rptReport1", "C:\design of report in text format.txt" End Sub
Private Sub CodeToProduceTextReports__05() ' --05-- routine to print a file for each course ... rst.MoveFirst Do Until rst.EOF DoCmd.OpenReport "rptReport1", acViewPreview, , "Course = '" & rst!Course & "'" ' for my testings I needed to switch from PDF to TXT DoCmd.OutputTo acOutputReport, "rptReport1", acFormatTXT, "Course Evaluations" & rst!Course & ".txt" DoCmd.Close acReport, "rptReport1" rst.MoveNext Loop ... End Sub

要执行这些说明,您需要文件"C:\rptReport1_design_exported_as_text.txt"的内容。


Version =19
VersionRequired =19
Checksum =-3122728
Begin Report
LayoutForPrint = NotDefault AllowDesignChanges = NotDefault
DefaultView =0 TabularFamily =0 DateGrouping =1 GrpKeepTogether =1 PictureAlignment =2 DatasheetGridlinesBehavior =3 GridY =10 Width =5669 ItemSuffix =3 Left =270 Top =600 Right =7725 Bottom =7140 DatasheetGridlinesColor =12632256 RecSrcDt = Begin 0x8d11b150f50de540 End GUID = Begin 0xebeaeb4b353b1e41965fcce57dee6850 End NameMap = Begin 0x0acc0e5500000000b21f4d48d7501c47975fad457306950f0000000006bb61eb , 0x3f0ee540000000000000000043006f00750072007300650020004c0069007300 , 0x7400000000000000a1f51f8cd9d88f40b7d811fdc68024a207000000b21f4d48 , 0xd7501c47975fad457306950f43006f00750072007300650000000000000030e5 , 0x8a48064ac944ba5f2d69ec0cfe2507000000b21f4d48d7501c47975fad457306 , 0x950f43006f0075007200730065005f005400690074006c006500000000000000 , 0x3f6cd73810d7ed4dacc51df5fba38c4a07000000b21f4d48d7501c47975fad45 , 0x7306950f43006f0075007200730065005f004c006f0063006100740069006f00 , 0x6e00000000000000000000000000000000000000000000000c00000002000000 , 0x0000000000000000000000000000 End RecordSource ="Course List" DatasheetFontName ="Arial Narrow" PrtMip = Begin 0xae050000ae050000ae050000ae0500000000000025160000a506000001000000 , 0x010000006801000000000000a10700000100000001000000 End PrtDevMode = Begin 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x010400049c009000032f000002000900000000006400010001002c0101000100 , 0x2c01010000004c65747465720000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x000000000000000000000000000000000000000000000000000000007769646d , 0x10000000010000000000000000000000fe00000001000000000000002c010000 , 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x000000000000000000000000 End PrtDevNames = Begin 0x080036005d000100000000000000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000000000000000000000000000000 , 0x00000000000000000000000000000000000000000000000000000000004d6963 , 0x726f736f667420446f63756d656e7420496d6167696e67205772697465722050 , 0x6f72743a00 End Begin Begin Label BackStyle =0 TextFontFamily =2 FontName ="Arial" End Begin TextBox FELineBreak = NotDefault OldBorderStyle =0 TextFontFamily =2 Width =1701 LabelX =-1701 FontName ="Arial" End Begin Section KeepTogether = NotDefault Height =1701 Name ="Details" GUID = Begin 0xbbf704242af93b45b77934d4cf2c2e95 End Begin Begin TextBox IMESentenceMode =3 Left =1870 Top =113 Name ="Course" ControlSource ="Course" GUID = Begin 0xc8704b4483a48440b68813fa30cc9d70 End Begin Begin Label Left =169 Top =113 Width =630 Height =225 Name ="Bijschrift0" Caption ="Course:" GUID = Begin 0xb620b2a6a8332d42a52b1dc58425b1be End End End End Begin TextBox IMESentenceMode =3 Left =2097 Top =453 TabIndex =1 Name ="Course_Title" ControlSource ="Course_Title" GUID = Begin 0xd33ee5134f828846ad63db31ed83c4e4 End Begin Begin Label Left =396 Top =453 Width =1005 Height =225 Name ="Bijschrift1" Caption ="Course_Title:" GUID = Begin 0x44b12ea59ae514488e333d8451fbf68a End End End End Begin TextBox IMESentenceMode =3 Left =2381 Top =850 TabIndex =2 Name ="Course_Location" ControlSource ="Course_Location" GUID = Begin 0x139fdfe9aad7034aa7ad2f137e63e486 End Begin Begin Label Left =680 Top =850 Width =1290 Height =225 Name ="Bijschrift2" Caption ="Course_Location:" GUID = Begin 0x8f36ef586867bb4faf9cbc80efd0a570 End End End End End End End End
相关问题