{"id":66213,"date":"2006-10-20T19:41:00","date_gmt":"2006-10-20T19:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/20\/how-can-i-print-a-microsoft-access-report\/"},"modified":"2006-10-20T19:41:00","modified_gmt":"2006-10-20T19:41:00","slug":"how-can-i-print-a-microsoft-access-report","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-print-a-microsoft-access-report\/","title":{"rendered":"How Can I Print a Microsoft Access Report?"},"content":{"rendered":"<p><P>&nbsp;<\/P><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I print a Microsoft Access report?<BR><BR>&#8212; RW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RW. You know the cool thing about being a Scripting Guy \u2013 aside from the opportunity to pal around with Paris and Nicole \u2013 is the fact that people think we know what we\u2019re doing. Someone asks a question like, \u201cHow can I print a Microsoft Access report?\u201d and the Scripting Guys respond with a script that does just that. Wow, you find yourself thinking. Is there anything those guys <I>don\u2019t<\/I> know?<\/P>\n<P>Just between us, RW, there are <I>tons<\/I> of things the Scripting Guys don\u2019t know, things like, well, how you can print a Microsoft Access report. To be honest, we have no idea how to print a report from Microsoft Access; that means a question like this one results in a flurry of activity, with a gaggle of Scripting Guys checking SDKs, writing sample code, and running and debugging scripts. But no one ever sees that part of the process; instead, everyone sees the finished product and assumes that we knew the answer right off the top of our heads. That\u2019s rarely the case, but we\u2019re careful not to let anyone know that: after all, we\u2019ve acquired a reputation as guys who know what they\u2019re doing, and you\u2019d have to be a <I>really<\/I> dumb Scripting Guy to spill the beans and let people know the sordid truth.<\/P>\n<TABLE class=\"dataTable\" id=\"EHD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><I>Editor\u2019s Note: It should be pretty obvious at this point that<\/I><I> at least<\/I><I> one of three things is occurring here:<\/I><BR><BR><I>&#8211; The Scripting Guy who writes this column is the dumb Scripting Guy<\/I><BR><I>&#8211; The Scripting Guy who writes this column is trying (most likely unsuccessfully) to convince his manager that he really does work<\/I><BR><I>&#8211; The Scripting Guy who writes this column has nothing interesting to talk about today<\/I><BR><BR><I>We\u2019ll let you decide.<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how <I>do<\/I> you print a Microsoft Access report? That\u2019s an easy one, RW, at least for highly-competent, world-class experts like the Scripting Guys. Here\u2019s how:<\/P><PRE class=\"codeSample\">Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;<\/p>\n<p>Set objCommand = objAccess.DoCmd<\/p>\n<p>objCommand.OpenReport &#8220;EmployeeList&#8221;<\/p>\n<p>objAccess.CloseCurrentDatabase\n<\/PRE>\n<P>As you can see, we didn\u2019t exactly have to knock ourselves out in order to print a report from Microsoft Access. We start out by creating an instance of the <B>Access.Application<\/B> object, then use the <B>OpenCurrentDatabase<\/B> method to open the database C:\\Scripts\\Test.mdb. <\/P>\n<TABLE class=\"dataTable\" id=\"ERE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. You might have noticed that, with this script, we didn\u2019t bother setting the <B>Visible<\/B> property to True and making Access visible onscreen. Why not? That\u2019s simple: the report prints out so quickly there didn\u2019t seem to be any reason to have Access appear onscreen and then, just as quickly, disappear. However, if you\u2019d prefer to see Access onscreen while the report prints all you have to do is add this line of code immediately after the line that opens the database:<\/P><PRE class=\"codeSample\">objAccess.Visible = True\n<\/PRE><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That brings us to this portion of the script:<\/P><PRE class=\"codeSample\">Set objCommand = objAccess.DoCmd\n<\/PRE>\n<P>The <B>DoCmd<\/B> object is an unusual \u2013 albeit useful \u2013 little object. More often than not scripts that interact with Microsoft Access don\u2019t use any Access-specific commands; instead, the scripts simply use Access as a data source and rely on generic database commands (like those found in ActiveX Data Objects) to perform their chores. On occasion, however (and this is one of those occasions), your script will need to do something Access-specific, like print a report or open a form. At times like that you need to use the DoCmd object to invoke those Access-only commands. <\/P>\n<P>Which, needless to say, explains why we created that instance of the DoCmd object.<\/P>\n<P>Once we have our very own DoCmd object we can then print the report (to the default printer) by calling the <B>OpenReport<\/B> method and passing the name of the report we want to print (in this case, that\u2019s a report named <I>EmployeeList<\/I>):<\/P><PRE class=\"codeSample\">objCommand.OpenReport &#8220;EmployeeList&#8221;\n<\/PRE>\n<P>And from there all we have to do is close the database and we\u2019re done:<\/P><PRE class=\"codeSample\">objAccess.CloseCurrentDatabase\n<\/PRE>\n<P>And yes, with this script you need to explicitly close the database. If you don\u2019t, your invisible instance of Access will continue to run in the background forever and ever; we\u2019re guessing you don\u2019t really need to have invisible copies of Access running in the background using up system resources. But that\u2019s up to you.<\/P>\n<P>Incidentally, you can use a related script to print the data in an Access table; that provides a way to print information without having to create an Access report. For example, this script prints all the data in the <I>Employees<\/I> table:<\/P><PRE class=\"codeSample\">Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;<\/p>\n<p>Set objCommand = objAccess.DoCmd<\/p>\n<p>objCommand.OpenTable &#8220;Employees&#8221;\nobjCommand.PrintOut<\/p>\n<p>objAccess.CloseCurrentDatabase\n<\/PRE>\n<P>You can see the similarities between this script and our report-printing script. In both scripts we start out by creating an instance of Access, opening the database C:\\Scripts\\Test.mdb, then creating an instance of the DoCmd object. In the table-printing script we then use these three lines of code to open the table, print the data (using the <B>Printout<\/B> method), and then close the database:<\/P><PRE class=\"codeSample\">objCommand.OpenTable &#8220;Employees&#8221;\nobjCommand.PrintOut<\/p>\n<p>objAccess.CloseCurrentDatabase\n<\/PRE>\n<P>All in all, pretty darn easy.<\/P>\n<P>We should also note that there are additional parameters you can employ when calling the PrintOut method; for example, you can specify a range of pages to print, you can specify a print quality (draft, low, medium, high), and even indicate the number of copies to print and whether or not those copies should be collated. For more information on these additional parameters take a look at the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbaac11\/html\/acmthactPrint_HV05186504.asp\" target=\"_blank\"><B>Microsoft Access VBA Language Reference<\/B><\/A>. <\/P>\n<P>Not that the Scripting Guys ever have to refer to outside resources like the VBA Language Reference; after all, we wouldn\u2019t be Scripting Guys if we didn\u2019t already know all that stuff. We just thought that other people might find it helpful.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct06\/hey1020.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct06\/hey1020.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! How can I print a Microsoft Access report?&#8212; RW Hey, RW. You know the cool thing about being a Scripting Guy \u2013 aside from the opportunity to pal around with Paris and Nicole \u2013 is the fact that people think we know what we\u2019re doing. Someone asks a question like, \u201cHow [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[54,49,3,5],"class_list":["post-66213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-access","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! How can I print a Microsoft Access report?&#8212; RW Hey, RW. You know the cool thing about being a Scripting Guy \u2013 aside from the opportunity to pal around with Paris and Nicole \u2013 is the fact that people think we know what we\u2019re doing. Someone asks a question like, \u201cHow [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}