{"id":306,"date":"2016-11-10T05:44:45","date_gmt":"2016-11-09T20:44:45","guid":{"rendered":"http:\/\/okamurax.com\/?p=306"},"modified":"2020-05-10T16:39:57","modified_gmt":"2020-05-10T07:39:57","slug":"vba-sql%e5%ae%9f%e9%a8%93%e7%94%a8","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=306","title":{"rendered":"VBA SQL\u5b9f\u9a13\u7528\u2460(Access(mdb))"},"content":{"rendered":"<p>SQL\u306e\u6319\u52d5\u3092\u8abf\u3079\u308b\u306e\u306b\u30a8\u30af\u30bb\u30eb\u304b\u3089\u624b\u8efd\u306bSQL\u3092\u767a\u884c<\/p>\n<pre class=\"lang:vb decode:true \">Sub test()\r\n\r\nApplication.CommandBars(\"cell\").Reset\r\n\r\nEnd Sub\r\n\r\nSub auto_open()\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"t\"\r\n.Caption = \"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u30fb\u767b\u9332\"\r\nEnd With\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"tt\"\r\n.Caption = \"SQL\u5b9f\u884c\"\r\nEnd With\r\n\r\nEnd Sub\r\n\r\nSub auto_close()\r\n\r\nOn Error Resume Next\r\n\r\nFor i = 1 To 2\r\n  Application.CommandBars(\"cell\").Controls(\"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u30fb\u767b\u9332\").Delete\r\n  Application.CommandBars(\"cell\").Controls(\"SQL\u5b9f\u884c\").Delete\r\nNext i\r\n\r\nEnd Sub\r\n\r\nSub t()\r\n\r\nWith ActiveSheet\r\n\r\ntbl = InputBox(\"\u30c6\u30fc\u30d6\u30eb\u540d\")\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\ncn.Open \"provider=microsoft.jet.oledb.4.0;data source=\" &amp; ThisWorkbook.Path &amp; \"\\SQL\u5b9f\u9a13.mdb\"\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u524a\u9664\r\n\r\nOn Error Resume Next\r\ncn.Execute \"drop table \" &amp; tbl\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\r\n\r\nOn Error GoTo 0\r\nfld = \"\"\r\n\r\nFor c = Selection(1).Column To Selection(Selection.Count).Column\r\n\r\n  Select Case Right(.Cells(Selection(1).Row, c).Value, 1)\r\n    Case \"s\"\r\n    tmp = \"varchar\" 'char\u306f\u77ed\u3044\u30c6\u30ad\u30b9\u30c8(varchar\u306f\u53ef\u5909\u9577), text\u306f\u9577\u3044\u30c6\u30ad\u30b9\u30c8\r\n    Case \"i\"\r\n    tmp = \"integer\"\r\n  End Select\r\n  \r\n  fld = fld &amp; .Cells(Selection(1).Row, c).Value &amp; \" \" &amp; tmp &amp; \",\"\r\n\r\nNext c\r\n\r\nfld = Left(fld, Len(fld) - 1)\r\nq = \"create table \" &amp; tbl &amp; \" (id integer identity(1,1) primary key,\" &amp; fld &amp; \")\": Debug.Print q\r\ncn.Execute q\r\n\r\n'\u30c7\u30fc\u30bf\u767b\u9332\r\n\r\nfld = \"\"\r\n\r\nFor c = Selection(1).Column To Selection(Selection.Count).Column\r\n\r\n  fld = fld &amp; .Cells(Selection(1).Row, c).Value &amp; \",\"\r\n\r\nNext c\r\n\r\nfld = Left(fld, Len(fld) - 1)\r\n\r\nFor r = (Selection(1).Row) + 1 To Selection(Selection.Count).Row\r\n\r\n  For cc = Selection(1).Column To Selection(Selection.Count).Column\r\n  \r\n    rec = rec &amp; \"'\" &amp; .Cells(r, cc).Value &amp; \"'\" &amp; \",\"\r\n  \r\n  Next cc\r\n\r\n  rec = Left(rec, Len(rec) - 1)\r\n  \r\n  q = \"insert into \" &amp; tbl &amp; \" (\" &amp; fld &amp; \") values (\" &amp; rec &amp; \")\": Debug.Print q\r\n  cn.Execute q\r\n\r\n  rec = \"\"\r\n  \r\nNext r\r\n\r\ncn.Close\r\nSet cn = Nothing\r\n\r\nMsgBox \"done\"\r\n\r\nEnd With\r\n\r\nEnd Sub\r\n\r\nSub tt()\r\n\r\nWith ActiveSheet\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\nSet rn = CreateObject(\"adodb.recordset\")\r\n\r\ncn.Open \"provider=microsoft.jet.oledb.4.0;data source=\" &amp; ThisWorkbook.Path &amp; \"\\SQL\u5b9f\u9a13.mdb\"\r\nrn.Open ActiveCell.Value, cn\r\n\r\n'\u30d5\u30a3\u30fc\u30eb\u30c9\u4f5c\u6210\r\n\r\ni = 0\r\nFor c = Selection(1).Column To (Selection(1).Column + rn.Fields.Count) - 1\r\n  ActiveCell.Offset(1, i).Value = rn.Fields(i).Name\r\n  i = i + 1\r\nNext c\r\n\r\n'\u30c7\u30fc\u30bf\u8aad\u8fbc\r\n\r\nActiveCell.Offset(2, 0).CopyFromRecordset rn\r\n\r\nrn.Close\r\nSet rn = Nothing\r\n\r\ncn.Close\r\nSet cn = Nothing\r\n\r\nEnd With\r\n\r\nEnd Sub\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SQL\u306e\u6319\u52d5\u3092\u8abf\u3079\u308b\u306e\u306b\u30a8\u30af\u30bb\u30eb\u304b\u3089\u624b\u8efd\u306bSQL\u3092\u767a\u884c Sub test() Application.CommandBars(&#8220;cell&#8221;).Reset End Sub Sub auto_open() With Appl &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=306\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;VBA SQL\u5b9f\u9a13\u7528\u2460(Access(mdb))&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-1","tag-vba"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":7,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":5110,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/5110"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}