Solved

Getting SQL Query Result to Excel

Posted on 2014-12-15
3
66 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 47

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 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