473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inefficient Loop

6
Hi,

I'm trying to simplify and make this piece of code more efficient (basically would like to remove the 2nd loop); any suggestions?

Thanks
Yann


Expand|Select|Wrap|Line Numbers
  1.     Dim db As Database, rst1 As Recordset, rst2 As Recordset, qry1 As String, qry2 As String
  2.     Dim intCurrentProgress As Integer, intTotalWidth As Integer, intProgressBarMax As Integer
  3.  
  4.     q = MsgBox("Are you sure you want to update all ranks?", vbExclamation + vbOKCancel)
  5.     If q = vbCancel Then Exit Function
  6.  
  7.     qry1 = "SELECT DISTINCT tblStudents.StudentID, tblStudents.RankID FROM tblStudents INNER JOIN tblGradingResults ON tblStudents.StudentID = tblGradingResults.StudentID"
  8.  
  9.  
  10.  
  11.     Set db = CurrentDb()
  12.     Set rst1 = db.OpenRecordset("qryLastGradingPerStudent")
  13.     intProgressBarMax = rst1.RecordCount
  14.  
  15.     SysCmd acSysCmdInitMeter, "Updating: ", intProgressBarMax
  16.     If Not rst1.EOF Then
  17.         rst1.MoveFirst
  18.         Do Until rst1.EOF
  19.  
  20.         SysCmd acSysCmdUpdateMeter, intCurrentProgress
  21.  
  22.         qry2 = "SELECT tblStudents.StudentID, tblStudents.RankID, tblStudents.LastGrading FROM tblStudents WHERE (((tblStudents.StudentID)=" & rst1!StudentID & "))"
  23.         Set rst2 = db.OpenRecordset(qry2)
  24.             rst2.Edit
  25.                 rst2!RankID = rst1!RankID
  26.                 rst2!LastGrading = rst1!GradingDate
  27.                 rst2.Update
  28.             rst2.Close
  29.                 Set rst2 = Nothing
  30.  
  31.  
  32.             'update Progress Barr
  33.             intCurrentProgress = intCurrentProgress + 1
  34.         rst1.MoveNext
  35.         Loop
  36.     End If
  37.  
  38.     rst1.Close
  39. '    rst2.Close
  40.     Set rst1 = Nothing
  41.     Set rst2 = Nothing
  42.     Set db = Nothing
  43.  
  44.  
Feb 19 '12 #1
6 1767
Rabbit
12,516 Expert Mod 8TB
Is there a reason you're doing it with a recordset instead of using an update query?
Feb 20 '12 #2
Yannt
6
I did, but got an error: "Operation must use and updateable query"; hence why I used the recordset.
Feb 20 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
The reason it is not updatable is because you are using the keyword Distinct in your SQL, which also leads me to wonder if your data is properly normalised since you feel you need the distinct keyword.


I agree with Rabbit that from what you have presented here, it seems more like something for an update query,but we would need more details on your table design to say anything for sure.

I would still like however to answer your question.
In terms of making your code more efficient, you need to move the second recordset opening out of the loop, and then use rst2.findfirst and rst2.findNext combined with rst2.Nomatch (use the help function to read about the 3 functions) inside the loop.
Feb 20 '12 #4
Yannt
6
Thanks for your answer Smiley. Qry1 is no longer required (I forgot to delete it from the code). The table structure is quite complex and can't be changed at this point of time.

I'm not sure to understand how to use the "find first" and "find Next" option, could you please give me more details?

Thanks
Feb 20 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
I still need more info on your table structure to give you a proper answer. At best I would be guessing and probably confusing you.

Imagine however a tbl_Orders (with primary Key: PK_Order) and a tbl_OrderDetails (With Foreign Key FK_Order) in which we would want to loop through the orders shipped today, specifying that each item was shipped succesfully.
Expand|Select|Wrap|Line Numbers
  1. Dim cDB as dao.Database
  2. Dim rsOrder as Dao.RecordSet
  3. Dim rsOrderDetails as Dao.Recordset
  4. set rsOrder=cDB.OpenRecordset("SELECT tbl_Order.PK_Order Where dt_OrderDate=Date()",dbopendynaset)
  5. Set rsOrderDetails =cdb.OpenRecordset("tbl_OrderDetails",dbOpenDynaset)
  6.  
  7. dim strCriteria as string
  8. Do while not rsOrder.EOF
  9.   strCriteria="FK_Order=" & rsOrder!PK_Order
  10.   rsOrderDetails.FindFirst strCriteria
  11.   Do while NOT rsOrderDetails.Nomatch
  12.     rsOrderDetails.Edit
  13.       rsOrderDetails!b_Shipped=True
  14.     rsOrderDetails.Update
  15.  
  16.     'Move to next match
  17.     rsOrderDetails.FindNext strCriteria
  18.   Loop
  19. Loop
  20.  
  21.  
  22. 'Cleanup
  23. set cDb=Nothing
  24. set rsOrder=nothing
  25. set rsOrderDetails=nothing
Feb 20 '12 #6
Yannt
6
FindFirst works like a charm, speed gain is great. Thanks for your help.

Yann
Feb 21 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Aaron | last post by:
Is there a way I can loop through everything that is in session?
5
by: Andrew Young | last post by:
How do I loop thru a result set Without using a curosr?
4
by: bourgon | last post by:
Working on some new code, I'm coming across WHILE loops used instead of cursors. I was curious if anyone had any stats on how the speed of doing this compares to the speed of a cursor. I...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
15
by: Alex Snast | last post by:
Hello I'm new to python and i can't figure out how to write a reverse for loop in python e.g. the python equivalent to the c++ loop for (i = 10; i >= 0; --i)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
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...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.