{"id":2616,"date":"2018-12-05T05:42:25","date_gmt":"2018-12-04T20:42:25","guid":{"rendered":"http:\/\/okamurax.com\/?p=2616"},"modified":"2018-12-05T05:42:25","modified_gmt":"2018-12-04T20:42:25","slug":"%e3%83%91%e3%82%b9%e4%bb%98%e3%81%8daccdb%e3%81%ab%e3%82%a8%e3%82%af%e3%82%bb%e3%83%abvba%e3%81%8b%e3%82%89%e3%82%a2%e3%82%af%e3%82%bb%e3%82%b9","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2616","title":{"rendered":"VBA \u30d1\u30b9\u4ed8\u304dAccess(accdb)\u306b\u30a2\u30af\u30bb\u30b9"},"content":{"rendered":"<p>\u3061\u3087\u3063\u3068\u8a66\u3057\u306b\u3002<\/p>\n<pre class=\"lang:vb decode:true \">Sub MainRoutine(TargetName, BaseYear, RoutineType)\r\n\r\nExecuteResult = False\r\n\r\nIf RoutineType = \"COMPARE_YEAR\" Then\r\n\r\nQueryString = \"select * from xxx\"\r\nExecuteResult = DbExecute(QueryString, TargetName, BaseYear, RoutineType)\r\n\r\nEnd If\r\n\r\nCall TmporaryDelete\r\n\r\nIf True = ExecuteResult Then\r\n  MsgBox \"\u5b8c\u4e86\u3057\u307e\u3057\u305f\"\r\nElse\r\n  MsgBox \"\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3044\u307e\u3059\"\r\nEnd If\r\n\r\nEnd Sub\r\n\r\nFunction DbExecute(QueryString, TargetName, BaseYear, RoutineType)\r\n\r\nOn Error GoTo e\r\n\r\nSet cn = CreateObject(\"ADODB.Connection\")\r\nSet rs = CreateObject(\"ADODB.Recordset\")\r\n\r\nDbPath = \"C:\\xxx.accdb\"\r\nConnectionString = \"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\" &amp; DbPath &amp; \"; Jet OLEDB:Database Password=xxx\"\r\n\r\ncn.Open ConnectionString\r\nrs.Open QueryString, cn\r\n\r\nIf RoutineType = \"COMPARE_YEAR\" Then\r\n  If False = CompareYear(rs, TargetName, BaseYear, RoutineType) Then GoTo e\r\nEnd If\r\n\r\nIf rs.State = 1 Then rs.Close\r\nSet rs = Nothing\r\n \r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nDbExecute = True\r\nExit Function\r\n\r\ne:\r\n\r\nIf rs.State = 1 Then rs.Close\r\nSet rs = Nothing\r\n \r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nDbExecute = False\r\n\r\nEnd Function\r\n\r\nFunction CompareYear(ByVal rs, TargetName, BaseYear, RoutineType)\r\n\r\nOn Error GoTo e\r\n\r\nCall TmporaryDelete\r\nSet w = Sheets.Add: w.Name = \"tmp\"\r\n\r\nSheetName = \"\u5e74\u5ea6\u6bd4\u8f03(\" &amp; BaseYear - 1 &amp; \"-\" &amp; BaseYear &amp; \")_\" &amp; TargetName\r\n\r\nIf True = ExistSheet(SheetName) Then\r\n  MsgBox \"\u30b7\u30fc\u30c8\u540d\u304c\u91cd\u8907\u3057\u3066\u3044\u307e\u3059\"\r\n  GoTo e\r\nEnd If\r\n\r\nSheets(\"(\u96db\u5f62)\u5e74\u5ea6\u6bd4\u8f03\").Copy After:=Sheets(Sheets.Count)\r\nActiveSheet.Name = SheetName\r\n\r\n'ActiveSheet.Cells(1, 1).CopyFromRecordset rs\r\n\r\nCompareYear = True\r\nExit Function\r\n\r\ne:\r\n\r\nCompareYear = False\r\n\r\nEnd Function\r\n\r\nFunction ExistSheet(SheetName)\r\n\r\nFor Each w In Sheets\r\n  If w.Name = SheetName Then\r\n    ExistSheet = True\r\n    Exit Function\r\n  End If\r\nNext\r\n\r\nExistSheet = False\r\n\r\nEnd Function\r\n\r\nSub TmporaryDelete()\r\n\r\nApplication.DisplayAlerts = False\r\n\r\nFor Each w In Sheets\r\n  If w.Name = \"tmp\" Then Sheets(\"tmp\").Delete\r\nNext\r\n\r\nApplication.DisplayAlerts = True\r\n\r\nEnd Sub\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3061\u3087\u3063\u3068\u8a66\u3057\u306b\u3002 Sub MainRoutine(TargetName, BaseYear, RoutineType) ExecuteResult = False If RoutineType = &#8220;COMPARE_YE &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2616\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;VBA \u30d1\u30b9\u4ed8\u304dAccess(accdb)\u306b\u30a2\u30af\u30bb\u30b9&#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-2616","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\/2616","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=2616"}],"version-history":[{"count":3,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2616\/revisions"}],"predecessor-version":[{"id":5108,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2616\/revisions\/5108"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}