<?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>Extensible By Design &#187; Extensible By Design</title>
	<atom:link href="http://extensiblecad.com/words/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://extensiblecad.com/words</link>
	<description>SolidWorks and PDMWorks Enterprise Development Blog</description>
	<lastBuildDate>Mon, 02 Feb 2009 09:02:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Macros 301 &#8211; Generate Document Numbers From MS Access Database And Auto Insert Them Into New Documents</title>
		<link>http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/</link>
		<comments>http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 04:04:27 +0000</pubDate>
		<dc:creator>Jeff Cope</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SolidWorks]]></category>
		<category><![CDATA[SolidWorks API]]></category>

		<guid isPermaLink="false">http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/</guid>
		<description><![CDATA[Father forgive me.  It has been two weeks since my last blog post.  I apologize everyone for the long interval but things have been crazy.  In addition to some long nights at my day job, I am in the process of releasing a new version of InspectionXpert, the Quality Inspection Sheet generating software from my [...]]]></description>
			<content:encoded><![CDATA[<p>Father forgive me.  It has been two weeks since my last blog post.  I apologize everyone for the long interval but things have been crazy.  In addition to some long nights at my day job, I am in the process of releasing a new version of <a target="_blank" href="http://www.inspectionxpert.com" title="Generate Quality Inspection Sheets From SolidWorks Fast">InspectionXpert, the Quality Inspection Sheet generating software</a> from my company Extensible CAD.  It is a rewarding endeavor but also very demanding.</p>
<p> Anyways&#8230; not so long ago I posted a <a target="_blank" href="http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/" title="How To Register Your Macro For SolidWorks Events">Macro &amp; tutorial on how to register for SolidWorks events.</a>  One application of that technique that I mentioned was to automatically generate document numbers for your SolidWorks document and insert them into SolidWorks &#8220;auto-magically.&#8221;  Well, not too long thereafter I was bombarded with emails asking me to post that little Macro and I promised to do so.  This post is the fullfillment of that promise.  I&#8217;m sorry it took so long but I think you guys will like it.  I feel it is &#8220;auto-magically delicious&#8221; myself : )</p>
<p><a href="http://extensiblecad.com/words/wp-content/uploads/2008/02/docnumbersfrommsaccess.zip" title="Auto-magically Generate Document Numbers For SolidWorks Documents From An MS Access Database"> You can download the database and the macro from here.</a></p>
<p>Keep reading to see a flash video of the macro in action&#8230;</p>
<p><span id="more-61"></span></p>
<p><script type="text/javascript" src="swfobject.js"></script></p>
<p id="flashcontent"> <strong>The Macro In Full Effizact</strong></p>
<p><script type="text/javascript">                                 var so = new SWFObject("http://extensiblecad.com/words/wp-content/uploads/2008/02/runmacro.swf", "Run Macro", "800", "600", "8", "#336699");     so.write("flashcontent");</script></p>
<p>Disclaimer:  As I am pretty pressed for time, as usual, I have not added any error handling to this macro.  You will have to do that yourselves.  At some point in the future (here I go making promises again), I will add a post on error handling with VBA. </p>
<p>What this Macro does:  If you didn&#8217;t understand the movie then (please check your home&#8217;s water for lead contamination) this macro registers with SolidWorks to receive notification (aka &#8216;callback&#8217;) when a new file is created in SolidWorks (i.e. File, New).  The official Event is &#8216;FileNewNotify2&#8242; and when fired, the macro will determine the type of file (e.g. SLDASM, SLDDRW or SLDPRT) and then use ActiveX Data Objects, ADO, to contact an MS Access database to find out the next available document number for that document type.  The document number is then written to the new document title, in essence the filename, and then the number in the database is incremented by one so that next time the document will have a unique number.</p>
<p> How Do You Set It Up?</p>
<ol>
<li> Create a database (I used VBA and MS Access because they are friendlier for newbs but SQLServer Express would be more appropriate for a production solution) that contains three colums;  a primary key (i.e. unique identifier for each record), a lookup key (we used file extension) and a column to hold the next number in sequence for each record-type.  In other words, we have a number series for each file type and the number gets incremented by one everytime a number is assigned.</li>
<li>Determine the connection string to connect to the database.  The easiest way to do this is with a little trick I picked up from an awesome SQL Server book (name escapes me right now). 
<ol>
<li>First you create a new text file and then you change the extension from .txt to .udl</li>
<li>Double-click on the file to open it and you will be prompted with a dialog that you can use to test and obtain the connection.  It is a completely un-intuitive GUI but it is better than the typing it by hand.</li>
<p><strong>Getting the Conection String To The Database</strong><br />
<a target="_blank" href="http://extensiblecad.com/words/wp-content/uploads/2008/02/create-connection-string.swf" title="Video Demonstration Of How To Obtain A Database Connection String">Here is a link to a flash video showing how to get the connection string. </a> I tried to add it to the post but I can only have one flash in the same post with the flash video player that I am using.  FYI &#8211; I tried a few flash players but I&#8217;m not that happy with any of them.  If anyone is aware of a stellar flash plugin for wordpress, then please suggest it. </p>
<li>Write a macro that connects to the database using the connection string gained from step #2.  To get the connection string, you just open the .udl file in notepad and copy out the text in the last row.  The macro should obtain an ADO Recordset object from the document numbers table.  I used a query as my filter but you there are other ways.  The query is the simplest.  I designed my query in Access&#8217; query builder GUI and copy/pasted the text from there to the macro.</li>
<li>Test the macro and verify that it is retrieving the correct value (i.e. from the correct row and column) and that it is correctly incrementing the value for next time.</li>
<li>Once you are correctly accessing the database and obtaining the correct information, write a SolidWorks macro to register for notification of the <strong>File, New</strong> event.  It is officially known as the FileNewNotify2 event.  If you don&#8217;t know how to do this, then see <a target="_blank" href="http://extensiblecad.com/words/2007/11/01/macros-201-how-to-register-your-solidworks-macro-to-receive-notifications-for-solidworks-events/" title="Register Your SolidWorks Macro To Receive Event Notifications From SolidWorks">my post on registering your macro to receive event notifications from SolidWorks.</a></li>
<li>Once your new macro is getting called reliable for the FileNewNotify2 event, copy the code from the first macro (the database macro) into the new macro.  Add the database function so that it is called by your FileNewNotify2 event handler.  The new macro should then copy the document number returned from the database function to the new document&#8217;s title.</li>
<p>That&#8217;s it.  That wasn&#8217;t too bad now was it?</p>
<p>Note:  If anyone wants to turn this into a SolidWorks Add-in then you can post it here for the masses.  I will give you full credit.  I am too busy right now to do that (unless it was for money :  )</ol>
</li>
</ol>
	<p></p>
	<hr noshade style="margin:0;height:1px" />
	<small><p>&copy; Jeff Cope for <a href="http://extensiblecad.com/words">Extensible By Design</a>, 2008. |
	<a href="http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/">Permalink</a> |
	<a href="http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/#comments">7 comments</a> |
	Add to
	<a href="http://del.icio.us/post?url=http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/&amp;title=Macros 301 &#8211; Generate Document Numbers From MS Access Database And Auto Insert Them Into New Documents"><img src="http://yoursite.com/images/icons/delicious.gif" border="0"> del.icio.us</a>
	<a href="http://www.digg.com/submit"><img src="http://yoursite.com/images/digg.gif" border="0"> digg</a><br/>
	Who's linking ?
	<a href="http://www.technorati.com/search/http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/" title="Search on Technorati"><img src="http://yoursite.com/images/icons/technorati.gif" border="0">Technorati</a>
	<a href="http://www.blogpulse.com/search?query=http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/" title="Search on Blogpulse"><img src="http://yoursite.com/images/icons/blogpulse.gif" border="0">BlogPulse</a>
	<a href="http://google.com/blogsearch?q=http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/" title="Search on Google Blog Search"><img src="http://yoursite.com/images/icons/google.gif" border="0">Google</a><br/>
	Want more on these topics ? Browse the archive of posts filed under <a href="http://extensiblecad.com/words/category/database/" title="View all posts in Database" rel="category tag">Database</a>, <a href="http://extensiblecad.com/words/category/solidworks/" title="View all posts in SolidWorks" rel="category tag">SolidWorks</a>, <a href="http://extensiblecad.com/words/category/solidworks/solidworks-api/" title="View all posts in SolidWorks API" rel="category tag">SolidWorks API</a>.</small></p>
<p>Copyright 2007 <a href=http://extensiblecad.com/words>Extensible By Design.</a>  All rights reserved.</p>]]></content:encoded>
			<wfw:commentRss>http://extensiblecad.com/words/2008/02/09/macros-301-generate-document-numbers-from-ms-access-database-and-auto-insert-them-into-new-documents/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

