{"id":66303,"date":"2006-10-09T13:18:00","date_gmt":"2006-10-09T13:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/09\/how-can-i-compact-an-access-database\/"},"modified":"2006-10-09T13:18:00","modified_gmt":"2006-10-09T13:18:00","slug":"how-can-i-compact-an-access-database","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-compact-an-access-database\/","title":{"rendered":"How Can I Compact an Access Database?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I compact an Access database using a script?<BR><BR>&#8212; DA<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DA. We have to tell you the truth: we are thrilled and excited about getting the chance to answer your scripting question. To be honest, that isn\u2019t always the case, most days we\u2019d look at a question like this and think, \u201cCompact an Access database? That sounds hard; why would we want to do that?\u2019\u201d But that was before last night, when the Scripting Guy who writes this column heard the most dreaded words in the English language: \u201cDad, can you help me with my math homework?\u201d<\/P>\n<TABLE id=\"E2C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. If you don\u2019t have children, there <I>are<\/I> a few rough equivalents to \u201cCan you help me with my math homework?\u201d including \u201cCould I see you in my office for a moment?\u201d and \u201cI hope you didn\u2019t cut a piece of that cake that I had sitting on the counter.\u201d<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As it turns out, compacting an Access database is <I>way<\/I> easier that determining the real zeroes of polynomial functions. For one thing, the Scripting Guy who writes this column had only the vaguest memories of polynomial functions. For another, back when he took high school math the zero hadn\u2019t even been invented yet; in fact, no one had ever found a use for zero, at least not until they had to start scoring the Scripting Guy\u2019s math homework. Suddenly, there was a need for zero after all!<\/P>\n<TABLE id=\"EOD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Over time, of course, people found more uses for zero; for example, it turns out to be incredibly important when calculating the Scripting Editor\u2019s batting average.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Somehow or another, the Scripting Guy and his son managed to slog through the assignment; in fact, as near as they could tell they even managed to come up with the right answers to boot. (The Scripting Guy who writes this column had forgotten that homework is much harder if you try to get the <I>right<\/I> answers.) Having experienced a taste of life outside the world of scripting, however, the Scripting Dad vowed that he would never again complain about having to answer scripting-related questions. Let\u2019s face it: scripting questions are <I>nothing<\/I> compared to determining the real zeroes of polynomial functions.<\/P>\n<P>If you don\u2019t believe us (and it <I>is<\/I> hard to believe that anything could be easier than determining the real zeroes of polynomial functions), take a look at this piece of code, a script that compacts (and, if necessary, repairs) the Access database C:\\Scripts\\Test.mdb:<\/P><PRE class=\"codeSample\">Const CreateLog = True<\/p>\n<p>Set objAccess = CreateObject(&#8220;Access.Application&#8221;)<\/p>\n<p>errReturn = objAccess.CompactRepair _\n    (&#8220;c:\\scripts\\test.mdb&#8221;, &#8220;c:\\scripts\\test2.mdb&#8221;, CreateLog)<\/p>\n<p>Wscript.Echo &#8220;Compact\/repair succeeded: &#8221; &amp; errReturn\n<\/PRE>\n<P>Granted, it\u2019s not as exciting as factoring x<SUP>4<\/SUP> + 29x + 100, although it\u2019s close. We start out by defining a constant named CreateLog and setting the value to True. When we compact the database we\u2019ll use this constant to tell the script to create a log file if any corruption is detected in the database. As far as we know (and, to be honest, our knowledge here rivals our knowledge of synthetic division) if a database is corrupt the script will create a log file in the same folder as the corrupted database. If the database is <I>not<\/I> corrupt then no log file will be created.<\/P>\n<P>Of course, the Scripting Guys would never have <I>anything<\/I> to do with a corrupted database; therefore we can\u2019t verify that this is actually the case. But that\u2019s what the documentation says, and when has Microsoft documentation ever been wrong?<\/P>\n<TABLE id=\"EUE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Don\u2019t bother sending in examples of when Microsoft documentation has been wrong. After all, our email Inbox doesn\u2019t have unlimited storage capacity.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After defining the constant we create an instance of the <B>Access.Application<\/B> object. We then use this line of code to compact (and, if necessary, repair) the database C:\\Scripts\\Test.mdb:<\/P><PRE class=\"codeSample\">errReturn = objAccess.CompactRepair _\n    (&#8220;c:\\scripts\\test.mdb&#8221;, &#8220;c:\\scripts\\test2.mdb&#8221;, CreateLog)\n<\/PRE>\n<P>As you can see, the <B>CompactRepair<\/B> method takes three parameters: the path to the database we want to compact (C:\\Scripts\\Test.mdb); the path to the new, compacted database we\u2019re about to create (C:\\Scripts\\Test2.mdb); and the constant CreateLog. When we execute this line of code the script attempts to compact the database, with the results (True if the operation succeeded, False if the operation failed) being stored in the variable errReturn. All we have to do now is echo back the value of errReturn and we\u2019re done:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Compact\/repair succeeded: &#8221; &amp; errReturn\n<\/PRE>\n<P>That <I>was<\/I> easy, wasn\u2019t it? However, there <I>is<\/I> one drawback to this script: if the file C:\\Scripts\\Test2.mdb already exists then the script will fail. That\u2019s because CompactRepair will not overwrite an existing file. With that in mind we offer a slightly-modified version of the script, one that uses the FileSystemObject to check for the existence of Test2.mdb and, if necessary, deletes it. Here\u2019s what that modified script looks like:<\/P><PRE class=\"codeSample\">Const CreateLog = True<\/p>\n<p>Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nSet objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>If objFSO.FileExists(&#8220;c:\\scripts\\test2.mdb&#8221;) Then\n    objFSO.DeleteFile(&#8220;c:\\scripts\\test2.mdb&#8221;)\nEnd If<\/p>\n<p>errReturn = objAccess.CompactRepair _\n    (&#8220;c:\\scripts\\test.mdb&#8221;, &#8220;c:\\scripts\\test2.mdb&#8221;, CreateLog)<\/p>\n<p>Wscript.Echo &#8220;Compact\/repair succeeded: &#8221; &amp; errReturn\n<\/PRE>\n<P>Admittedly, it would be a bit of a stretch to say that the Scripting Guy who writes this column enjoyed being reacquainted with polynomial functions. However, he <I>was<\/I> intrigued by the opportunity to work with imaginary numbers again. When dealing with polynomial functions you sometimes have to take the square root of a negative number. That\u2019s a problem: negative numbers don\u2019t <I>have<\/I> square roots. But mathematicians aren\u2019t deterred by that: if they can\u2019t use real numbers then they simply use imaginary numbers instead. The Scripting Guys are currently working on a scripting equivalent: if you can\u2019t use a real scripting method to carry out a task we\u2019re trying to develop imaginary scripting methods that you can use instead. We\u2019ll keep you posted on that.<\/P>\n<TABLE id=\"E3F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Of course, we\u2019ll likely report the results in an imaginary scripting column, which means we aren\u2019t totally sure how you\u2019ll <I>find<\/I> that information. But we\u2019ll worry about that later.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I compact an Access database using a script?&#8212; DA Hey, DA. We have to tell you the truth: we are thrilled and excited about getting the chance to answer your scripting question. To be honest, that isn\u2019t always the case, most days we\u2019d look at a question like this and [&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-66303","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>Hey, Scripting Guy! How can I compact an Access database using a script?&#8212; DA Hey, DA. We have to tell you the truth: we are thrilled and excited about getting the chance to answer your scripting question. To be honest, that isn\u2019t always the case, most days we\u2019d look at a question like this and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66303","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=66303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66303\/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=66303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}