Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why my code take time 10 Seconds

0 views
Skip to first unread message

Nova

unread,
Nov 8, 2007, 11:43:01 PM11/8/07
to
I need to insert data to table intermod and write code in cal button click
event. Unfortunately It takes time more than 20 seconds. I try many times
and belives that "Insert into...." in M2FSql1 take almost of time.
How to write code to reduce time.


Dim M2FSql1 as String

N=35
For M = 0 To N - 1
For X = M + 2 To N
For Q = 1 To 3

F1=M*N+5
F2=M+1-N*2
OrderN = 2 * Q + 1
cal1 = (Q + 1) * F1 - Q * F2
cal2 = (Q + 1) * F2 - Q * F1


M2FSql1 = "INSERT INTO Intermod (OrderN,IntmodFreq, Freq1, Freq2) "
M2FSql1 = M2FSql1 & "Values(" & OrderN & "," & cal1 & "," & F1 & "," & F2 &
" );"
DoCmd.RunSQL M2FSql1
Next Q
Next X
Next M

Marshall Barton

unread,
Nov 9, 2007, 1:42:04 AM11/9/07
to


I think this approach might be faster:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = OpenRecordset(Intermod, dbOpenDynaset)


N=35
For M = 0 To N - 1
For X = M + 2 To N
For Q = 1 To 3

rs.AddNew
F1=M*N+5
F2=M+1-N*2
rs!OrderN = 2 * Q + 1
rs!IntmodFreq= (Q + 1) * F1 - Q * F2


' cal2 = (Q + 1) * F2 - Q * F1

rs!Freq1 = F1
rs!Freq2 = F2
rs!Update


Next Q
Next X
Next M

rs.Close : Set rs = Nothing
Set db = Nothing

--
Marsh
MVP [MS Access]

John Spencer

unread,
Nov 9, 2007, 8:18:04 AM11/9/07
to
Since you are running somethng on the order of 1700-1800 queries in the
nested loops, I think that the performance is fairly good.

Perhaps Marshall's solution will work better for you.

Can I ask how often you need to do this? Might you be better off building
the table once and then if you need a limited set of records use a query to
restrict the set of records returned.

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
.

"Nova" <No...@discussions.microsoft.com> wrote in message
news:E69BB7A7-A051-4ED0...@microsoft.com...

George Nicholson

unread,
Nov 9, 2007, 4:30:52 PM11/9/07
to
In addition to the other suggestions provided, consider:
1) Under Options>Advanced, check "Open databases using record-level locking'
2) remove table indexes before adding that many records and replace them
afterwards. Otherwise, the index(s) will be updated with every single Insert
or AddNew.

--
HTH,
George


"Nova" <No...@discussions.microsoft.com> wrote in message
news:E69BB7A7-A051-4ED0...@microsoft.com...

0 new messages