DG_Montana
New Member
- Joined
- Dec 27, 2011
- Messages
- 19
I've written a routine to populate an excel spreadsheet with a list of geographic regions (cities, counties, congressional districts, etc) that are stored on a server. Each type of geographic region is stored on a separate sheet in the Excel workbook. The lists are then used to populate a form for selection purposes, so performance requires the lists to be stored locally.
Some of the lists (all cities in the US, for example) are very large (35,000+ records) and must be retrieved in 1000-record chunks, again for performance reasons. In Excel 2007/2010, if I disconnect my network connection during the geography refresh for a large data set that is broken into chunks, Excel hangs forever (we've allowed over 30 minutes in our testing) and must be closed and restarted. In Excel 2007/2010 if I disconnect my network connection during the geography refresh for a small data set (such as congressional districts) that isn't broken into chunks, Excel reports the disconnect error and gracefully terminates the process according to the error handling I have in place. Oddly, in Excel 2003, Excel terminates the process gracefully whether I'm refreshing a large, "chunked" data set or a small one. Even more interestingly, if I reconnect after 5 or 10 minutes, Excel 2007/2010 picks right back up where it left off and continues with the fetching of the data.
As you can see from the code sample below, exactly the same procedure is called for both small and large data sets:
The function that is called appears below, edited for anonymity. If someone could suggest a method for forcing Excel 2007/2010 to stop trying to refresh and run my error handling code when the network connection is unavailable, I'd REALLY appreciate it!!! The DoWhile/DoEvents loop was my attempt at forcing that, but it hasn't had any effect. Thanks!!!
Some of the lists (all cities in the US, for example) are very large (35,000+ records) and must be retrieved in 1000-record chunks, again for performance reasons. In Excel 2007/2010, if I disconnect my network connection during the geography refresh for a large data set that is broken into chunks, Excel hangs forever (we've allowed over 30 minutes in our testing) and must be closed and restarted. In Excel 2007/2010 if I disconnect my network connection during the geography refresh for a small data set (such as congressional districts) that isn't broken into chunks, Excel reports the disconnect error and gracefully terminates the process according to the error handling I have in place. Oddly, in Excel 2003, Excel terminates the process gracefully whether I'm refreshing a large, "chunked" data set or a small one. Even more interestingly, if I reconnect after 5 or 10 minutes, Excel 2007/2010 picks right back up where it left off and continues with the fetching of the data.
As you can see from the code sample below, exactly the same procedure is called for both small and large data sets:
Code:
If Iterate = False Then ' this is used for small data sets
' Run the query once
If Not GeogLists.QueryBuild() Then
Err.Raise glHANDLED_ERROR
End If
Else ' this is used for large, "chunked" data sets
' For each 1000 rows in the data set, run the query once
For i = 0 To NumRefresh - 1
DestRow = (i * 1000)
GeogRange = "$A$" & (DestRow + 1)
StartRow = DestRow
' Run the query multiple times
If Not GeogLists.QueryBuild() Then
Err.Raise glHANDLED_ERROR
End If
Next i
End If
Code:
Public Function QueryBuild() As Boolean
Dim URL As String ' the web address of the query results
Dim lAttempt As Long ' the number of connection attempts made
Const sSOURCE As String = "QueryBuild"
Dim bReturn As Boolean
Dim qt As QueryTable
On Error GoTo ErrorHandler
bReturn = True ' Assume the procedure succeeded until an error is encountered
' Update Excel's Status Bar
Application.StatusBar = "Refreshing " & QryName & " " & StartRow
' Build the Query Connection String
URL = "URL;http://www.urltogetdata.aspx?&startrow=" & StartRow & "&query=" & QryName
Set qt = GeogWB.Worksheets(GeogWS).QueryTables.Add(Connection:=URL, _
Destination:=Range(GeogRange))
With qt
.Name = QryName
.RefreshStyle = xlOverwriteCells
On Error GoTo RetryConnection
.Refresh BackgroundQuery:=False
Do While qt.Refreshing
DoEvents
Loop
On Error GoTo ErrorHandler
End With
ErrorExit:
' Update Excel's Status Bar
Application.StatusBar = False
' If there was an error during the building of the connection, remove it
If bReturn = False Then qt.Delete
QueryBuild = bReturn
Exit Function
RetryConnection:
' Attempt to make the connection three times before bailing out.
If lAttempt < 2 Then
Application.StatusBar = "Retrying connection..."
lAttempt = lAttempt + 1
Application.Wait Now + TimeSerial(0, 0, 3) ' wait 3 seconds before retrying connection
Resume
End If
Err.Description = "Nice error message in user-friendly English" & _
Chr(10) & Chr(10) & Err.Description
ErrorHandler:
bReturn = False
If bCentralErrorHandler(msMODULE, sSOURCE) Then
Stop
Resume
Else
Resume ErrorExit
End If
End Function