Link to home
Start Free TrialLog in
Avatar of PeterFrb
PeterFrbFlag for United States of America

asked on

Waiting to populate Excel worksheets using data connection

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

ASKER CERTIFIED SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterFrb

ASKER

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