Solved

Getting SQL Query Result to Excel

Posted on 2014-12-15
3
56 Views
Last Modified: 2015-01-13
Hello All,

I have created an excel macro which connects to a DB server, gets data from sheet1 and uses it to parameterize the query.
The problem is to paste the data to excel. here is the code.

Sub sql_query()
Application.ScreenUpdating = False

 Dim Code_Number As String
 Dim Color As String
 Dim usedRowCount
 Dim conn1 As New Connection
 Dim rec As New Recordset
 Dim comm As New Command
 Dim Str
 
 Set ws = ThisWorkbook.Worksheets("CDF Style Color")
 
conn1.Open "Provider=MSDASQL;DRIVER=SQL SERVER;SERVER=192.168.1.1;111;DNS=DB;UID=SA;PWD=sa;DATABASE=db"
Set comm.ActiveConnection = conn1
 

 
LR = Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 2 To 50 'LR
    
ThisWorkbook.Sheets("CDF Style Color").Activate
        codenum = Cells(i, 1)
        clr = Cells(i, 2)
        Str = "Select code, color, upc, upc2, upc3, upc4, upc5, upc6, upc7, upc8, upc9, upc10, upc11, upc12, upc_prepack from ivtf where code ='" & codenum & "' and Color = '" & clr & "'" '& Chr(34)
       comm.CommandText = Str
        rec.Open comm
        Sheets("Style&Color_Match").Select
        Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).CopyFromRecordset rec
        rec.Close
        
 Next
 
 Rows(1).Delete

         Columns("A:AZ").Select
        Selection.Sort key1:=[C1], order1:=xlAscending, key2:=[A1], order2:=xlAscending, Header:=xlNo, MatchCase:=False, Orientation:=xlTopToBottom
       Application.DisplayAlerts = False
        Columns("A:A").Select
 
Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:gvilbis
3 Comments
 
LVL 1

Accepted Solution

by:
psdesignadmin earned 500 total points
ID: 40501260
You could use the inbuilt CopyFromRecordSet method once you open the recordsheet.

SheetName.RangeAddress.CopyFromRecordset rst 

Open in new window


A detailed commentary is here.

~Tim
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40546220
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now