Solved

Waiting to populate Excel worksheets using data connection

Posted on 2013-01-30
2
277 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
[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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

752 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