Solved

Waiting to populate Excel worksheets using data connection

Posted on 2013-01-30
2
239 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

743 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

16 Experts available now in Live!

Get 1:1 Help Now