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 -
Dim db As Database, rst1 As Recordset, rst2 As Recordset, qry1 As String, qry2 As String
-
Dim intCurrentProgress As Integer, intTotalWidth As Integer, intProgressBarMax As Integer
-
-
q = MsgBox("Are you sure you want to update all ranks?", vbExclamation + vbOKCancel)
-
If q = vbCancel Then Exit Function
-
-
qry1 = "SELECT DISTINCT tblStudents.StudentID, tblStudents.RankID FROM tblStudents INNER JOIN tblGradingResults ON tblStudents.StudentID = tblGradingResults.StudentID"
-
-
-
-
Set db = CurrentDb()
-
Set rst1 = db.OpenRecordset("qryLastGradingPerStudent")
-
intProgressBarMax = rst1.RecordCount
-
-
SysCmd acSysCmdInitMeter, "Updating: ", intProgressBarMax
-
If Not rst1.EOF Then
-
rst1.MoveFirst
-
Do Until rst1.EOF
-
-
SysCmd acSysCmdUpdateMeter, intCurrentProgress
-
-
qry2 = "SELECT tblStudents.StudentID, tblStudents.RankID, tblStudents.LastGrading FROM tblStudents WHERE (((tblStudents.StudentID)=" & rst1!StudentID & "))"
-
Set rst2 = db.OpenRecordset(qry2)
-
rst2.Edit
-
rst2!RankID = rst1!RankID
-
rst2!LastGrading = rst1!GradingDate
-
rst2.Update
-
rst2.Close
-
Set rst2 = Nothing
-
-
-
'update Progress Barr
-
intCurrentProgress = intCurrentProgress + 1
-
rst1.MoveNext
-
Loop
-
End If
-
-
rst1.Close
-
' rst2.Close
-
Set rst1 = Nothing
-
Set rst2 = Nothing
-
Set db = Nothing
-
-
6 1767
Is there a reason you're doing it with a recordset instead of using an update query?
I did, but got an error: "Operation must use and updateable query"; hence why I used the recordset.
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.
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
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. - Dim cDB as dao.Database
-
Dim rsOrder as Dao.RecordSet
-
Dim rsOrderDetails as Dao.Recordset
-
set rsOrder=cDB.OpenRecordset("SELECT tbl_Order.PK_Order Where dt_OrderDate=Date()",dbopendynaset)
-
Set rsOrderDetails =cdb.OpenRecordset("tbl_OrderDetails",dbOpenDynaset)
-
-
dim strCriteria as string
-
Do while not rsOrder.EOF
-
strCriteria="FK_Order=" & rsOrder!PK_Order
-
rsOrderDetails.FindFirst strCriteria
-
Do while NOT rsOrderDetails.Nomatch
-
rsOrderDetails.Edit
-
rsOrderDetails!b_Shipped=True
-
rsOrderDetails.Update
-
-
'Move to next match
-
rsOrderDetails.FindNext strCriteria
-
Loop
-
Loop
-
-
-
'Cleanup
-
set cDb=Nothing
-
set rsOrder=nothing
-
set rsOrderDetails=nothing
FindFirst works like a charm, speed gain is great. Thanks for your help.
Yann
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Aaron |
last post by:
Is there a way I can loop through everything that is in session?
|
by: Andrew Young |
last post by:
How do I loop thru a result set Without using a curosr?
|
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...
|
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...
|
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 = ...;
....
}
}
|
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...
|
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...
|
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...
|
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)
{...
|
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)
|
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,...
|
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: 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...
|
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: 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...
| |