<?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>aviatormonkey</title>
	<atom:link href="http://aviatormonkey.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://aviatormonkey.wordpress.com</link>
	<description>tasty excel bites...</description>
	<lastBuildDate>Wed, 29 Apr 2009 22:17:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='aviatormonkey.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>aviatormonkey</title>
		<link>http://aviatormonkey.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://aviatormonkey.wordpress.com/osd.xml" title="aviatormonkey" />
	<atom:link rel='hub' href='http://aviatormonkey.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Lesson One&#8230; SQL in VBA</title>
		<link>http://aviatormonkey.wordpress.com/2009/04/20/lesson-one-sql-in-vba/</link>
		<comments>http://aviatormonkey.wordpress.com/2009/04/20/lesson-one-sql-in-vba/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 23:36:00 +0000</pubDate>
		<dc:creator>excelpenguin</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Data Import]]></category>

		<guid isPermaLink="false">http://aviatormonkey.wordpress.com/?p=8</guid>
		<description><![CDATA[SQL in VBA, am I crazy? In this case... no! Not only is it flexible, but it's the fastest way to get data from A to B<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aviatormonkey.wordpress.com&amp;blog=6254385&amp;post=8&amp;subd=aviatormonkey&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-19" title="The references you'll need" src="http://aviatormonkey.files.wordpress.com/2009/02/references1.jpg?w=322&#038;h=255" alt="The references you'll need" width="322" height="255" />Vital to any peice of reporting is timely and accurate data. New data needs to be imported and the whole process needs to be as quick and seemless as possible. While you could simply record or write VBA to open a given file, copy accross the data and close the file again. This may work, but there is infact a better, faster and more flexible way. In fact, you won&#8217;t even need to open the file your reading again!</p>
<p><strong>Step 1:</strong></p>
<p>Before we get started let&#8217;s lay the groundwork.  First things first let&#8217;s add a few references we&#8217;ll be needing, so press alt+F11 and you&#8217;ll get yourself to the Visual Basic editor. From there we need on the top menu &#8220;tools&#8221; and then &#8220;references&#8221;. Then you&#8217;ll need to add some in (top right):</p>
<p>Microsoft ADO  Ext. for DDL and Security and Microsoft ActiveX Data Objects Library.</p>
<p><strong>Step 2:</strong></p>
<p>Now let&#8217;s add in the VBA, to do that we&#8217;ll need a new module and add the VBA in below:</p>
<blockquote><p><span style="color:#3366ff;">Sub SQLinVBA(FullFileName As String,  SQL_Text As String, ToSheet As String)</span></p>
<p><span style="color:#3366ff;">&#8216; ///// aviator monkey /////<br />
</span></p>
<p><span style="color:#3366ff;">Dim Cn As ADODB.Connection<br />
Dim FileFullName As String<br />
Dim SheetName As String, SQL_Text As String<br />
Dim Rst As ADODB.Recordset</span></p>
<p><span style="color:#3366ff;">&#8216; Define the closed excel workbook as database :<br />
&#8216; Setting up a new connection :<br />
Set Cn = New ADODB.Connection</span></p>
<p><span style="color:#3366ff;">&#8216; Connection using the &#8216;OLE DB Microsoft Jet&#8217; provider </span></p>
<p><span style="color:#3366ff;">With Cn<br />
.Provider = &#8220;Microsoft.Jet.OLEDB.4.0&#8243;<br />
&#8216; With columns headers :<br />
.ConnectionString = &#8220;Data Source=&#8221; &amp; FileFullName &amp; _<br />
&#8220;;Extended Properties=&#8221;"Excel 8.0;HDR=YES&#8221;";&#8221;<br />
&#8216;        &#8216; Without columns headers<br />
&#8216;        .ConnectionString = &#8220;Data Source=&#8221; &amp; FileFullName &amp; _<br />
&#8220;;Extended Properties=Excel 8.0;&#8221;<br />
.Open<br />
End With</span></p>
<p><span style="color:#3366ff;">Set Rst = Cn.Execute(SQL_Text)</span></p>
<p><span style="color:#3366ff;">&#8216; Write the request result in cell A1 :<br />
Sheets(ToSheet).Range(&#8220;A1&#8243;).CopyFromRecordset Rst</span></p>
<p><span style="color:#3366ff;">&#8216; Close connection :<br />
Cn.Close<br />
Set Rst = Nothing<br />
Set Cn = Nothing</span></p>
<p><span style="color:#3366ff;">End Sub</span></p></blockquote>
<p><strong>Step 3:</strong></p>
<p><span style="color:#333399;"><span style="color:#000000;">Ok let&#8217;s try an example. Calling the subroutine (that&#8217;s fancy talk for using the code you just added), either by highlighting the code (below) an pressing F5 (the quickest way), or by running the macro in your preferred way&#8230;<br />
</span></span></p>
<blockquote><p><span style="color:#3366ff;">Sub Go<br />
</span></p>
<p><span style="color:#3366ff;">Call SQLi<span style="color:#3366ff;">nVBA(&#8220;C:\TEST.xls&#8221;,&#8221;SELECT * FROM [Sheet1$]&#8220;,&#8221;Sheet2&#8243;)</span></span></p>
<p><span style="color:#3366ff;"><span style="color:#3366ff;">End Sub<br />
</span></span></p></blockquote>
<p>R<span style="color:#000000;">eplace C:\TEST.xls with the .xls file you want to read, Sheet2 with the sheet you&#8217;d like it written to (in your current workbook) and as for the the SQL text, SELECT * FROM [Sheet1$]: in this case it&#8217;s reading all the data from Sheet1, but there&#8217;s nothing stopping you doing almost anything you would in SQL normally, add a few where clauses, th</span>e odd &#8220;case&#8221; statement, knock youself out. Notice that the sheet name is surrounded by square brackets and a dollar symbol this is vital (not optional!) and if it&#8217;s not in there you&#8217;ll get throw all sorts of errors your way.</p>
<p>That&#8217;s it for today&#8217;s lesson, go play.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/aviatormonkey.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/aviatormonkey.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/aviatormonkey.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=aviatormonkey.wordpress.com&amp;blog=6254385&amp;post=8&amp;subd=aviatormonkey&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://aviatormonkey.wordpress.com/2009/04/20/lesson-one-sql-in-vba/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/aee08d5e4b759ce7f53ba2c019803afe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">excelpenguin</media:title>
		</media:content>

		<media:content url="http://aviatormonkey.files.wordpress.com/2009/02/references1.jpg" medium="image">
			<media:title type="html">The references you'll need</media:title>
		</media:content>
	</item>
	</channel>
</rss>
