Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2010, 12:09 AM
Registered User
 
Join Date: May 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Import Data from SQL server to excel

Hi,

I have recorded below macro which works fine to extract data from SQL server but If I have more then 65536 rows of data in my sql table for the criteria which i entered in the below macro but still the macro exports only 65536 rows of data and then stops. ideally it should populate rest of the data in a new worksheet.

Can we modify the below code in such a way that it imports the rest of the data to new worksheet if the number of records exceed 65536.

Code:
Sub Extractdata()

'
    With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DRIVER=SQL Native Client;SERVER=XXXXXX;UID=admin;PWD=****;APP=Microsoft Office XP;WSID=XXXXXXX" _
        ), Array(";DATABASE=meta_data;")), Destination:=Range("A1"))
        .CommandText = Array( _
        "SELECT mydata.CAC, mydata.Year, mydata.""Cost Element"", mydata.""Cost Element Name"", mydata.Name, mydata.""Cost Center"", mydata.""Company Code"", mydata.""Unique Indentifier 1"", ""Cost Center mapping"".""Produ" _
        , _
        "ct UBR Code"", ""Cost Element Mapping"".FSI_LINE2_code" & Chr(13) & "" & Chr(10) & "FROM sap_data.dbo.""Cost Center mapping"" ""Cost Center mapping"", sap_data.dbo.""Cost Element Mapping"" ""Cost Element Mapping"", sap_data.dbo.mydata myda" _
        , _
        "ta" & Chr(13) & "" & Chr(10) & "WHERE mydata.""Unique Indentifier 1"" = ""Cost Element Mapping"".CE_SR_NO AND mydata.""Cost Center"" = ""Cost Center mapping"".""Cost Center"" AND ((""Cost Center mapping"".""Product UBR Code""='G_0768') AND (""" _
        , "Cost Element Mapping"".FSI_LINE2_code='F1547000000'))")
        .Name = "Query from mydatanew"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
Thanks for your help in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Import data from sql server using biztalk server kaushalparik Biztalk 0 July 22nd, 2008 06:16 AM
how to import excel data into sql server khaink ASP.NET 1.0 and 1.1 Basics 1 July 31st, 2007 09:10 PM
SQL Server 2000 DTS Excel Import Error Tglover SQL Server DTS 0 June 22nd, 2007 12:27 PM
Import Data In MS SQL SERVER 2000 The Beginner Classic ASP Basics 1 August 14th, 2006 04:59 PM
how to import data from excel,csv into sql server therisingsun ASP.NET 1.0 and 1.1 Basics 2 October 1st, 2005 12:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.