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

<channel>
	<title>Ms Access VBA Tutorial</title>
	<atom:link href="http://msaccessvbatutorial.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://msaccessvbatutorial.com</link>
	<description>MS Access Programming - MS Access VBA with Real-World Examples</description>
	<pubDate>Sun, 27 Dec 2009 17:27:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to Object Oriented Programming (OOP) with MS Access VBA</title>
		<link>http://msaccessvbatutorial.com/blog/introduction-to-object-oriented-programming-with-ms-access-vba/</link>
		<comments>http://msaccessvbatutorial.com/blog/introduction-to-object-oriented-programming-with-ms-access-vba/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 13:27:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://msaccessvbatutorial.com/?p=39</guid>
		<description><![CDATA[If you have read my Open Letter to MS Access Users, you&#8217;d know why it is important to introduce Object Oriented Programming within your MS Access Application: because your IT and even yourself are fed up with the Mess so let&#8217;s stop it :).
Nevertheless Object Oriented Programming may be hard for people accustomed to Procedural [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://msaccessvbatutorial.com/files/2009/12/oop.gif" alt="oop" width="120" height="90" class="alignleft size-full wp-image-66" />If you have read my <a href="http://msaccessvbatutorial.com/about/" target="_blank">Open Letter to MS Access Users</a>, you&#8217;d know why it is important to introduce Object Oriented Programming within your MS Access Application: because your IT and even yourself are fed up with the Mess so let&#8217;s stop it :).</p>
<p>Nevertheless Object Oriented Programming may be hard for people accustomed to Procedural Programming because Object Oriented Programming is Non-Linear whereas the old way is Linear or Sequential. So why would you want to complicate things ? Because of the Divide-and-Conquer law. By splitting your code into Objects, you can better maintain each component separately by concern and not mix everything like GUI Interface with your Business Logics.</p>
<p>So in this lesson, I&#8217;m going to introduce you VBA Class Module and I am supposing that you know at least a bit of VBA (I will surely write some basic VBA tutorial but since there are already plenties, you can probably find one without my help).</p>
<p>The first thing you should know is that there is a type of module which is a class module. Under MS Access 2007 you just have to click on this Icon&#8217;s Submenu:<br />
<img src="http://msaccessvbatutorial.com/files/2009/12/msaccess-vba-class-module.gif" alt="msaccess-vba-class-module" width="151" height="226" class="aligncenter size-full wp-image-49" /></p>
<p>Once inside, you can create and rename the default class1 module:<br />
<img src="http://msaccessvbatutorial.com/files/2009/12/msaccess-vba-class-module-open.gif" alt="msaccess-vba-class-module-open" width="460" height="324" class="aligncenter size-full wp-image-51" /></p>
<p>Let&#8217;s rename it to clsPerson (Best Practice in Naming Convention is to prefix each class name with cls) as we&#8217;re going to create some properties for it:</p>
<pre class="brush: vb;">
Private Id As Long
Private FirstName As String
Private LastName As String
Private BirthDate As Date
</pre>
<p><img src="http://msaccessvbatutorial.com/files/2009/12/msaccess-vba-cls-class-module.gif" alt="msaccess-vba-cls-class-module" width="489" height="319" class="aligncenter size-full wp-image-56" /></p>
<p>To access these properties, you need to create an object or instance first (as a class is just a template for creating objects) with the new instruction like this:</p>
<pre class="brush: vb;">
Set oPerson = New clsPerson
</pre>
<p>so that you could access an oPerson Property like this (using the Immediate Window which is a kind of VBA console):</p>
<pre class="brush: vb;">
Debug.Print oPerson.Id
</pre>
<p>Oops, something&#8217;s wrong:<br />
<img src="http://msaccessvbatutorial.com/files/2009/12/access-private-properties.gif" alt="access-private-properties" width="540" height="313" class="aligncenter size-full wp-image-58" /></p>
<p>Why ? Because you set the properties as Private, remember ? So let&#8217;s turn them into Public:</p>
<pre class="brush: vb;">
Public Id As Long
Public FirstName As String
Public LastName As String
Public BirthDate As Date
</pre>
<p>This time it works as the Immediate Command Window returns 0 (by default a Long Property will be inialized automatically to zero by default):<br />
<img src="http://msaccessvbatutorial.com/files/2009/12/access-private-properties-public.gif" alt="access-private-properties-public" width="269" height="228" class="aligncenter size-full wp-image-60" /></p>
<p>That&#8217;s fine now isn&#8217;t it &#8230; except we&#8217;ve just defeated one of the purpose of good OOP which is to protect the properties from the external world by controlling their access (like locking your house with a key to forbid people to steal your assets). So go back to private please !</p>
<p>But wait how are we going to access them ? Well, we&#8217;re going to create Accessors or Getters and Setters (we&#8217;ll see in the future that this also is evil but it is less evil than outright Public Properties and let&#8217;s not be too harsh for a beginning). Getters and Setters are like Doors that you must open to access the private properties and are created with Property Let / Property Get:</p>
<pre class="brush: vb;">
Private m_lId As Long
Private m_sFirstName As String
Private m_sLastName As String
Private m_dtBirthDate As Date

Public Property Get Id() As Long

    Id = m_lId

End Property

Public Property Let Id(ByVal lId As Long)

    m_lId = lId

End Property

Public Property Get FirstName() As String

    FirstName = m_sFirstName

End Property

Public Property Let FirstName(ByVal sFirstName As String)

    m_sFirstName = sFirstName

End Property

Public Property Get LastName() As String

    LastName = m_sLastName

End Property

Public Property Let LastName(ByVal sLastName As String)

    m_sLastName = sLastName

End Property

Public Property Get BirthDate() As Date

    BirthDate = m_dtBirthDate

End Property

Public Property Let BirthDate(ByVal dtBirthDate As Date)

    m_dtBirthDate = dtBirthDate

End Property
</pre>
<p>Yawn &#8230; All this for just that ? Are you kidding ? Well I know, that seems overkilled but OOP is a Paradigm shift and you need some times to get accustomed to all this. And don&#8217;t worry if it&#8217;s so verbose, I&#8217;ll show you in future lesson how you can use a nice tool to cut off this burden so that even Visual Studio Professional Coders would envy you <img src='http://msaccessvbatutorial.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Good Practice, see you next !</p>
]]></content:encoded>
			<wfw:commentRss>http://msaccessvbatutorial.com/blog/introduction-to-object-oriented-programming-with-ms-access-vba/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to relink MS Access Tables</title>
		<link>http://msaccessvbatutorial.com/blog/how-to-relink-ms-access-tables/</link>
		<comments>http://msaccessvbatutorial.com/blog/how-to-relink-ms-access-tables/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 22:31:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Utitilities]]></category>

		<category><![CDATA[msaccess vba relink programmatically]]></category>

		<category><![CDATA[relink]]></category>

		<guid isPermaLink="false">http://msaccessvbatutorial.com/?p=27</guid>
		<description><![CDATA[It is a Best Practice to split your MS Access File between a Front-End MS Access Application and a Back-End MS Access Database this is to make your Software Maintenance easier but this comes with a little price: if you work on multiple computers at work and at home while not using the same directory, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://msaccessvbatutorial.com/files/2009/11/link.gif" alt="link" width="120" height="90" class="alignleft size-full wp-image-32" />It is a Best Practice to split your MS Access File between a Front-End MS Access Application and a Back-End MS Access Database this is to make your Software Maintenance easier but this comes with a little price: if you work on multiple computers at work and at home while not using the same directory, you may encounter the problem of broken links between the two parts. </p>
<p>Of course you can use MS Access Tool to rebuild the link but this is rather tedious so here&#8217;s a VBA script to do so automatically:</p>
<pre class="brush: vb;">
Public Function RelinkAllAccessTables(SourceDatabaseName )

    Dim DB As DAO.Database
    Dim RS As Recordset

    Set DB = CurrentDb
    SQL = &quot;select name from MsysObjects where type = 6&quot;
    Set RS = DB.OpenRecordset(SQL)
    RS.MoveLast
    Debug.Print RS.RecordCount
    RS.MoveFirst
    i = 0
    Do While Not RS.EOF
        SourceTableName = RS(0)
        Call RelinkLocalTable(SourceTableName, SourceDatabaseName)
        RS.MoveNext
        i = i + 1
        Debug.Print i
    Loop

End Function
</pre>
<p>The RelinkLocalTable essentially redefine the Tabledef Connect property to the new database path:</p>
<pre class="brush: vb;">
Public Function RelinkLocalTable(p_SourceTableName, p_SourceDatabaseName, Optional p_TargetTableName)

    TablePath = CurrentProject.Path &amp; &quot;\&quot;
    SourceDatabaseName = p_SourceDatabaseName

    Dim DB As DAO.Database
    Dim RS As Recordset

    Set DB = CurrentDb()
    Dim tempTableDef As DAO.TableDef

    If IsMissing(p_TargetTableName) Then
        TargetTableName = p_SourceTableName
    Else
        TargetTableName = p_TargetTableName
    End If

    SourceTableName = p_SourceTableName
    SourceDatabaseName = p_SourceDatabaseName

    Set tempTableDef = DB.TableDefs(TargetTableName)
        If tempTableDef.SourceTableName  &quot;&quot; Then
            tempTableDef.Connect = &quot;;DATABASE=&quot; &amp; TablePath &amp; SourceDatabaseName
            tempTableDef.RefreshLink
      End If

End Function
</pre>
<p>To use the RelinkAllAccessTables function just call for example:</p>
<pre class="brush: vb;">
call RelinkAllAccessTables(&quot;Northwind.mdb&quot;)
</pre>
<p>For more info see:<br />
<a href="http://www.databasejournal.com/features/msaccess/article.php/3586401/Automatic-Table-Relinking.htm" target="_blank">Automatic Table Relinking</a></p>
]]></content:encoded>
			<wfw:commentRss>http://msaccessvbatutorial.com/blog/how-to-relink-ms-access-tables/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to delete all tables programmatically</title>
		<link>http://msaccessvbatutorial.com/blog/how-to-delete-all-tables-programmatically/</link>
		<comments>http://msaccessvbatutorial.com/blog/how-to-delete-all-tables-programmatically/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 17:58:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Utitilities]]></category>

		<category><![CDATA[delete]]></category>

		<guid isPermaLink="false">http://msaccessvbatutorial.com/?p=5</guid>
		<description><![CDATA[Let&#8217;s say you have exported all your tables to another database and now want to delete them in current database. Unfortunately for security reason, Microsoft doesn&#8217;t facilitate this task by allowing you to select all tables and delete them all at once: if done manually you have to do it one by one.
The VBA function [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://msaccessvbatutorial.com/files/2009/10/delete.gif" alt="delete" width="120" height="90" class="alignleft size-full wp-image-35" />Let&#8217;s say you have exported all your tables to another database and now want to delete them in current database. Unfortunately for security reason, Microsoft doesn&#8217;t facilitate this task by allowing you to select all tables and delete them all at once: if done manually you have to do it one by one.</p>
<p>The VBA function below will query the MsysObjects table to get the list of all tables (type = 1) in the current database:</p>
<pre class="brush: vb;">
'call deleteAllTables
Public Function deleteAllTables()

    Dim DB As Database
    Dim RS As Recordset
    Dim Table As String

    Set DB = CurrentDb()
    Set RS = DB.OpenRecordset(&quot;Select name from MsysObjects where (type = 1 and name not like 'Msys*')&quot;)

    Do While Not RS.EOF
        TableName = RS(&quot;Name&quot;)
        On Error Resume Next
        DoCmd.DeleteObject acTable, TableName
        RS.MoveNext
    Loop
    RS.Close
    MsgBox (&quot;All Tables have been deleted&quot;)
End Function
</pre>
]]></content:encoded>
			<wfw:commentRss>http://msaccessvbatutorial.com/blog/how-to-delete-all-tables-programmatically/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
