Hi to all,
Iam using vb6.0,excel 2003 and access , by vb6.0 iam retrieve the data from excel and store it in the msaccess 2003.
the problem is i need to upload about 20000 or above it take about 10 min and more.User asking me to reduce the time .
can any one help me in this...,
here is my code......................................
Private Sub cmdExcelupload_Click()
On Error GoTo ERRORHANDLER
Dim xlApp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim path
Dim intI As Integer
cdFiles.Filter = "Only Excel files (*.xls)|*.xls"
cdFiles.ShowOpen
path = cdFiles.FileName
If Len(cdFiles.FileName) = 0 Then
cmdExcelupload.Enabled = False
optExcel.Value = False
Exit Sub
End If
Set xlApp = New Excel.Application
Set xlbook = xlApp.Workbooks.Open(path)
Set xlsheet = xlApp.Worksheets("sheet1")
'Processing label box
lblProcessing.Visible = True
ssDebitcardInformation.Enabled = False
intI = 2
Do Until xlsheet.Cells(intI, 1) = ""
strSql = "Insert into InputExcelData values ("
strSql = strSql & "'" & intI - 1 & "' ,"
strSql = strSql & "'" & xlsheet.Cells(intI, 1) & "' ,"
strSql = strSql & "'" & xlsheet.Cells(intI, 2) & "' ,"
strSql = strSql & "#" & xlsheet.Cells(intI, 3) & "# ,"
strSql = strSql & "'" & xlsheet.Cells(intI, 4) & "' ,"
strSql = strSql & "'" & xlsheet.Cells(intI, 5) & "' ,"
strSql = strSql & "#" & xlsheet.Cells(intI, 6) & "# ,"
strSql = strSql & "'" & xlsheet.Cells(intI, 7) & "' ,"
Set rs = objCon.Execute(strSql)
intI = intI + 1
Debug.Print intI
Loop
xlbook.Close
Set xlApp = Nothing
ERRORHANDLER:
If Err.Number <> 0 Then
If Err.Number = 9 Then
MsgBox "", vbCritical
Else
Call err1
End If
'
End If
End Sub
3 1868
Hi. The speed losses could be to do with the loop itself, the references to the worksheet cells within the loop, and the repeated running of the INSERT INTO statement. Hard to tell, because otherwise you have minimum interaction with the Excel sheet, and you are not killing off the speed by using unnecessary sheet or cell selections (which would dramatically affect performance).
Some suggestions:
* If the Excel sheet does not have multiple datasets separated by blank rows you could replace the DO loop with a FOR loop, as follows: - Dim LastRow as Long
-
...
-
LastRow = xlsheet.Range("A1").SpecialCells(xlLastCell).Row
-
FOR intI = 2 to LastRow
-
...
-
Next intI
FOR loops can be proven to run nearly twice as fast as DO loops, so this may help. Won't work if you have non-contiguous blocks of data - which I was thinking you might have from your DO Until checking for a blank cell.
Next suggestion:
* use a FOR loop to read the seven cells from your worksheet into an array in memory instead of doing so within the build string of the SQL statement. - Const NDataCells = 7
-
DIM ColumnRef as Integer
-
DIM Celldata(NDataCells) as Variant
-
...
-
FOR ColumnRef = 1 to NDataCells
-
Celldata(ColumnRef) = xlsheet.cells(intI, ColumnRef)
-
NEXT ColumnRef
All row interactions with Excel are then done in one small loop without any overhead introduced by the string building. This might be a small effect overall, but even so it may be worth trying. You can then use the Celldata array in memory as the source for your SQL build string.
Final suggestion:
* instead of building a SQL Insert string which you run as many times as there are rows you could add the values directly within the loop by opening the recordset first (using an ADO recordset if you are using VB outside of Access), then using the .addnew method of the recordset within the loop. For each row you then set the field values directly instead of building the SQL string element, and use .update to store those values. You close the recordset again when the loop closes. If you look for Help on the Addnew method of an ADO recordset you will find a VB exemplar which shows how it is done.
I have a feeling that this approach could yield a substantial speed improvement over repeated use of INSERT INTO, as you are opening and closing the recordset just once, whereas the database engine, highly efficient though it is, has to open and close the recordset repeatedly when handling the INSERTs over and over again.
Hope one or more of these suggestions helps.
-Stewart
You can make all assignment statements as a single statement for strSql.
1. Get and filter files in first command button. - Dim xlApp As Excel.Application
-
Dim xlbook As Excel.Workbook
-
Dim xlsheet As Excel.Worksheet
-
Dim path
-
Dim intI As Integer
-
cdFiles.Filter = "Only Excel files (*.xls)|*.xls"
-
cdFiles.ShowOpen
-
path = cdFiles.FileName
-
If Len(cdFiles.FileName) = 0 Then
-
cmdExcelupload.Enabled = False
-
optExcel.Value = False
-
Exit Sub
-
End If
2. Then validate and create work book object there. -
Set xlApp = New Excel.Application
-
Set xlbook = xlApp.Workbooks.Open(path)
-
Set xlsheet = xlApp.Worksheets("sheet1")
3. Get conformation to collect data from excel in another Command button. - lblProcessing.Visible = True
-
ssDebitcardInformation.Enabled = False
-
intI = 2
Here use for loop instead Do loop - Do Until xlsheet.Cells(intI, 1) = ""
-
‘Do All this as a single assignment statement. -
-
strSql = "Insert into InputExcelData values ("
-
strSql = strSql & "'" & intI - 1 & "' ,"
-
strSql = strSql & "'" & xlsheet.Cells(intI, 1) & "' ,"
-
strSql = strSql & "'" & xlsheet.Cells(intI, 2) & "' ,"
-
strSql = strSql & "#" & xlsheet.Cells(intI, 3) & "# ,"
-
strSql = strSql & "'" & xlsheet.Cells(intI, 4) & "' ,"
-
strSql = strSql & "'" & xlsheet.Cells(intI, 5) & "' ,"
-
strSql = strSql & "#" & xlsheet.Cells(intI, 6) & "# ,"
-
strSql = strSql & "'" & xlsheet.Cells(intI, 7) & "' ,"
-
Set rs = objCon.Execute(strSql)
-
intI = intI + 1
-
Debug.Print intI
‘Use the keyword DoEvents to alow the user to do some other operation instead hang
then complite your (for) loop -
Loop
-
xlbook.Close
-
Set xlApp = Nothing
-
ERRORHANDLER:
-
If Err.Number <> 0 Then
-
If Err.Number = 9 Then
-
MsgBox "", vbCritical
-
Else
-
Call err1
-
End If
-
'
-
End If
hi
actually it takes too long because reading data from excel takes too much time each time it's done
The trick is to read the data only once. To do this, have in mind that when you store a Range into a Variant, the Variant wont become a Variant/Range, but a 2 dimensioned Array with the Range's values!!
^.^
So, try something like this (it'll for sure make your code run in less than 1 minute): - dim a as variant
-
with xlApp.worksheets("sheet1")
-
a = .range(.cells(2,1),.cells(2,1).end(-4121))
-
end with
-
for intI = 1 to ubound(a)
-
strSql = "Insert into InputExcelData values ("
-
strSql = strSql & "'" & intI & "' ,"
-
strSql = strSql & "'" & a(intI, 1) & "' ,"
-
strSql = strSql & "'" & a(intI, 2) & "' ,"
-
strSql = strSql & "#" & a(intI, 3) & "# ,"
-
strSql = strSql & "'" & a(intI, 4) & "' ,"
-
strSql = strSql & "'" & a(intI, 5) & "' ,"
-
strSql = strSql & "#" & a(intI, 6) & "# ,"
-
strSql = strSql & "'" & a(intI, 7) & "' ,"
-
Set rs = objCon.Execute(strSql)
-
Debug.Print intI
-
next
HTH
Note: I used the Excel's VBA method 'END' with the parameter xlDown... but since there're no xl constants in VB6, i used its numeric value (-4121)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Hans Georg Krauthaeuser |
last post by:
Hi All,
I was playing with scipy.stats.itemfreq when I observed the following
overflow:
In :for i in :
.....: l=*i
.....: print i, stats.itemfreq(l), l.count(0)
.....:
254 ] 254
|
by: Simon |
last post by:
Dear reader,
I have an Access application which works as back-end and front-end.
In case it's running on a local PC it works perfect.
If I install it on a server the response time is...
|
by: nirav009 |
last post by:
Hi,
i would like to know, how we can reduce the ImageCopy Job run time. The job is usually taking almost 2hours for approximate 6M records. Ofcourse it is run through Utility, but is there any...
|
by: Manogna |
last post by:
hi! all,
in a directory nearly 10 zipped file are available.
totally the size of the all files is nearly 15GB.
i have to retrive the line which dont have the text "ORA" from each file...
|
by: sbettadpur |
last post by:
hello friends,
I need some solution for reducing the query execution time.
Let me explain briefly:
I have 5000 records in mysql database. actually i am using browse buttons to see the...
|
by: cnb |
last post by:
This must be because of implementation right? Shouldn't reduce be
faster since it iterates once over the list?
doesnt sum first construct the list then sum it?
-----------------------
reduce...
|
by: Slaunger |
last post by:
I know there must be a simple method to do this.
I have implemented this function for calculating a checksum based on a
ones complement addition:
def complement_ones_checksum(ints):
"""...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |