Michel,
I thought it would overkill to post the method that creates the CSV file, since it works fine when not run inside the loop. That is, it successfully downloads a single CSV file from the row that calls it via the DoubleClick event. I just have a problem when it is run inside the loop that attempts to download a series of CSV files based on all the rows seen in the displayed WebListBox. In that case, after running through all the rows in the loop, it ends up successfully executing the ShowURL(Self.TextFile.URL) step only during the last iteration.
Anyway, here’s the code in full, with some extra comments added for this post:
[code]// Method name: DownloadAllCSVs
// This method attempts to download all the CSVs for the selected agent.
// It runs the SaveToCSV method that you see after it here, in a loop
// that covers all rows found in the displayed WebListBox (AgentWeeksList1).
// SaveToCSV takes 5 parameters (agentID, agentWebID, year, week and agentName).
// SelectedAgentID, SelectedAgentWebID and SelectedAgentName are properties of the WebPage.
// The year and week strings are found in cells 0 and 1, respectively.
If AgentWeeksList1.RowCount > 0 Then
For row As Integer = 0 To AgentWeeksList1.RowCount - 1
Self.SaveToCSV(_
AdminAgentsPage.SelectedAgentID, _
AdminAgentsPage.SelectedAgentWebID, _
AgentWeeksList1.Cell(row, 0), _
AgentWeeksList1.Cell(row, 1), _
AdminAgentsPage.SelectedAgentName _
)
Next
Else
Return
End If[/code]
[code]// Method name: SaveToCSV
// Parameters passed, all as strings: year, week, agentID, agentWebID, agentName.
// AccountabilityFieldsNames_Week_CSV is a list of field names for the header row.
// Set the CSVFilename filename
Dim CSVFilename As String = _
“WeeklyData-” + year + “-” + week + “-” + _
agentID + “-” + ReplaceAll(agentName, " ", “”) + _
“.csv”
// Setup variables for writing the two rows (header and data).
Dim i As Integer
Dim names() As String = AccountabilityFieldsNames_Week_CSV
Dim line1 As String
Dim line2 As String
Dim s As String
// Write header row.
For i = 0 To Ubound(names)
If i <> Ubound(names) Then
line1 = line1 + names(i) + “,”
Else
line1 = line1 + names(i)
End If
Next
s = line1
// Get data from database file to be used for data row.
Dim sql As String
Dim rs As RecordSet
sql = “SELECT * FROM AgentWeeks” + _
" WHERE AgentWebID = " + agentWebID + _
" AND Year = " + year + _
" AND Week = " + week
rs = Session.DB.SQLSelect(sql)
// ***
// Error logging goes here, which I omitted for this post
// ***
// Write data row.
For i = 0 To Ubound(names)
If i <> Ubound(names) Then
line2 = line2 + Str(rs.Field(names(i)).IntegerValue) + “,”
Else
line2 = line2 + Str(rs.Field(names(i)).IntegerValue)
End If
Next
s = s + EndOfLine + line2
// Prepare the file for download.
Self.TextFile = New WebFile
Self.TextFile.MimeType = “text/plain”
Self.TextFile.ForceDownload = True
Self.TextFile.FileName = CSVFilename
Self.TextFile.Data = s
// Download the file.
ShowURL(Self.TextFile.URL)[/code]