Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Waiting to populate Excel worksheets using data connection

Posted on 2013-01-30
2
Medium Priority
?
297 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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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.
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.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

824 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