{"id":76,"date":"2012-03-16T21:53:23","date_gmt":"2012-03-17T02:53:23","guid":{"rendered":"http:\/\/en.latindevelopers.com\/ivancp\/?p=76"},"modified":"2014-03-19T20:17:43","modified_gmt":"2014-03-20T01:17:43","slug":"ms-access-to-mysql-with-relationships","status":"publish","type":"post","link":"http:\/\/en.latindevelopers.com\/ivancp\/2012\/ms-access-to-mysql-with-relationships\/","title":{"rendered":"Converting MS Access to MySQL with relationships"},"content":{"rendered":"<p>I have used tools like MySQL Migration Toolkit (*) and  Bullzip&#8217;s Access To MySQL utility, both do a excellent job but without relationships. We can spend a lot of hours to identifying and creating  relationships until now:<\/p>\n<p>I have write a VBA script to identify MS-Access relationships and create MySQL code with SQL-CREATE sentences, it will be useful after migration process using any free tool:<\/p>\n<pre lang=\"vb\">'Put this function in new\/existing MS-Access module.\r\n'\r\n' Version History:\r\n'\r\n' 2014-02-09 - Seamus Casey\r\n' a modification to Ivan's handy Access to MySQL relationship\/constraint generator\r\n'\r\n' changes include:\r\n'  1) skip Access system tables (TableDefAttributeEnum.dbSystemObjec)\r\n'  2) add support for cascading updates\/deletes\r\n'\r\n\r\nPublic Sub printRelations()\r\n    Dim sql, fk As String\r\n    Dim I, J As Integer\r\n    Dim db As Database\r\n    Dim Table As TableDef\r\n    Dim TableName As String\r\n    \r\n    ' grab a reference to this once, otherwise when we retrieve a table below,\r\n    ' we will get an 'Object Invalid or No Longer Set' error.\r\n    Set db = CurrentDb\r\n    \r\n    For I = 0 To db.Relations.Count - 1\r\n    \r\n        Set Table = db.TableDefs.Item(db.Relations(I).Table)\r\n        \r\n        If ((Table.Attributes And TableDefAttributeEnum.dbSystemObject) = 0) Then\r\n           \r\n           sql = \"ALTER TABLE `\" & db.Relations(I).ForeignTable & _\r\n               \"` ADD CONSTRAINT `\" & db.Relations(I).Name & \"` FOREIGN KEY (\"\r\n           fk = \"(\"\r\n           For J = 0 To db.Relations(I).Fields.Count - 1\r\n               sql = sql & \"`\" & db.Relations(I).Fields(J).ForeignName & \"` ,\"\r\n               fk = fk & \"`\" & db.Relations(I).Fields(J).Name & \"` ,\"\r\n           Next J\r\n    \r\n           sql = Left(sql, Len(sql) - 1)\r\n           fk = Left(fk, Len(fk) - 1)\r\n           fk = fk & \")\"\r\n           sql = sql & \") REFERENCES `\" & db.Relations(I).Table & \"`\" & fk\r\n           \r\n           If (db.Relations(I).Attributes And RelationAttributeEnum.dbRelationUpdateCascade) Then\r\n               sql = sql & \" ON UPDATE CASCADE\"\r\n           End If\r\n           \r\n           If (db.Relations(I).Attributes And RelationAttributeEnum.dbRelationDeleteCascade) Then\r\n               sql = sql & \" ON DELETE CASCADE\"\r\n           End If\r\n           \r\n           sql = sql & \";\"\r\n    \r\n           Debug.Print sql\r\n        End If\r\n    Next I\r\nEnd Sub<\/pre>\n<p><strong><a href=\"https:\/\/gist.github.com\/ivancp\/9655326\" title=\"Gist Source\" target=\"_blank\">Gist Source<\/a><\/strong><\/p>\n<p>To run the adobe code go to intermediate windows (Ctrl+G) and execute: <code>printRelations<\/code> and  copy the generated SQL code.<\/p>\n<p><a href=\"http:\/\/www.latindevelopers.com\/ivancp\/wp-content\/uploads\/ms-access-foreign-keys.png\"><img loading=\"lazy\" src=\"http:\/\/www.latindevelopers.com\/ivancp\/wp-content\/uploads\/ms-access-foreign-keys-450x273.png\" alt=\"\" title=\"ms-access-foreign-keys\" width=\"450\" height=\"273\" class=\"aligncenter size-medium wp-image-570\" \/><\/a><\/p>\n<p>Enjoy!<\/p>\n<p>(*)MySQL Migration Toolkit is discontinued but is still available from mirrors like: <br \/>\nhttp:\/\/mirrors.dotsrc.org\/mysql\/Downloads\/MySQLGUITools\/<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/ms-access-to-mysql-with-relationships\/\" data-text=\"Converting MS Access to MySQL with relationships\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/ms-access-to-mysql-with-relationships\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I have used tools like MySQL Migration Toolkit (*) and Bullzip&#8217;s Access To MySQL utility, both do a excellent job but without relationships. We can spend a lot of hours to identifying and creating relationships until now: I have write a VBA script to identify MS-Access relationships and create MySQL code with SQL-CREATE sentences, it [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-official sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/ms-access-to-mysql-with-relationships\/\" data-text=\"Converting MS Access to MySQL with relationships\"  >Tweet<\/a><\/li><li class=\"share-facebook\"><div class=\"fb-share-button\" data-href=\"http:\/\/en.latindevelopers.com\/ivancp\/2012\/ms-access-to-mysql-with-relationships\/\" data-layout=\"button_count\"><\/div><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[20,19,5,21],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2f09V-1e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":5,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":101,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/posts\/76\/revisions\/101"}],"wp:attachment":[{"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/en.latindevelopers.com\/ivancp\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}