Solved

Getting SQL Query Result to Excel

Posted on 2014-12-15
3
61 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 46

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

777 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