Solved

ADO addnew slow

Posted on 2004-10-19
7
1,680 Views
Last Modified: 2013-11-23
Hi,
I have a problem here. I'm using ADO in my VB programming. I created a function to add new record into a table("result"). It able to add 500 records into the table but takes 40 seconds to run the process. If compare with DAO method, it can run very fast(5 second). My PC is Pentium III 800Mhz. why ADO run so slow compare to DAO? Please help me? here is my code.
Thank you.

Function Connection()
Connection = "Provider=Microsoft.jet.OLEDB.4.0;Data source=" & App.Path & "\books.mdb;persist security info=false"
End Function

Function RunSearch()
Dim rst As ADODB.Recordset
Dim query As String

query = "select * from IdentMaster Where IdentNo=15000 ORDER BY IdentMaster.IdentNo"
Set rst = New ADODB.Recordset

rst.CursorType = adOpenStatic
rst.Open query, connect_string, , , adCmdText

Dim rst1 As New ADODB.Recordset

With rst1
    .CursorLocation = adUseServer
    .CursorType = adOpenStatic
    .Open "Result", connect_string, adOpenStatic, adLockOptimistic, adCmdTableDirect
    .Index = "PrimaryKey"
End With
If rst.RecordCount = 0 Then
    MsgBox "Record not found!", vbInformation
    Me.sfToolList.Refresh
Else

'delete all records in Result table.
Dim oRs As New ADODB.Recordset
Dim adoConn As ADODB.Connection
Set adoConn = New ADODB.Connection
adoConn.ConnectionString = Connection()
adoConn.Open
oRs.CursorLocation = adUseClient
oRs.Open "Delete * from Result", adoConn, adOpenKeyset, adLockOptimistic

rst1.Requery
    i = 0
        rst.MoveLast
        intCount = rst.RecordCount
        rst.MoveFirst
        For i = 1 To intCount
            If rst1.EOF Then
                With rst1
                .AddNew
                !IdentNo = rst!IdentNo
                !MachineManufacturerID = rst!MachineManufacturerID
                !MachineTypeID = rst!MachineTypeID
                !groupid = rst!groupid
                .Update
                End With
            End If
            rst1.Requery
        rst.MoveNext
        Next i
End If

Rst.close
Set rst=nothing

End Function
0
Comment
Question by:william43
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 12345872
Try use the Execute method in Connection Object to insert your data, instead of using addNew method, this will make your process faster.

Example:

SQLstr = "Insert into mytable (fld1, fldN) values ('value1', 'valueN') "
conn.execute SQLstr

try do necessary change for your above scripting.

Hope this helps
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 12345877
Find more on Jet SQL command syntax if necessary at:
http://www.devguru.com/Technologies/jetsql/quickref/statements.html
0
 
LVL 4

Expert Comment

by:ChenChen
ID: 12345907

agree with ryancys, really should use "insert" statement
0
 
LVL 14

Expert Comment

by:aelatik
ID: 12347782
Had the same issue last time and in my case the addnew command turned out a lot faster then the insert statement because the recordset remains open.
What you should do is to put the .UPDATE command after the loop instead of updating it after each record.
0
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 12349315
The script is slow because you executing your addNew method in a loop, if the loop is big, then more you're suffering to wait the code to execute finished.

As review your code, seems that you want to select the records from a statement (select IdentMaster) and dump it to another table (Result), so why don't you try using Select Into statement? More easier and i think even faster way to code can be executed!

so, try like:

Function RunSearch()

Dim query As String

Dim conn As ADODB.Connection
Set conn = New ADODN.Connection

query = "select IdentNo, MachineManufacturerID, MachineTypeID, groupid into Result from IdentMaster Where IdentNo=15000 ORDER BY IdentMaster.IdentNo"

conn.ConnectionString = Connection
conn.Open

If rs.state = adStateOpen Then
   conn.execute query
   conn.close
end if

Set conn = nothing

End Function


Is this what you want??
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question