<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>scott's spot &#187; VBA</title>
	<atom:link href="http://scottclyerly.wordpress.com/category/vba/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottclyerly.wordpress.com</link>
	<description></description>
	<lastBuildDate>Tue, 30 Jun 2009 11:50:40 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='scottclyerly.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/faa5ba6df98f09f8ff0642735114cf78?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>scott's spot &#187; VBA</title>
		<link>http://scottclyerly.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://scottclyerly.wordpress.com/osd.xml" title="scott&#8217;s spot" />
		<item>
		<title>Passing Paramters to an Access query from Excel</title>
		<link>http://scottclyerly.wordpress.com/2009/06/30/passing-paramters-to-an-access-query-from-excel/</link>
		<comments>http://scottclyerly.wordpress.com/2009/06/30/passing-paramters-to-an-access-query-from-excel/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 11:50:40 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://scottclyerly.wordpress.com/?p=72</guid>
		<description><![CDATA[So, since I spend so much time in Excel, I thought I&#8217;d do some blogging about my favorite business application. Yes, I&#8217;m a geek, I know. Sue me.
Recently I was trying to figure our how to change an Access query from Excel. By way of some background, I have a report that is generated in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottclyerly.wordpress.com&blog=1973483&post=72&subd=scottclyerly&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So, since I spend so much time in Excel, I thought I&#8217;d do some blogging about my favorite business application. Yes, I&#8217;m a geek, I know. Sue me.</p>
<p>Recently I was trying to figure our how to change an Access query from Excel. By way of some background, I have a report that is generated in Excel via a number of SQL statements passed from Excel to Access via ADO, and the resulting recordset dropped into data sheets in the Excel file.  Okay, fine, no problem. There are endless examples on the web of how to do this. However&#8230;</p>
<p>I was recently tasked with resdesigning what I had originally designed to make it a little more portable. So, in that respect, I needed to pass a single criteria (or parameter) back to an Access query that I had created as part of the overall report function. This query is a time-based query, setting a week number as chosen by a user.</p>
<p>The previous method I used was a number of individual data files, each of which contained Sales and other data up to that week. In other words, the week 1 file had data from week 1, the week 2 file had data from weeks 1 and 2, the week 3 file had weeks 1, 2, and 3, blah blah, etc etc. The purpose of this was to help isolate a beginning of period number and and end of period inventory number.</p>
<p>(The yawn you just heard was my wife dropping off to sleep at this point.)</p>
<p>What I needed to do was to run with a single data set, but allow the users to choose the week they wanted to report on. But to do that, I needed one of the base queries to accept a criteria of that particular week number, and since the report is generated in Excel, I needed to pass that query parameter as part of the report engine.</p>
<p>Okay, so why pass a parameter? Why not just submit the SQL as a command? Because this parameter-accepting query is the basis for sub-queries that allow the whole thing the run (the final SQl statement is an ugly, ugly beast.)</p>
<p>All this being said, I tried a couple of things before landing on the solution. I tried using ADO to change the query, tried ADOX, and final landed on a hybird solution of sorts.</p>
<p>First, I needed an instance of the Access application, so I defined a very generic Object variable and then used CreateObject to create an instance of Access.</p>
<p>Then, using a reference to the Access object library, I was able to use commands normally found in the Access object model to do the work I was trying to do. The work, by the way, was simply to change the QueryDef in the one query and save it.</p>
<p>After some testing, debugging, and commenting, I nailed the solution.</p>
<p>And here it is, heavily commented for your reading enjoyment:</p>
<p><span style="font-family:Courier;"><span style="color:#00007F;">Sub</span> Pass_Parameter_To_MaxTimeQry(sWeek <span style="color:#00007F;">As</span> <span style="color:#00007F;">String</span>)<br />
<span style="color:#007F00;">&#8216;   This passes a parameter to the max of time qry in order</span><br />
<span style="color:#007F00;">&#8216;   to enable dynamic time period run of the report.</span><br />
<span style="color:#007F00;">&#8216;</span><br />
<span style="color:#007F00;">&#8216;   NOTE: in order to run this, you&#8217;ll need a reference to the</span><br />
<span style="color:#007F00;">&#8216;   Microsoft DAO X.X Object Libray.</span><br />
    <br />
    <span style="color:#007F00;">&#8216; Some variables</span><br />
    <span style="color:#00007F;">Dim</span> sDBPath <span style="color:#00007F;">As</span> <span style="color:#00007F;">String</span>       <span style="color:#007F00;">&#8216; This is the path of the DB.</span><br />
    <span style="color:#00007F;">Dim</span> sDBName <span style="color:#00007F;">As</span> <span style="color:#00007F;">String</span>       <span style="color:#007F00;">&#8216; This is the name of the DB</span><br />
    <span style="color:#00007F;">Dim</span> accObj <span style="color:#00007F;">As</span> <span style="color:#00007F;">Object</span>        <span style="color:#007F00;">&#8216; This will be our Access object</span><br />
    <span style="color:#00007F;">Dim</span> dbs <span style="color:#00007F;">As</span> DataBase         <span style="color:#007F00;">&#8216; This is the database object we&#8217;ll use (requires the reference)</span><br />
    <span style="color:#00007F;">Dim</span> qryDef <span style="color:#00007F;">As</span> QueryDef      <span style="color:#007F00;">&#8216; This is the query definition we&#8217;ll use (requires the reference)</span><br />
    <span style="color:#00007F;">Dim</span> sSQL <span style="color:#00007F;">As</span> <span style="color:#00007F;">String</span>          <span style="color:#007F00;">&#8216; This is our SQL string.</span><br />
    <span style="color:#00007F;">Dim</span> sQueryName <span style="color:#00007F;">As</span> <span style="color:#00007F;">String</span>    <span style="color:#007F00;">&#8216; This is our query name.</span><br />
    <br />
    <span style="color:#007F00;">&#8216; We&#8217;ll start out by setting the string variables.</span><br />
    <br />
    <span style="color:#007F00;">&#8216;   The DB path and name are acutally global constants I use elsewhere,</span><br />
    <span style="color:#007F00;">&#8216;   so I don&#8217;t set them specifically here.</span><br />
    sDBPath = msDATA_FILE_PATH<br />
    sDBName = msDATABASE_NAME<br />
    <span style="color:#007F00;">&#8216;   This is the name of the query we&#8217;ll be changing.</span><br />
    sQueryName = &#8220;qryTimeMaxes&#8221;<br />
    <span style="color:#007F00;">&#8216;   This is out SQL statement: the dynamic part that will be fed</span><br />
    <span style="color:#007F00;">&#8216;   from a user form is the Week field.</span><br />
    sSQL = &#8220;SELECT Max(MonRptData.Year) AS MaxOfYear, Max(MonRptData.[Fiscal Season]) AS [MaxOfFiscal Season], Max(MonRptData.[Fiscal Quarter]) AS [MaxOfFiscal Quarter], Max(MonRptData.Month) AS MaxOfMonth, MonRptData.Week &#8221; _<br />
            &amp; &#8220;FROM MonRptData &#8221; _<br />
            &amp; &#8220;GROUP BY MonRptData.Week &#8221; _<br />
            &amp; &#8220;HAVING (((MonRptData.Week)=&#8221; &amp; sWeek &amp; &#8220;));&#8221;<br />
    <br />
    <span style="color:#007F00;">&#8216; Now we&#8217;ll play with our objects (nothing dirty!)</span><br />
    <br />
    <span style="color:#007F00;">&#8216;   First we set the access object. The access object isn&#8217;t really an</span><br />
    <span style="color:#007F00;">&#8216;   access object at all, just a plain old ordinary object that we make</span><br />
    <span style="color:#007F00;">&#8216;   into an access object using the &#8220;Access.Application&#8221; string.</span><br />
    <span style="color:#00007F;">Set</span> accObj = CreateObject(&#8220;Access.Application&#8221;)</p>
<p>    <span style="color:#007F00;">&#8216;   Then we open the object, using an access object model method,</span><br />
    <span style="color:#007F00;">&#8216;   OpenCurrentDatabase, the the DB set by our path and DB name strings.</span><br />
    accObj.OpenCurrentDatabase sDBPath &amp; sDBName, <span style="color:#00007F;">False</span><br />
    <br />
    <span style="color:#007F00;">&#8216;   Next we&#8217;ll set the database variable to the database we&#8217;ve just opened.</span><br />
    <span style="color:#007F00;">&#8216;   Note that, because we didn&#8217;t make our DB visible when we opened it, you can&#8217;t</span><br />
    <span style="color:#007F00;">&#8216;   see it, but it&#8217;s open nevertheless.</span><br />
    <span style="color:#00007F;">Set</span> dbs = accObj.CurrentDb<br />
    <br />
    <span style="color:#007F00;">&#8216;   Now we&#8217;ll get into the query itself.</span><br />
    <span style="color:#007F00;">&#8216;   First we delete the old one&#8230;</span><br />
    dbs.QueryDefs.Delete sQueryName<br />
    <span style="color:#007F00;">&#8216;   &#8230; then we recreate it with our new SQL string</span><br />
    <span style="color:#00007F;">Set</span> qryDef = dbs.CreateQueryDef(sQueryName, sSQL)</p>
<p>    <span style="color:#007F00;">&#8216; That&#8217;s it. The only left if to clean up. We&#8217;ll close the database&#8230;</span><br />
    dbs.Close<br />
    <span style="color:#007F00;">&#8216; &#8230;then destroy our objects to free up all the juicy memory they take up.</span><br />
    <span style="color:#00007F;">Set</span> qryDef = <span style="color:#00007F;">Nothing</span><br />
    <span style="color:#00007F;">Set</span> dbs = <span style="color:#00007F;">Nothing</span><br />
    <span style="color:#00007F;">Set</span> accObj = <span style="color:#00007F;">Nothing</span><br />
    <br />
<span style="color:#00007F;">End</span> <span style="color:#00007F;">Sub</span></span></p>
<p>You can see there&#8217;s no error handling here, and that&#8217;s a flaw, but an easy one to fix.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/scottclyerly.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/scottclyerly.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/scottclyerly.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/scottclyerly.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/scottclyerly.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/scottclyerly.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/scottclyerly.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/scottclyerly.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/scottclyerly.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/scottclyerly.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottclyerly.wordpress.com&blog=1973483&post=72&subd=scottclyerly&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://scottclyerly.wordpress.com/2009/06/30/passing-paramters-to-an-access-query-from-excel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f359e6e85c71a603db923e50aa4af5d6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Scott</media:title>
		</media:content>
	</item>
	</channel>
</rss>