Solved

Waiting to populate Excel worksheets using data connection

Posted on 2013-01-30
2
271 Views
Last Modified: 2013-04-11
I have a VB.Net program that populates a series of Excel files with queries based on SQL Server.  The automated process generates fifteen files, four worksheets each, and each worksheet is populated with data from a connection to SQL Server.  Each query takes several minutes to complete sending its data to Excel, and each workbook can take about fifteen minutes to complete the upload to all four worksheets.

If I were able to put all of these queries in the worksheets, but without actually executing them, then the population would be extremely fast.  Then, when all the processes in place, I would like to be able, simply to turn all of the queries with a simple "Refresh All".  The reason this has come up is that my VB program sometimes stalls from a timeout error, and I have to rerun the program, starting from the place that stalled.  

Infinitely prefereable would be to have all the pieces in place and then turn on the queries and perform the worksheet population, after the code has done all of its work.  Is this possible?  I hope my question is clearly stated.  You're welcome to ask questions, if necessary, to gain clarity on this issue.

If you find it useful, I've included the code responsible for creating a new connection in a worksheet and importing the contents into that sheet.

Thanks, ~Peter Ferber
    Sub AddListObject_FromConnection(ByVal UseWorksheet As Worksheet, MyRcd As ADODB.Recordset, strConnName As String, ByVal strCommandTemplate As String, _
    ByVal strConnection As String)
        Dim UseConnection As WorkbookConnection
        Dim UseQueryTable As Excel.QueryTable

        Dim UseWorkbook As Workbook = UseWorksheet.Parent
        On Error GoTo Err
        UseWorksheet.Cells.Delete()
        UseQueryTable = UseWorksheet.ListObjects.Add(0, strConnection, True, XlYesNoGuess.xlYes, UseWorksheet.Range("A1")).QueryTable
        With UseQueryTable
            .CommandType = XlCmdType.xlCmdSql
            .CommandText = MyUniversalClass.ReplaceFields_inRcd(strCommandTemplate, MyRcd)
            .BackgroundQuery = True
            .Refresh(BackgroundQuery:=False)
        End With

        UseConnection = UseWorkbook.Connections("Connection")
        Dim MyConn As WorkbookConnection = FindConnection(UseWorksheet.Parent, strConnName)
        If (Not MyConn Is Nothing) Then
            MyConn.Delete()
        End If
        With UseConnection
            .Name = strConnName
            .OLEDBConnection.SaveAsODC("\\dtchyb-casf025\c_wbgint_users\u231237\My Documents\My Data Sources\" & strConnName & ".odc")
        End With

Sub_Exit:
        Exit Sub

Err:
        Select Case Err.Number
            Case Else
                Call MyUniversalClass.PrintError(Err, "AddListObject_FromConnection")
                Resume Sub_Exit
                Resume
        End Select
    End Sub

Open in new window

0
Comment
Question by:PeterFrb
2 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 38838591
If you run the code without the following code:

'UseConnection = UseWorkbook.Connections("Connection")
        'Dim MyConn As WorkbookConnection = FindConnection(UseWorksheet.Parent, strConnName)
        'If (Not MyConn Is Nothing) Then
            'MyConn.Delete()
        'End If
        'With UseConnection
            '.Name = strConnName
            '.OLEDBConnection.SaveAsODC("\\dtchyb-casf025\c_wbgint_users\u231237\My Documents\My Data Sources\" & strConnName & ".odc")
        'End With

Open in new window


(so comment it out with apostrophe as above)
You should be left with linked query tables in Excel which can be refreshed from Excel using the right click refresh or Data>Refresh all option. (or set to refresh on open).

The following code is the main culprit for non-refreshable tables in your excel documents, as it is deleting the connection (before the file is saved):
        If (Not MyConn Is Nothing) Then
            MyConn.Delete()
        End If

Open in new window

0
 

Author Comment

by:PeterFrb
ID: 38849953
You seem to understand my question.  I will be heading to work tomorrow, when I'll be in a position to test your ideas.  Once I've had a chance to do that, I'll contact you again.

Thanks, ~Peter
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

679 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