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
UseQueryTable = UseWorksheet.ListObjects.Add(0, strConnection, True, XlYesNoGuess.xlYes, UseWorksheet.Range("A1")).QueryTable
.CommandType = XlCmdType.xlCmdSql
.CommandText = MyUniversalClass.ReplaceFields_inRcd(strCommandTemplate, MyRcd)
.BackgroundQuery = True
UseConnection = UseWorkbook.Connections("Connection")
Dim MyConn As WorkbookConnection = FindConnection(UseWorksheet.Parent, strConnName)
If (Not MyConn Is Nothing) Then
.Name = strConnName
.OLEDBConnection.SaveAsODC("\\dtchyb-casf025\c_wbgint_users\u231237\My Documents\My Data Sources\" & strConnName & ".odc")
Select Case Err.Number
Call MyUniversalClass.PrintError(Err, "AddListObject_FromConnection")