Issue Exporting Table From SQL to Excel

justinua

New Member
Joined
Mar 27, 2015
Messages
35
I've been working on a macro that exports data from a SQL table and imports it into an Excel worksheet. When I run the macro, it only brings in a portion of the data, but not all of it. Here is the code I've been using:

Code:
Sub CustCountData()    Dim Server As String
    Dim SQLString As String
    Dim MarkID As String
    Dim FCDate As String
    Dim EXDate As String
    Dim NMDate As String
    Dim PrevFCDate As String
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    
    Set ws = Sheets("Count Summary")
    Set ws2 = Sheets("Count DATA")
    Set ws3 = Sheets("Start")
    
    ws3.Range("CurrentTag").Formula = "='Count Summary'!B6"
    ws3.Range("Previousag").Formula = "='Count Summary'!D6"
    ws3.Range("CurrentTag").Value = ws3.Range("CurrentTag").Value
    ws3.Range("PreviousTag").Value = ws3.Range("PreviousTag").Value
    
    'Populate report parameters
    Server = Range("Server")
    SQLString = Range("CountQueryString3") ' Insert the name of the range/cell where the query is stored here
    MarkID = ws3.Range("MarkID")
    EXDate = Range("EXDate")
    NMDate = Range("NMDate")
    ForecastDate = Range("FCDate")
    PrevMonthForecastDate = Range("PrevFCDate")
        
    SQLString = Replace(SQLString, "@MarkID", MarketID)
    SQLString = Replace(SQLString, "@FCDate", FCDate)
    SQLString = Replace(SQLString, "@EXDate", EXDate)
    SQLString = Replace(SQLString, "@NMDate", NMDate)
    SQLString = Replace(SQLString, "@PrevFCDate", PrevFCDate)
    
With Sheets("Count DATA")
 With Sheets("Count DATA").ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=_____;UID=____;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=____;DATABASE=____" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandText = SQLString
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_CountData"
        .Refresh BackgroundQuery:=False
  End With
  End With


ws2.Columns("B:H").NumberFormat = "General"
ws2.Columns("A:A").NumberFormat = "m/d/yyyy"


'Changes any blank cells to
For Each cell In ws2.Range("Table_CountData")
If IsEmpty(cell) Then
cell.Value = 0
End If
Next


Here is the SQL query it is pulling:

Code:
[TABLE="width: 723"]
<tbody>[TR]
[TD]select ecd.fcast_date, ecd.type, ecd.class_code, sum(ecd.count/ecd.at_factor) as count, sum(ecd.count) as atcount, sum(ecd.con) as con, 'EX' as Type, 'Current' as TAG
from existing_con ecd
where ecd.mark_id = '@MarkID'
 and ecd.fc_date >= '@FCDate'
 group by fc_date, type, class_code
 Union All
 select ecda.fc_date, ecda.type, ecda.class_code, sum(ecda.count/ecda.at_factor) as count, sum(ecda.count) as atcount, sum(ecda.con) as con, 'EX' as Type, 'Previous' as TAG
from ca_con_archive ecda
where ecda.mark_id = '@MarkID'
  and ecda.archive_date = '@EXDate'
  and ecda.fc_date >= '@PrevFCDate'
  group by fc_date, type, class_code
order by tag, fc_date, type, class_code[/TD]
[/TR]
</tbody>[/TABLE]


When the data gets to my worksheet, it is only pulling the data from the table that gets the "Current" tab, but nothing from the table that gets the "Previous" tag. Essentially, anything after the "UNION ALL" is not pulling into Excel (although it runs correctly in SQL.) In addition, when the macro gets to: ".Refresh BackgroundQuery:=False" I get the error message "General ODBC Error". I have to change it from False to True to get the table to run. Lastly, I've tried pulling the data into a temp table 1st, but that just doesn't pull any data to my worksheet.

Does anyone know what I am doing wrong? How can i get it to pull in the data from both the "Current" and "Previous" tables?

Thanks for your help.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,221,780
Messages
6,161,888
Members
451,730
Latest member
BudgetGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top