<?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/"
	>

<channel>
	<title>haslo.ch - Guido's Blog &#187; vb</title>
	<atom:link href="http://www.haslo.ch/blog/tag/vb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.haslo.ch/blog</link>
	<description>We believe that people with passion can change the world for the better.</description>
	<lastBuildDate>Sun, 07 Mar 2010 17:58:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stack Overflow: Solutions for Coders</title>
		<link>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/</link>
		<comments>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 12:56:35 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[bubble 2.0]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=1225</guid>
		<description><![CDATA[If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things I decried in my What&#8217;s Wrong with VBA series, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a good awesome well-designed [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1252" class="wp-caption alignright" style="width: 160px"><a href="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow.jpg"><img class="size-thumbnail wp-image-1252" title="Stack Overflow in Firefox 3" src="http://www.haslo.ch/blog/wp-content/uploads/2008/09/stack_overflow-150x150.jpg" alt="Stack Overflow" width="150" height="150" /></a><p class="wp-caption-text">Stack Overflow</p></div>
<p>If you&#8217;re a programmer, you know the problem of solution googlability. It&#8217;s particularly prominent in mainstream languages (and one of the things <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/#community">I decried</a> in my <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">What&#8217;s Wrong with VBA series</a>, as VBA is particularly bad in that respect); you have a problem, you google for it, unless you&#8217;re coding in a <span style="text-decoration: line-through;">good</span> <span style="text-decoration: line-through;">awesome</span> <span style="text-decoration: line-through;">well-designed</span> freak language like Ruby, Python, or Haskell, all you get back is trash and spam.</p>
<p>So, since you&#8217;re reading blogs and a programmer (or you&#8217;d have stopped reading by now, I guess), you know <a href="http://www.joelonsoftware.com/">Joel on Software</a>. Now this guy and his team have done something: They took the Wiki approach of &#8220;everybody can edit&#8221;, added Digg&#8217;s &#8220;let&#8217;s vote stuff up&#8221;, and made the whole thing a generic programming Q&amp;A site.</p>
<p style="text-align: center;"><strong>Behold, and witness, the rise of</strong> <a href="http://stackoverflow.com/"><strong>Stack Overflow</strong></a>.</p>
<p>From <a href="http://www.joelonsoftware.com/items/2008/09/15.html">his launch post</a>:</p>
<blockquote><p>Here’s how it’s supposed to work. This is a community project, so I’m being careful to avoid saying this is how it will work… that’s up to the community. But this is roughly what I have in mind.</p>
<p>Every question in Stack Overflow is like the Wikipedia article for some extremely narrow, specific programming question. <em>How do I enlarge a fizzbar without overwriting the user’s snibbit?</em> [...]</p>
<p>Some people propose answers. Others vote on those answers. If you see the right answer, vote it up. If an answer is obviously wrong (or inferior in some way), you vote it down. Very quickly, the best answers bubble to the top. The person who asked the question in the first place also has the ability to designate one answer as the “accepted” answer, but this isn’t required. The accepted answer floats above all the other answers.</p></blockquote>
<p>Sounds great! It has tags, it&#8217;s not restricted to one programming language, it could provide the framework for thousands of programmers collaborating. And the best thing: They plan to &#8220;keep it ad-free and open to the public forever&#8221;.</p>
<p>You&#8217;ll need an <a href="http://en.wikipedia.org/wiki/Openid">OpenID</a> to log in &#8211; I got one <a href="https://www.myopenid.com/">at myOpenID</a> myself. Too bad it&#8217;s not possible to log in without, but whatever &#8211; I guess this approach has more promise than the closed one that Microsoft tried to force upon us with that whole <a href="http://en.wikipedia.org/wiki/Windows_Live_ID">Live ID</a> crap, but still shares the same weaknesses (which are, mainly, another party involved and a central point of failure).</p>
<p>Nevertheless, I do think this could be awesome. Let&#8217;s see where it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/stack-overflow-solutions-for-coders/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (3/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-33/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-33/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 17:27:48 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=496</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3
This is the third part of a three-part series, have a look at part 1 and part 2 first.
So let&#8217;s go on ahead and jump into the final bits of my troubles with MS Access and VBA as environment for medium and larger IT projects &#8211; basically, anything [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p>This is the third part of a three-part series, have a look at <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> and <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> first.</p>
<p>So let&#8217;s go on ahead and jump into the final bits of my troubles with MS Access and VBA as environment for medium and larger IT projects &#8211; basically, anything having 2 and up users.</p>
<h2>Syntax</h2>
<p>There are quite a few things that bother me about the VBA syntax. First of all: it&#8217;s inconsistent, even more so than the one of <a href="http://en.wikipedia.org/wiki/PHP">php</a> (where mainly <a href="http://www.tnx.nl/php.html#args">method signatures are inconsistent</a>), in several places. The most prominent of them are functions. Take these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> answer <span style="color: #000080;">As</span> VbMsgBoxResult
MsgBox <span style="color: #800000;">&quot;Hello World!&quot;</span>, vbOKOnly
answer = MsgBox(<span style="color: #800000;">&quot;Hello World?&quot;</span>, vbYesNo)</pre></div></div>

<p>Did you notice anything? Yes, indeed. Take note, the parentheses are forbidden in the first case, and mandatory in the second. Now assume that you have a function call that you suddenly need the answer from, whereas you didn&#8217;t need it earlier. Not only do you now have to write the assignment, but you also have to add parentheses to the function call. Way to spend productive time.</p>
<p>The same goes for &#8220;Set&#8221; and assignments to native data types and Objects: For Objects, it&#8217;s mandatory, for native types it&#8217;s forbidden. That goes even for Objects stored in a Variant variable &#8211; all Variants are not made equal, some will throw runtime errors when you assign stuff to them.<br />
<span id="more-496"></span><br />
Another annoying syntax bit is this one:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' Won't work!
</span><span style="color: #000080;">Dim</span> someNumber <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = 5
<span style="color: #008000;">' Will work :-/
</span><span style="color: #000080;">Dim</span> someNumber <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
someNumber = 5</pre></div></div>

<p>Yay, more <a href="http://en.wikipedia.org/wiki/Lines_of_code#Disadvantages">lines of code</a>, how great. But wait, I have more! What do you think this produces?</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> firstVar, secondVar <span style="color: #000080;">As</span> <span style="color: #000080;">Double</span>
<span style="color: #000080;">Dim</span> thirdVar, fourthVar <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span></pre></div></div>

<p>Yep, you guessed it. &#8220;firstVar&#8221; is a Variant, &#8220;secondVar&#8221; a Double, &#8220;thirdVar&#8221; a Variant, &#8220;fourthVar&#8221; an Object. Oh, you got it wrong? Don&#8217;t worry, it&#8217;s just that silly.</p>
<p>My final pet peeve with the syntax is its capitalization (and thus technically the IDE and not VBA itself). VBA is not per se case sensitive &#8211; and it expresses that through simple means: Whenever it encounters a variable, it tries to capitalize it the way it&#8217;s supposed to be, to make it clear that it&#8217;s the same.</p>
<p>Now, enter <a href="http://en.wikipedia.org/wiki/Scope_(programming)">scoping</a>, and intuitively named API bits. So you have a local variable called &#8220;currentItem&#8221; &#8230; what happens if you try to access the property &#8220;CurrentItem&#8221; in, say, an <a href="http://msdn.microsoft.com/en-us/library/aa210923(office.11).aspx">Inspector Object</a>? Right, it&#8217;s capitalized &#8220;currentItem&#8221;. It has nothing at all to do with my local variable, but the IDE is too stupid to notice.</p>
<p>Of course there&#8217;s other things amiss or strange, like the lack of a <a href="http://www.fmsinc.com/free/NewTips/NET/NETtip33.asp">side effect free ternary operator</a>, or the (nearly syntax) VBA developer gospel for the redundant <a href="http://en.wikipedia.org/wiki/Hungarian_notation">Hungarian notation</a> taken to <a href="http://www.joelonsoftware.com/articles/Wrong.html">wrong extremes</a>, but I&#8217;ll just stop here.</p>
<h2>MS Access</h2>
<p>I won&#8217;t even go into how I find Access 2007 to be much worse than its predecessors when it comes to usability, with longer mouse paths and more clicks per action, the <a href="http://office.microsoft.com/en-us/excel/HA101730521033.aspx">developer tab not showing by default</a>, and shortcuts no longer easily visible. Just so much: <strong>it&#8217;s annoying</strong>. I guess I still haven&#8217;t gotten used to the new, less quick, <a href="http://teaandbiscuits.org.uk/drupal/node/8">way of displaying shortcuts</a>, and often, object placements aren&#8217;t intuitive for me and I&#8217;m looking for things in the wrong tab.</p>
<p>What really bothers though is that Access has <strong>stability issues</strong>. Access is the only program so far that managed to crash on my development system. <a href="http://www.adobe.com/products/dreamweaver/">Dreamweaver MX 04</a> is quite an old make and has huge problems with Vista&#8217;s <a href="http://www.microsoft.com/windows/windows-vista/features/user-account-control.aspx">UAC</a>, but it doesn&#8217;t crash. Nor does any other tool. But Access, which happens to often corrupt databases when it does.</p>
<p>Even crashes aside, occasionally the <strong>database just corrupts</strong> like that, without any visible reason. Or it keeps code that you deleted, and keeps spitting error messages how that code isn&#8217;t valid &#8211; without you having any possibility to do anything about it, not even overwriting helps.</p>
<p>I hope you make backups every couple minutes, I certainly learned to. I haven&#8217;t found another reliable way to get those databases back into stable conditions.</p>
<p>Then, what&#8217;s one of the many responsibilities of a proper <a href="http://en.wikipedia.org/wiki/Database_management_system">DBMS</a>? <strong>Housekeeping</strong>! What does Access do? Provide you with &#8220;compact and repair&#8221;, which is user-initiated <em>only</em>. Well, I guess with huge databases that Access couldn&#8217;t handle anymore anyway, that would make sense, not having that an automated regular task.</p>
<p>Furthermore, every single user <strong>needs MS Access installed</strong> on his computer. Potentially a security risk, certainly not something that should be necessary. You don&#8217;t go and install an MS SQL Server on everybody&#8217;s machine. At least Microsoft consequently follows through, and their <a href="http://office.microsoft.com/en-us/access/HA011225981033.aspx">Access security FAQ</a> is all about how you can protect your computer from malicious Access macros, and not how you can secure your Access databases.</p>
<p>By the way, <strong>Access doesn&#8217;t scale</strong> &#8211; what else is new. <a href="http://databases.aspfaq.com/database/what-are-the-limitations-of-ms-access.html">Multiple users are a problem</a>, for larger databases (&gt;<a href="http://support.microsoft.com/kb/302524/">2GB</a>, but you&#8217;re hitting <a href="http://www.programmingado.net/a-381/Performance-testing-Access-MySQL-MSSQL.aspx">performance bottlenecks</a> way before that) you have to move to a proper backend &#8211; essentially, lugging around the whole DBMS part of your GUI windowing toolkit for naught afterwards.</p>
<p>It does at least have <a href="http://mcs.open.ac.uk/kgw9/interesting/AccessLocking.html">proper locking</a>, quite refined <a href="http://www.databasedev.co.uk/permissions.html">permissions</a>, and with Access 2007, <a href="http://databases.about.com/od/productinfo/a/encryption.htm">database encryption</a> (without which all the permission structures in the world are worth naught) as well.</p>
<h2>Other Things</h2>
<p>There&#8217;s a few other small things that I found incredibly annoying, coming from other languages like <a href="http://java.sun.com/">Java</a>, <a href="http://www.python.org/">Python</a> and my personal favourite, <a href="http://www.ruby-lang.org/en/">Ruby</a>.</p>
<p>For one, <a href="http://wesnerm.blogs.com/net_undocumented/2005/10/null_comparison.html"><strong>Null comparisons</strong> work in odd ways</a>, VBA uses <a href="http://en.wikipedia.org/wiki/Ternary_logic">ternary logic</a> (although admittedly, this is consistent with the way SQL handles Nulls).</p>
<p>The next minor thing are <strong>silly defaults</strong> &#8211; there are two lines every halfway serious programmer adds as a very first thing to every single bit of VBA code they write (and this goes so far that there&#8217;s option settings to add it automatically to every source bit):</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
<span style="color: #000080;">Option</span> Compare Database</pre></div></div>

<p>The reason for this is simple: backwards compatibility. While VB6 (maybe earlier?) changed the default to the need for explicit variable declaration, VBA never did, and thus it&#8217;s still possible to mistype a simple variable and never find the error later on. And as for Compare Database &#8230; yeah sure, let&#8217;s compare stuff everywhere a bit differently, to make sure the database behaves completely unforeseeable.</p>
<p>Another one: <strong>No external source file support</strong>. If you want to use any library or other cool thingy you found on the net or elsewhere, you have to copy-paste the code into Access. Where it&#8217;s needed, that is, which of course means that if it&#8217;s needed anywhere else, you have to copy-paste it again. The only way around this are Access databases that allow you to copy-paste (oh, look, a pattern) the respective classes or modules as a whole. No, you can&#8217;t just link (with <a href="http://www.access-programmers.co.uk/forums/showthread.php?t=27139">very limited exceptions</a>). Hooray?</p>
<p>We&#8217;re not done yet: You <strong>can&#8217;t return an array</strong>! If you want a Function that returns an array, you have to define it as returning a Variant. If you do that, you can return arrays (since they&#8217;re a subtype of Variant), but will lose all meta information: Data type, size, everything. So if you want to return an array and its size, you have to do it via separate functions, or with a stateful returning object, or with a custom ADT.</p>
<p>Something that was rightfully pointed out to me (thanks Ste!) &#8211; why is it that VBA (and VB.NET, for that matter) distinguishes between <strong>Functions and Subs</strong>? There&#8217;s no fundamental difference, one returns something, the other doesn&#8217;t (so, essentially returns Nothing), but that&#8217;s it. Visual Studio goes so far to automatically change the keyword from Sub to Function when you return something.</p>
<h2>Conclusion</h2>
<p>I guess it&#8217;s apparent by now: I do think that it&#8217;s justifiable to say that VBA is outdated and should be dumped right now.</p>
<p>For Microsoft: There are good replacements, there&#8217;s no reasonable reason beyond &#8220;it was easier&#8221; to have Office 2007 use VBA still, and there&#8217;s certainly no reason to make the next version of MS Office still have VBA support &#8211; particularly if that means that there still won&#8217;t be native VB.NET support inside MS Office.</p>
<p>How delusional of me to think they&#8217;ll actually see this, though <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Whats Wrong With VBA? (3/3)" /> </p>
<p>For anybody considering Access and VBA for their projects: Well, if you still want to, you can of course. Or get the free <a href="http://www.microsoft.com/express/vb/Default.aspx">Visual Studio Express for VB.NET</a> plus <a href="http://www.microsoft.com/express/sql/Default.aspx">SQL Server Express</a>, and stick to a Microsoft environment with a very similar but much more mature language, and a more performant database, for less money.</p>
<p>I still do work with VBA, when I really have to, but I think I laid out here why it&#8217;s a fact that I really only do so when there is no other way.</p>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-33/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (2/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-23/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-23/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 11:08:11 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=494</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3
This is the second part of a three-part series, have a look at part 1 first if you haven&#8217;t read it yet.
Let&#8217;s just jump right in and continue the list of problems I have with VBA as a programming language for medium and larger MS Access projects.
Error Handling
The [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p>This is the second part of a three-part series, have a look at <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> first if you haven&#8217;t read it yet.</p>
<p>Let&#8217;s just jump right in and continue the list of problems I have with VBA as a programming language for medium and larger MS Access projects.</p>
<h2>Error Handling</h2>
<p>The modern paradigm for error handling is <a href="http://en.wikipedia.org/wiki/Exception_handling">exception handling</a>, but to VBA that doesn&#8217;t apply for a simple reason: VBA doesn&#8217;t have exceptions. Instead, it <a href="http://msdn.microsoft.com/en-us/library/5hsw66as(VS.80).aspx">has this</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> ErrorHandler</pre></div></div>

<p>Yes, that&#8217;s a GoTo, and we all know <a href="http://xkcd.com/292/">those are evil</a>. Even worse than GoTo though is this line:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span></pre></div></div>

<p>Basically, &#8220;just ignore all the errors that happen now, they&#8217;re not important&#8221;. Ah, right. We can still check for error codes, so even this evil construct (<a href="http://www.developerfusion.co.uk/show/4325/">never ever</a> use this as default!) is occasionally necessary to avoid the spaghetti code that the GoTo alternative would produce, while providing reasonable feedback instead of the techy default Access warnings:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">DoCmd.SetWarnings <span style="color: #000080;">False</span>
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span>
doSomethingUseful
<span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> Err.Number = 0 <span style="color: #000080;">Then</span>
    MsgBox <span style="color: #800000;">&quot;Descriptive error message&quot;</span>, vbCritical, <span style="color: #800000;">&quot;Error&quot;</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0
DoCmd.SetWarnings <span style="color: #000080;">True</span></pre></td></tr></table></div>

<p>What a silly contraption. In VB.NET, it&#8217;d look like this, and be way more flexible and expandable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Try</span>
    doSomethingUseful<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception
    <span style="color: #0600FF;">MsgBox</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Descriptive error message&quot;</span>, MsgBoxStyle.<span style="color: #0600FF;">Critical</span>, <span style="color: #808080;">&quot;Error&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span></pre></td></tr></table></div>

<p><span id="more-494"></span>The current consensus among programmers is that <a href="http://david.tribble.com/text/goto.html">GoTo isn&#8217;t necessarily evil</a>, but it has to be used the right way. Myself, I have yet to see a case where I find GoTo to be better than its alternatives, but that&#8217;s subjective I guess. Excursus: I couldn&#8217;t resist solving the <a href="http://david.tribble.com/text/goto.html#conclusion">Last Example &#8211; Nontrivial Gotos</a> from that last link, with less C code and no GoTo and, in my opinion, clearer:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> parse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>Token tok <span style="color: #339933;">=</span> gettoken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> END<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>shift<span style="color: #009900;">&#40;</span>tok<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>reduce<span style="color: #009900;">&#40;</span>tok<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> ERROR<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ACCEPT<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Anyway. VBA provides a GoTo, which is neutral, and it forces that GoTo upon programmers for error handling due to lack of alternatives, which is bad. <a href="http://cprogramming.com/tutorial/goto.html">Only C gets away with that</a>.</p>
<h2>The IDE</h2>
<p>The Access VBA <a href="http://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> basically consists of two parts (and thus technically isn&#8217;t really integrated, but we&#8217;ll let that slide): The in-Access editor (or layouter) for forms and reports, and the code editor for actual programming.</p>
<p>The layouter works quite well really, except that the one even in the free <a href="http://www.microsoft.com/Express/">Visual Studio Express</a> (which isn&#8217;t even the central part of that IDE, unlike in Access) is lightyears ahead, be it concerning auto-snaps or ease of use or number of clicks for anything. Microsoft doesn&#8217;t seem to use its internal synergies all that well.</p>
<p>The same then goes for the code editor, just worse: It hasn&#8217;t gotten an update in recent years, the one that shipped in Office 2007 is exactly the same as the one from Office 2003. Grievances are&#8230;</p>
<ul>
<li>Whenever you have a half-finished line, and switch line to write something somewhere else, you have an error message box pop up that you have to click away first &#8211; because it only checks errors when you leave a line, and has no other way to display them.</li>
<li>Class lookup is shaky, with new custom classes it often it only works after you exit and restart Access.</li>
<li>Even if it works it&#8217;s not completely reliable, working autocompletion is a rare positive surprise.</li>
<li>Debugging is pretty limited, often when there&#8217;s an error the IDE doesn&#8217;t even stop at the point where the error actually occurred but at some point further up the call stack.</li>
</ul>
<p>I can live with those shortcomings of course, but I really don&#8217;t see why Microsoft didn&#8217;t just use the Visual Studio IDE instead &#8211; it would not only have fixed those problems, but also have gained better searching capabilities, snippets, refactoring and wizards to go with it.</p>
<h2>Object Orientation</h2>
<p>VBA is object oriented! Yeah, to some extent: You can make classes, and there&#8217;s even <a href="http://www.developerfusion.co.uk/show/3965/3/">properties</a>. However, yet again there&#8217;s a bullet list of things that should be better:</p>
<ul>
<li>You can&#8217;t have parameters for your class constructors (the <a href="http://www.xtremevbtalk.com/showthread.php?t=224316">Class_Initialize Sub</a>), so initializing a class must be either with default values or through a separate &#8220;init&#8221; function that&#8217;s called after instantiation with &#8220;New&#8221;.</li>
<li>No real <a href="http://en.wikipedia.org/wiki/Inheritance_(computer_science)">inheritance</a>, you can just implement interfaces (and thus inherit method signatures, but no content or properties or anything) &#8211; considering there&#8217;s no compile time type checking anyway, I don&#8217;t see much purpose in that.</li>
<li>No <a href="http://en.wikipedia.org/wiki/Method_overloading">overloading</a>. Yes, you can have <a href="http://vbadud.blogspot.com/2007/04/optional-parameter-example.html">optional parameters</a>, but the same function name with different signatures won&#8217;t work (the language does support encapsulation though, so you can <a href="http://www.utteraccess.com/forums/showflat.php?Board=48&amp;Number=1252455&amp;Main=1252455">override functions in narrower namespaces</a>, but that&#8217;s not really overloading).</li>
<li>Objects are handled different from native data types. Not just &#8220;they are different&#8221;, but they have differing syntax too &#8211; an assignment to an object needs &#8220;Set&#8221;, one to a Variant must not have &#8220;Set&#8221;. So even though you can store an Object in a Variant, you&#8217;ll run into runtime errors if you do. Admittedly, Java shares similar problems, but at least it has <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html">autoboxing</a> meanwhile.</li>
</ul>
<p>Some people also have a problem with VBA having both modules <em>and</em> classes &#8211; myself, I see that more pragmatically: There are cases when you&#8217;d make just static functions in a class anyway, or work just with a singleton, so why not pack those in a module instead? You can always change to a class structure later if it becomes necessary.<br />
<a name="community" /><br />
<h2>The Community</h2>
<p>Now this one is certainly controversial, and even more subjective than the rest of this post series. First off, there are quite a few great community sites full of dedicated and knowledgable authors, like the <a href="http://msdn.microsoft.com/en-us/vbasic/cc546545.aspx">MS VB forums</a>, <a href="http://www.databasedev.co.uk/">databasedev.co.uk</a>, <a href="http://www.vb-helper.com/index.html">VB Helper</a>, <a href="http://www.mvps.org/access/">The Access Web</a>, <a href="http://www.freevbcode.com/">FreeVBCode</a>, the <a href="http://www.tek-tips.com/threadminder.cfm?pid=707">TekTips VBA section</a> and the <a href="http://www.fmsinc.com/free/tips.html#TipsforAccess">FMS tips section</a>.</p>
<p>But what I&#8217;m talking about is the &#8220;Googlability&#8221; of problems.</p>
<p>If I don&#8217;t find a solution in a site I know (or sometimes while looking in one of those sites, with &#8220;site:foobar.com&#8221;), I just Google it (<a href="http://www.justfuckinggoogleit.com/">of course</a>) &#8211; problems do occur, and others must have had the same ones before. Now what happens if I do that for VBA problems?</p>
<p>I find trash.</p>
<p>This is for these three reasons, I guess:</p>
<ul>
<li>There isn&#8217;t so much a VBA community as 4 VBA communities: ASP VBA, Excel VBA, Word VBA, and finally also Access VBA (about in that order of prominence) &#8211; and then there&#8217;s the VB6 and VB.NET community too, with solutions that often don&#8217;t apply to VBA.<br />
So the search queries have to get more and more elaborate, adding &#8220;-asp&#8221; and &#8220;-excel&#8221; is common if you want to find anything &#8211; thus it takes longer until you find your solutions.</li>
<li>There are plenty of solution providers that might provide reasonable solutions, but cost money. Now, that&#8217;s a problem of mine I guess, but I don&#8217;t like giving my credit card info to some obscure website for a solution that might or might not work for me. Luckily, <a href="http://www.customizegoogle.com/">CustomizeGoogle</a> filters those out quite reliably, after I tell it to.</li>
<li>There are plenty of inexperienced users when it comes to VBA, way more so than with most other programming languages and other technologies I used so far (well, <a href="http://en.wikipedia.org/wiki/Python_(programming_language)">Python</a> and <a href="http://en.wikipedia.org/wiki/Ruby_(programming_language)">Ruby</a> tend to have geeky target audiences anyway, but the same applies to <a href="http://en.wikipedia.org/wiki/Java_(programming_language)">Java</a>). This means two things:
<ul>
<li>Often, questions are answered less than optimal by only slightly more knowledgable users than the ones asking the question, meaning the solution is either just wrong or not generic and thus doesn&#8217;t work in a slightly altered context.</li>
<li>Even more often, questions aren&#8217;t answered at all. The favourites here are the people later writing &#8220;nevermind, I found the solution&#8221;, without providing <em>what</em> that solution was that I could use now as well&#8230;</li>
</ul>
</li>
</ul>
<p>Keep in mind that I don&#8217;t have any problems with inexperienced users whatsoever, I&#8217;m regularly a newbie in yet another programming language I&#8217;m learning myself. And asking questions is a good thing anyway &#8211; it&#8217;s just that it seems like more experienced VBA programmers move on to VB.NET or something and thus VBA queries often disappear in the void, because there&#8217;s nobody experienced around to answer them.</p>
<p>That&#8217;s all for now, stay tuned for <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3 with syntax, MS Access, other things and a conclusion</a>.</p>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-23/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#8217;s Wrong With VBA? (1/3)</title>
		<link>http://www.haslo.ch/blog/whats-wrong-with-vba-13/</link>
		<comments>http://www.haslo.ch/blog/whats-wrong-with-vba-13/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 13:56:11 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scorchers]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=267</guid>
		<description><![CDATA[part 1 &#8211; part 2 &#8211; part 3
Visual Basic for Applications &#8211; the legacy language that Microsoft apparently still won&#8217;t drop even in future Office updates, although they&#8217;re working on it, and certainly hasn&#8217;t dropped in Office 2007. The plague with a lobby. Why is it so bad, is it so bad at all?
This very [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
<p><a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">Visual Basic for Applications</a> &#8211; the legacy language that Microsoft apparently still won&#8217;t drop even in future Office updates, although they&#8217;re working on it, and certainly hasn&#8217;t dropped in Office 2007. The plague with a <a href="http://classicvb.org/petition/">lobby</a>. Why is it so bad, is it so bad at all?</p>
<p>This very subject here was, in fact, one of the things that made me think &#8220;I have to get a blog again&#8221; over those years when I didn&#8217;t have one &#8211; I so wanted to write it whenever I was forced to work with applications coded inside <a href="http://office.microsoft.com/en-us/access/">MS Access</a>, or when I coded them myself from scratch. I decided to make what started as a single post into a three part series, simply because there was so much content that one blog post couldn&#8217;t hold it all.</p>
<p>I won&#8217;t go into VBA for the rest of the Office suite too much, for tiny macro things it might even fit, what I&#8217;m talking about is medium (and larger) applications. And I will use the term &#8220;database&#8221; for the course of this article series for something it technically doesn&#8217;t strictly apply to, which is a MS Access application and actually an entire <a href="http://en.wikipedia.org/wiki/Database_management_system">DBMS</a>, plus GUI stuff and application logic, all mashed together.</p>
<p>So, what are all those things that bug me so much?<br />
<span id="more-267"></span></p>
<h2>Language Updates</h2>
<p>That&#8217;s possibly the biggest gripe of them all. Quoting the very <a href="http://msdn.microsoft.com/en-us/isv/bb190538.aspx">Microsoft VBA site</a>, at least MS makes their stance towards VBA perfectly clear:</p>
<blockquote><p>[...] As previously announced, Microsoft does not expect to make significant enhancements to VBA. [...]</p>
<p>As of July 1, 2007, Microsoft will no longer offer VBA distribution licenses to new customers. Existing VBA customers can still purchase additional VBA licenses from Summit Software and Microsoft for existing solutions.</p></blockquote>
<p>In other words, &#8220;we&#8217;re desparately trying to get rid of this mess&#8221;. Well, why wouldn&#8217;t they: VBA wasn&#8217;t updated for nearly 10 years now &#8211; while programming languages evolved around it, some very well-designed behemoths (for the time they were designed at) like <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a> still stand their ground, many others have vanished and were replaced.</p>
<p>Yes, of course there&#8217;s always a demand for coders for older languages, just look at <a href="http://en.wikipedia.org/wiki/Cobol">COBOL</a>. If you like VBA, there&#8217;s plenty of legacy Access applications, and <a href="http://www.fmsinc.com/TPapers/genaccess/DBOD.asp">places where Access makes sense</a>, so you won&#8217;t be out of a job any time soon.</p>
<p>That doesn&#8217;t change that the language would be in desparate need of an update, wouldn&#8217;t the update already exist in the form of <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET">VB.NET</a> and, to a lesser extent, in <a href="http://en.wikipedia.org/wiki/C_Sharp_(programming_language)">C#</a>. Why Microsoft hasn&#8217;t at least added support for those inside MS Access in the course of introducing <a href="http://www.microsoft.com/NET/">.NET</a> is beyond me.</p>
<h2>The API</h2>
<p>What clearly does belong to the VBA API, particularly in our context of developing MS Access applications, is the windowing framework. While it has its very own problems that I&#8217;ll describe in the IDE chapter, it does what it&#8217;s supposed to do rather well: Forms, reports and components, with a nice drag-and-drop interface for tinkering interfaces. It doesn&#8217;t have the best performance out there, but for database applications that work, who makes that their top priority.</p>
<p>Beyond those widgets and happy drag-and-drop things, for the most part, the Application Programmer Interface of VBA can be described with a few simple words: It doesn&#8217;t exist. While VBA does make use of <a href="http://en.wikipedia.org/wiki/Data_Access_Objects">DAO</a> and <a href="http://en.wikipedia.org/wiki/ActiveX_Data_Objects">ADO</a> (not the way better <a href="http://en.wikipedia.org/wiki/ADO.NET">ADO.NET</a>, mind you), and the <a href="http://support.microsoft.com/kb/222101">Office object model</a> (which is actually more comfortable to use in VBA than in C#), everything that goes beyond that is out of bounds.</p>
<p>Let me illustrate by means of a symptomatic example.</p>
<p>Let&#8217;s assume you want a dynamically sized array, a Vector or ArrayList, that grows and shrinks as you add and remove items, preferrably useable as a <a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">stack</a> or <a href="http://en.wikipedia.org/wiki/Queue_(data_structure)">queue</a> too by means of simple procedures. Now, in every other language that&#8217;s as simple as importing a bit of the API and then using said dynamic array. Not so in VBA, that bit of the API doesn&#8217;t exist (except if you count the <a href="http://msdn.microsoft.com/en-us/library/a1y8b3b3(VS.80).aspx">Collection</a> that has whooping 4 methods). There are &#8220;dynamically sized arrays&#8221; through lines like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> myArray(0 <span style="color: #000080;">To</span> arraySize)</pre></div></div>

<p>This does mean though that all the automatic growing and shrinking has to be added by hand, and push and pop don&#8217;t work at all. In short, you have to do everything yourself.</p>
<p>Which I did by the way, you can <a href="http://www.haslo.ch/blog/vba-vector-classes/">download my classes here</a>.</p>
<h2>Mangled Tiers</h2>
<p>Every non-trivial application, particularly multi-user applications, is best handled with <a href="http://en.wikipedia.org/wiki/Multitier_architecture">multiple tiers</a>: A database tier, a client/presentation tier, maybe an application server in between, that&#8217;s usually enough but can be extended. Less is bad, more is more complicated but depending on the application, necessary.</p>
<p>In MS Access, everything is &#8220;in the database&#8221; by default. Now,what does this mean? Basically, it&#8217;s a mess, and sorting things out properly is nearly impossible, whenever you update the database you also update the application and extending the user circle beyond, say, 1-2 users inevitably leads to problems (more on this later).</p>
<p>It is worth noting that these tiers can be somewhat simulated by using multiple databases for different purposes: You can easily have an &#8220;application&#8221; database that just does frontend work, and a &#8220;data&#8221; database. You can then create links from the &#8220;application&#8221; database to the &#8220;data&#8221; database, and have the frontend update independendly of the backend.</p>
<p>That&#8217;s common practice and makes sense, and comes close to true multi tier programming, although it still makes a mess of things because those links are hardcoded file paths and aren&#8217;t as easy to manage as they should be; the &#8220;data&#8221; database&#8217;s location has to be known on a file share, and every user needs write access to that share, which leads to obvious security inconsistencies and further problems that we&#8217;ll explore in the course of part 3.</p>
<p>The problem about paths being hardcoded can of course be solved by writing libraries that update paths, and the tier problem as a whole can to some extent be resolved by having data reside in an MS SQL (or other, through the rather dated <a href="http://en.wikipedia.org/wiki/Open_Database_Connectivity">ODBC</a>) database.</p>
<h2>What&#8217;s MVC?</h2>
<p>This problem is very similar to the last one concerning mangled tiers. Modern applications often use, for maintenance and development reasons, a <a href="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</a> approach. Some languages and frameworks, like <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, even make it really hard not to (which is good, considering the approach makes sense). Now, if you want to create a program that&#8217;s cleanly using that paradigm in VBA, you basically have to ignore all the structure there is and make a new one. The good news is that you can actually do that, of course, and VBA doesn&#8217;t stop you from doing it &#8211; it just doesn&#8217;t help you a bit.</p>
<p>It all starts out so nicely:</p>
<ul>
<li>The <strong>model</strong> is the database, tables and views can be accessed via DAO or ADO</li>
<li>The <strong>view</strong> is WYSIWYG-edited and stashed in Reports and Forms</li>
<li>Every Frame has its own class, akin to its very own <strong>controller</strong></li>
</ul>
<p>Regarding <strong>model</strong>s: Yes, in theory it&#8217;s nice like that. However, as soon as we start having things that go beyond tables and views, we&#8217;re screwed. That goes for simple things like tree view models (that are stored inside the tree view and not really accessible from the outside). You can make <a href="http://www.4guysfromrolla.com/webtech/011401-1.shtml">stand-alone recordsets</a> (aka RAM tables), you can even <a href="http://support.microsoft.com/kb/184397">disconnect recordsets</a> from their source, that&#8217;s good. But overall, the model is in the view and vice versa, splitting the two is hard and in some places impossible.</p>
<p>The <strong>view</strong> only in the WYSIWYG editor is a nice thought for simple things, but when you want to start exchanging components on the fly, you have to write your own view classes (or end in chaos, as many VBA projects do) anyway. Or, the alternative many VBA projects take, have invisible components that you make visible or visible depending on the mode the form currently is in &#8211; a maintenance nightmare. The best approach here is having subforms and exchanging those, that actually works really well too and is my favourite approach. It&#8217;s just not the way VBA and Access make easy, and it can yet again very easily lead to duplicated bits (code or UI components).</p>
<p>Finally, the <strong>controller</strong> &#8211; you can avoid duplicated code and make pretty good controllers if you completely ignore the Frame&#8217;s default classes and only use those for proxying your own controller classes that you write in addition to them. Works well, it&#8217;s just, again, not the way VBA and Access makes easy.</p>
<p>That&#8217;s all for now, we&#8217;re warming up. Stay tuned for&#8230;</p>
<ul>
<li><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">Part 2 with error handling, IDE, object orientation and the community</a></li>
<li><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">Part 3 with syntax, MS Access, other things and a conclusion</a></li>
</ul>
<p style="text-align: center;"><a href="http://www.haslo.ch/blog/whats-wrong-with-vba-13/">part 1</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-23/">part 2</a> &#8211; <a href="http://www.haslo.ch/blog/whats-wrong-with-vba-33/">part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/whats-wrong-with-vba-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VBA Vector Classes</title>
		<link>http://www.haslo.ch/blog/vba-vector-classes/</link>
		<comments>http://www.haslo.ch/blog/vba-vector-classes/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 15:36:25 +0000</pubDate>
		<dc:creator>haslo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[haslo]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[vb]]></category>

		<guid isPermaLink="false">http://www.haslo.ch/blog/?p=416</guid>
		<description><![CDATA[If you&#8217;re anything like me and ever had to code anything in Visual Basic for Applications, you must have felt the need for a proper API with some very basic common ADTs. The most basic of them being, in my opinion, the dynamic array (Vector, ArrayList, List and Collection are often used more or rather [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re anything like me and ever had to code anything in <a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">Visual Basic for Applications</a>, you must have felt the need for a proper API with some very basic common <a href="http://en.wikipedia.org/wiki/Abstract_data_type">ADTs</a>. The most basic of them being, in my opinion, the <a href="http://en.wikipedia.org/wiki/Dynamic_array">dynamic array</a> (Vector, ArrayList, List and Collection are often used more or rather less synonymous). So what would it feel like to be able to write this and have it work?</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> primitiveVector <span style="color: #000080;">As</span> clsVector
<span style="color: #000080;">Set</span> primitiveVector = <span style="color: #000080;">New</span> clsVector
<span style="color: #000080;">Dim</span> myInt <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
myInt = 3
&nbsp;
primitiveVector.AddItem myInt</pre></div></div>

<p>There are of course other people who have made such a thing, and there&#8217;s the VBA <a href="http://msdn.microsoft.com/en-us/library/a1y8b3b3(VS.80).aspx">Collection</a>, but none of them quite fit what I wanted. I wanted a Vector that had random access, was able to handle <a href="http://en.wikipedia.org/wiki/LIFO">LIFO</a> and <a href="http://en.wikipedia.org/wiki/FIFO">FIFO</a> (and thus act as stack and queue), addition and insertion and deletion, could check for item existence, and had dynamic resizing. So I went and coded my own. I did this while working at <a href="http://promino.ch/">Promino AG</a> and thus the code is technically theirs, so thanks to <a href="https://www.xing.com/profile/PaulB_Moser/">Paul Moser</a> for the nod for publishing this under the GNU <a href="http://www.gnu.org/licenses/lgpl.html">Lesser General Public License</a>.</p>
<p><a href="http://www.haslo.ch/media/Vectors.zip"><img src="/media/floppy.png" style="border: none; float: left" title="VBA Vector Classes" alt="floppy VBA Vector Classes" /></a>You can download both classes (two? more below), together with a tiny trivial testing framework, <a href="http://www.haslo.ch/media/Vectors.zip">as Access 2002-2003 .mdb</a>.<br />
<span id="more-416"></span><br />
This here is the vector class for Variants. It goes into a new class module, I called mine &#8220;clsVector&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    CheckDimensions
    itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>
    GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i) = item <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>Now, it would be nice if we would be done. But, we&#8217;re not done yet, obviously, after all there&#8217;s plenty more code below this break. The reason for that is simple: In VBA, a Variant is not an Object, and an Object is not a Variant, consequently functions that take Objects don&#8217;t take Variants and vice versa (although you can technically store an Object in a Variant, this <em>will</em> lead to problems). Objects also happen to have &#8220;Set&#8221; mandatory in places where it isn&#8217;t allowed for Variants.</p>
<p>What this means is that there is no way to have the same Vector class for both your custom ADTs and the primitive data types. So basically, we have another class that has exactly the same functions and exactly the same algorithms, but takes Objects instead of Variants. I called mine &#8220;clsObjectVector&#8221;. Here is the source for the class module:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> Compare Database
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #008000;">' Copyright 2007, 2008 by Promino AG
</span><span style="color: #008000;">' Author: Guido Gloor
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is free software: you can redistribute it and/or modify
</span><span style="color: #008000;">' it under the terms of the GNU Lesser General Public License as published by
</span><span style="color: #008000;">' the Free Software Foundation, either version 3 of the License, or
</span><span style="color: #008000;">' (at your option) any later version.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' This program is distributed in the hope that it will be useful,
</span><span style="color: #008000;">' but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span style="color: #008000;">' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
</span><span style="color: #008000;">' GNU Lesser General Public License for more details.
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' You should have received a copy of the GNU Lesser General Public License
</span><span style="color: #008000;">' along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
</span>
<span style="color: #000080;">Private</span> itemCount <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBufferSize <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #000080;">Private</span> itemBuffer() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Class_Initialize()
    itemCount = 0
    itemBufferSize = 100
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> NewDimensions()
    <span style="color: #000080;">ReDim</span> <span style="color: #000080;">Preserve</span> itemBuffer(itemBufferSize) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CheckDimensions()
    <span style="color: #000080;">If</span> itemCount &gt; itemBufferSize - 2 <span style="color: #000080;">Then</span>
        itemBufferSize = itemBufferSize * 2
        NewDimensions
    <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> ShrinkDimensions()
    itemCount = itemBufferSize + 10
    NewDimensions
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetSize() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    GetSize = itemCount
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> AddItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    CheckDimensions
    <span style="color: #000080;">Set</span> itemBuffer(itemCount) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetItem = itemBuffer(index)
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> PopItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> PopItem = itemBuffer(itemCount - 1)
    DeleteItem itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> GetNextQueueItem() <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>
    <span style="color: #000080;">Set</span> GetNextQueueItem = itemBuffer(0)
    DeleteItem 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> ContainsItem(item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    <span style="color: #008000;">' The used ADTs need to have an Equals function for this to work
</span>    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    ContainsItem = <span style="color: #000080;">False</span>
&nbsp;
    <span style="color: #000080;">For</span> i = 0 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">If</span> itemBuffer(i).Equals(item) <span style="color: #000080;">Then</span> ContainsItem = <span style="color: #000080;">True</span>
    <span style="color: #000080;">Next</span> i
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> InsertItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>, item <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    CheckDimensions
    <span style="color: #000080;">For</span> i = itemCount - 1 <span style="color: #000080;">To</span> index <span style="color: #000080;">Step</span> -1
        <span style="color: #000080;">Set</span> itemBuffer(i + 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    <span style="color: #000080;">Set</span> itemBuffer(index) = item
    itemCount = itemCount + 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> DeleteItem(index <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
    <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">For</span> i = index + 1 <span style="color: #000080;">To</span> itemCount - 1
        <span style="color: #000080;">Set</span> itemBuffer(i - 1) = itemBuffer(i)
    <span style="color: #000080;">Next</span> i
    itemCount = itemCount - 1
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> IsEmpty() <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
    IsEmpty = itemCount &lt;= 0
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></td></tr></table></div>

<p>This is it, have fun <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' title="VBA Vector Classes" />  As much as you can have if you&#8217;re forced to work with VBA, that is.</p>
<p>Oh, and if you can use it &#8211; comments are appreciated <img src='http://www.haslo.ch/blog/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' title="VBA Vector Classes" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.haslo.ch/blog/vba-vector-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
