473,385 Members | 1,292 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,385 software developers and data experts.

For Each loop object declaration issues.

17
Hi,

I am very new to Access and wondered if anyone could help with a syntax error I have with my VBA.

I have a table, and 3 fields which should update a 4th field, but referencing 3 other tables - each linked to one of the 3 fields. This section seems to work fine, however I want it to do it for every record in the table and currently it only does it for the first one.

I have tried using the For Each statement, but this is apparently invalid for "this type of object".

Any ideas of how I can declare things differently?

Thanks

Zoe

My code is as follows:


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdUpdate_Click()
  2. Dim rcdTesting As DAO.Recordset
  3. Dim rcdReference As DAO.Recordset
  4. Dim rcdReference2 As DAO.Recordset
  5. Dim rcdReference3 As DAO.Recordset
  6. Dim I As Variant
  7. Dim Constant As Integer
  8. Dim Constant2 As Integer
  9. Dim Constant3 As Integer
  10.  
  11.  
  12.    Set rcdTesting = CurrentDb.OpenRecordset("tblTest2")
  13.    Set rcdReference = CurrentDb.OpenRecordset("tblReference")
  14.    Set rcdReference2 = CurrentDb.OpenRecordset("tblReference2")
  15.    Set rcdReference3 = CurrentDb.OpenRecordset("tblReference3")
  16.  
  17.  With rcdTesting
  18.  For Each I In rcdTesting
  19.     .Edit
  20.    If ![Outing] = rcdReference![Value] Then
  21.    Constant = rcdReference![Result]
  22.    End If
  23.    If ![Lap] = rcdReference2![Value] Then
  24.    Constant2 = rcdReference2![Result]
  25.    End If
  26.    If ![Time] = rcdReference3![Value] Then
  27.    Constant3 = rcdReference3![Result]
  28.    End If
  29.    ![Value].Value = Constant * Constant2 * Constant3
  30.    .Update
  31.    Next
  32.  End With
  33.  
  34. End Sub
Sep 1 '08 #1
11 1916
yaaara
77
Instead of a For Each statement, try looping through the recordset using the EOF property..

Expand|Select|Wrap|Line Numbers
  1. With rcdTesting
  2. For i=0 to .EOF
  3. .edit
  4. If ![Outing] = rcdReference![Value] Then
  5. Constant = rcdReference![Result]
  6. End If
  7. If ![Lap] = rcdReference2![Value] Then
  8. Constant2 = rcdReference2![Result]
  9. End If
  10. If ![Time] = rcdReference3![Value] Then
  11. Constant3 = rcdReference3![Result]
  12. End If
  13. ![Value].Value = Constant * Constant2 * Constant3
  14. .Update
  15. .MoveNext
  16. End With
  17. End Sub 
See if this works...
Sep 1 '08 #2
zoeb
17
Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF.

Expand|Select|Wrap|Line Numbers
  1. For I = 0 To .EOF
  2. With rcdTesting
  3.     .Edit
  4.    If ![Outing] = rcdReference![Value] Then
  5.    Constant = rcdReference![Result]
  6.    End If
  7.    If ![Lap] = rcdReference2![Value] Then
  8.    Constant2 = rcdReference2![Result]
  9.    End If
  10.    If ![Time] = rcdReference3![Value] Then
  11.    Constant3 = rcdReference3![Result]
  12.    End If
  13.    ![Value].Value = Constant * Constant2 * Constant3
  14.    .Update
  15.    .MoveNext
  16. End With
Any ideas?

Thanks for your help.
Sep 1 '08 #3
yaaara
77
Oops I forgot the "Next" statement just before the End With...

See if that works it out..

Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF.

For I = 0 To .EOF
With rcdTesting
.Edit
If ![Outing] = rcdReference![Value] Then
Constant = rcdReference![Result]
End If
If ![Lap] = rcdReference2![Value] Then
Constant2 = rcdReference2![Result]
End If
If ![Time] = rcdReference3![Value] Then
Constant3 = rcdReference3![Result]
End If
![Value].Value = Constant * Constant2 * Constant3
.Update
.MoveNext
End With

Any ideas?

Thanks for your help.
Sep 1 '08 #4
yaaara
77
Also, Please use it without swapping....

Oops I forgot the "Next" statement just before the End With...

See if that works it out..
Sep 1 '08 #5
hjozinovic
167 100+
Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF.

For I = 0 To .EOF
With rcdTesting
.Edit
If ![Outing] = rcdReference![Value] Then
Constant = rcdReference![Result]
End If
If ![Lap] = rcdReference2![Value] Then
Constant2 = rcdReference2![Result]
End If
If ![Time] = rcdReference3![Value] Then
Constant3 = rcdReference3![Result]
End If
![Value].Value = Constant * Constant2 * Constant3
.Update
.MoveNext
End With

Any ideas?

Thanks for your help.
You used .EOF before With statement.
Try:
Expand|Select|Wrap|Line Numbers
  1. rcdTesting.EOF
Sep 1 '08 #6
zoeb
17
Thanks for all your help everyone.

If I have the Next I, before the End With statement, it says "For without Next" and if I have the Next I after the End With statement, it says "With, without End With!".

Just wondered if there was another way of doing in - I've got a query somewhere else which calculates the maximum ID (which will always be the total number of records in the table). Would it be possible to replace the For I=0 to rcdTesting.EOF to For I=1 to TotalNoRecords (for example)?

I had a go at this but I didn't know how to select record 1, perform operation on record one, and then with the next iteration of the for loop go on to record 2.

Is there a syntax for example which:

I = 2

![Value].Value(I) = Constant * Constant2 * Constant3

where the (I) selects the record corresponding to ID 2?

Sorry I'm sued to MATLAB which would automatically step through records so this is all new to me :-)

Thanks for all your help.

Zoe

P.S. When I don't get either of the errors stated at the top, the for loop ends after one iteration.
Sep 1 '08 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi. You are using the wrong kind of loop here; rcdTesting is a recordset, and For ... Each applies to object collections, not recordsets. Although you can use a FOR loop you would need to access the recordcount of the recordset to get the stop value - not difficult, but unnecessary.

The norm is to use

Expand|Select|Wrap|Line Numbers
  1. Do While not rcdTesting.EOF
  2.    <statements currently in your FOR loop>
  3.   rcdTesting.Movenext
  4. Loop
The End of File property of the recordset is a boolean property (true/false, represented as -1 or 0). It cannot be used as the FOR-loop index stop value with any meaningful result.

For completeness, to use a FOR loop instead of the Do loop - which I don't recommend - you would need the following statements:

Expand|Select|Wrap|Line Numbers
  1. Dim lngRecCount as Long
  2. <... open your recordset etc>
  3. rcdTesting.movelast ' force a traverse of all records - sets record counter correctly
  4. rcdTesting.movefirst 
  5. lngRecCount = rcdTesting.Recordcount
  6. For I = 1 to lngRecCount
  7.    <...loop statements as before>
  8. Next I
-Stewart
Sep 1 '08 #8
Stewart Ross
2,545 Expert Mod 2GB
...and to add to my previous reply you will need more than one of these loops, as you are updating a different recordset than the one you are looping through.

You will find examples of multiple recordset processing elsewhere on this site (as in this howto article on Access VBA DAO recordset loop using two recordsets) and I suggest you consider what you are trying to achieve here. There may well be better methods using SQL updates (no code at all), but without knowing your application I can't advise further on this.

-Stewart
Sep 1 '08 #9
zoeb
17
That works brilliant and certainly solves that problem. Thank you!

Having just seen your extra answer I shall have a read as you were right I was having issues looping through additional recordests.

Thanks again, you've been great.

Zoe
Sep 1 '08 #10
yaaara
77
Thanks for the corrections above Steve... and I agree to you that multiple loops will have to be used as there are no iterations in the other multiple references that are being made and apparantly, the same values will be referred to again and again... I guess a different approach is required as suggested by you in the reply above :-)

...and to add to my previous reply you will need more than one of these loops, as you are updating a different recordset than the one you are looping through.

You will find examples of multiple recordset processing elsewhere on this site (as in this howto article on Access VBA DAO recordset loop using two recordsets) and I suggest you consider what you are trying to achieve here. There may well be better methods using SQL updates (no code at all), but without knowing your application I can't advise further on this.

-Stewart
Sep 1 '08 #11
NeoPa
32,556 Expert Mod 16PB
It's nice to see I'm not the only one that can learn by getting it wrong sometimes.

Trying to help is a laudable aim in itself :)
Sep 1 '08 #12

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

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
2
by: Unruled Boy | last post by:
1.The follow two ways to declare one object: any difference? especially its performance. a.Private m_objMyObject As MyObject=New MyObject() b.Private m_objMyObject As MyObject m_objMyObject=New...
19
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
34
by: sushant | last post by:
hi all, suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++) { for(j=0;j<=10;j++) { some code; }
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
7
by: Alvin Bruney | last post by:
Error: Collection was modified; enumeration operation may not execute. I've dodged this issue for a while now with workarounds but now i want to stand up and fight. I don't want to run away...
4
by: Aussie Rules | last post by:
Hi, I have the Windows media object placed on a web page. Since its not a .net component (its a com object) I have placed the code in the html source of the page. The problem I am having is...
13
by: Rick | last post by:
The following code will enter an infinate loop when in ReadChars. I can only make it happen when reading a Stream and with this particular XML. If I use the ReadInnerXml call rather than my own...
0
by: davidsavill | last post by:
Hi All, I am migrating a database from Firebird/Interbase to DB2 and have having issues with the stored procedures/functions. I have a number of functions that loop over a FOR loop, each pass...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.