How to create a download program
Hi All
I created a visual basic report using report tool. when i tried to download this report to MSexcel, it takes times when the report is too large.
Below is the code that i am using for transfer
Private Sub exporttoexcel()
Dim strTbl As String
Dim strFld As String
Dim NoRows As Double
Dim NoCols As Double
Dim i As Integer
Dim TbName As String
Dim j As Integer
On Error GoTo ExpErrCheck
Screen.MousePointer = vbHourglass
Working (False) 'Disabling Buttons
ExNoRows = 0
Set xla = CreateObject("Excel.Application") 'look for a running copy of Excel
Set xlb = xla.Workbooks.Add
Set xls = xlb.Worksheets.Add
xls.Activate
DoEvents
ExNoRows = 0
For j = 0 To Mf.rows - 1
ExNoRows = ExNoRows + 1
Dim dt As String
dt = " "
For i = 0 To Mf.Cols - 1
If IsDate(AvNullS(Mf.TextMatrix(j, i))) Then 'Date checking
If Format(AvNullS(Mf.TextMatrix(j, i)), "dd-mm-yyyy") <> "30-12-1899" Then 'add for zero value checking
xls.Cells(ExNoRows, i + 1) = Trim(AvNullS(Mf.TextMatrix(j, i)))
Else
xls.Cells(ExNoRows, i + 1) = Trim(AvNullS(Mf.TextMatrix(j, i)))
End If
Else
xls.Cells(ExNoRows, i + 1) = Trim(AvNullS(Mf.TextMatrix(j, i)))
End If
Next i
Next j
xla.Visible = True
Working (True)
Screen.MousePointer = vbNormal
Exit Sub
ExpErrCheck:
If Err.Number <> 1004 Then
MsgBox Err.Description
Working (True)
Screen.MousePointer = vbNormal
Exit Sub
Else
Resume Next
End If
End Sub
Is there any other way to transfer vb report to excel fastly.
Takes this as an urgent. Waiting for ur reply.........
Bijeesh
[2230 byte] By [
bijeesh] at [2007-11-19 17:02:19]

# 1 Re: How to create a download program
Could you output the data to a text file, with a comma delimiter, and save it as a .csv? Excel reads CSV files, and writing to text is fairly quick in VB.
Once the CSV is written, Shell it, and Excel will open it.
For example:
1,2,3,4
5,6,7,8
9,10,11,12
Saving that as a CSV and opening that in Excel will show:
| A | B | C | D |
------
1 | 1 2 3 4
2 | 5 6 7 8
3 | 9 10 11 12Except much better, because Excel has a GUI and isn't all Text.
Could you manage that?
# 2 Re: How to create a download program
Here is the code I use to copy data from a flexgrid. After copying the code, all you need to do is paste into excel. It is much much faster than trying to write into excel. The key is to put a tab character between each data value.
Private Sub mnuCopy_Click()
Dim strData As String
Dim nRowIndex As Integer
Dim nColIndex As Integer
strData = ""
Clipboard.Clear
For nRowIndex = 0 To grdDisplay.Rows - 1
For nColIndex = 0 To grdDisplay.Cols - 1
strData = strData & grdDisplay.TextMatrix(nRowIndex, nColIndex) & vbTab ' Delimit with tabs
Next nColIndex
strData = Mid(strData, 1, Len(strData) - 1) ' Delete last tab char
strData = strData & vbCrLf ' Delimit new rows with new line
Next nRowIndex
Clipboard.SetText strData ' Copy it to clipboard
End Sub
# 3 Re: How to create a download program
I am not so sure what is your data source. But if data is coming from a recordset, the fastest way to transfer bulk data is to use the <Excel object>.CopyFromRecordset method (Make sure your checkings/reformatting of fields has already executed here, otherwise you should create a macro as shown below).
<Worksheet>.<Range>(<Cell1>, [Cell2]).CopyFromRecordset <Recordset>
As for the checkings and formatting of fields, it will be faster if you would embed your code into the Excel with the help of macro.
'Create macro
Set oMod = <Workbook>.VBProject.VBComponents.Add(vbext_ct_StdModule)
oMod.CodeModule.AddFromString <Your VBA code (a Sub Procedure)>
'Execute
xlsApp.Run <Sub Procedure Name/Macro Name>
'Remove macro
xlsWbk.VBProject.VBComponents.Remove oMod
Set oMod = Nothing