{"id":1503,"date":"2015-07-22T22:17:02","date_gmt":"2015-07-22T20:17:02","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=1503"},"modified":"2015-07-22T22:17:26","modified_gmt":"2015-07-22T20:17:26","slug":"excel-vba-create-dynamic-worksheet-from-sql-query-excel-arbeitsblatt-durch-sql-query-erstellen","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=1503","title":{"rendered":"Excel VBA: Create dynamic worksheet from SQL Query \/ Excel Arbeitsblatt durch SQL Query erstellen"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>A new Worksheet should be created, that contains the result of an SQL Query (ADODB) generic (in the way, that no column names have to be adressed in the recordset.<\/p>\n<h1>Approach &#8211; Ansatz<\/h1>\n<ul>\n<li>Iteration over the Resultsets field names to get Meta Information \/ Header names<\/li>\n<li>Usage of CopyFromRecordSet method of the Range Object: worksheet.Range(&#8222;A2&#8220;).CopyFromRecordset Rs<\/li>\n<\/ul>\n<h1>Solution &#8211; L\u00f6sung<\/h1>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nSub CreateExcelSheetWithQueryResult(ws As Worksheet, sql As String)\r\n    Dim Cn As New ADODB.Connection\r\n    Dim Rs As New ADODB.Recordset\r\n    Dim vaTmp() As String\r\n    \r\n    Cn.Provider = &quot;SQLOLEDB.1&quot;\r\n    \r\n    Cn.ConnectionString = &quot;Password=pass;&quot; &amp; _\r\n    &quot;Persist Security Info=True;&quot; &amp; _\r\n    &quot;User ID=user;&quot; &amp; _\r\n    &quot;Initial Catalog=databaseName;&quot; &amp; _\r\n    &quot;Data Source=ServernameOrIP&quot;\r\n    \r\n    Cn.Open\r\n    \r\n    Rs.CursorType = adOpenKeyset\r\n    Rs.LockType = adLockPessimistic\r\n    Rs.Open sql, Cn, adOpenStatic\r\n   \r\n    ws.Cells.Clear\r\n    \r\n    &#039; This section fills in the field names from the Orders table.\r\n    ReDim vaTmp(Rs.Fields.Count)\r\n    For x = 0 To Rs.Fields.Count - 1\r\n        vaTmp(x) = Rs.Fields(x).Name\r\n    Next\r\n    ws.Cells(1, 1).Resize(1, Rs.Fields.Count) = vaTmp\r\n   \r\n    ws.Range(&quot;A2&quot;).CopyFromRecordset Rs\r\n    \r\n    Rs.Close\r\n    Cn.Close\r\nEnd Sub\r\n<\/pre>\n<p>And if you want to create a new Excel sheet you can do this with a button that assigned macro contains the following source:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nSub DetailQuery1_KlickenSieAuf()\r\n    Dim ws As Worksheet\r\n    Sheets.Add After:=Sheets(Sheets.Count)\r\n    &#039; Referenz darauf\r\n    Set ws = Sheets(Sheets.Count)\r\n    CreateExcelSheetWithQueryResult ws, &quot;SELECT TOP 10 * FROM irrsinn&quot;\r\n    \r\nEnd Sub\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D1503&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem A new Worksheet should be created, that contains the result of an SQL Query (ADODB) generic (in the way, that no column names have to be adressed in the recordset. Approach &#8211; Ansatz Iteration over the Resultsets field names to get Meta Information \/ Header names Usage of CopyFromRecordSet method of the Range Object: &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=1503\" class=\"more-link\"><span class=\"screen-reader-text\">Excel VBA: Create dynamic worksheet from SQL Query \/ Excel Arbeitsblatt durch SQL Query erstellen<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","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,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11,12],"tags":[],"class_list":["post-1503","post","type-post","status-publish","format-standard","hentry","category-ms-sql-server","category-vba"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-of","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1503"}],"version-history":[{"count":1,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1503\/revisions"}],"predecessor-version":[{"id":1504,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1503\/revisions\/1504"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}