473,387 Members | 1,420 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.

For Each Next Problem

Hello Everyone,
I am having a spot if difficulty creating a process loop using the For Each Next procedure.
I basically have a field in a table that stores a file location, This is passed to a query which has two fields. One field is the original file location, and the other is a new file path and filname based on the primary key for the record.
The trouble is that it performs the required action on the first record in the query, and then exits the loop before processing any subsequent records.

Here is the code I am trying to use, please can anyone help.

Expand|Select|Wrap|Line Numbers
  1. Function CopyFiles()
  2. Dim strImageLoc As String
  3. Dim strMoveToFilename As String
  4. Dim retval As String
  5. Dim strSQL As String
  6. Dim strText As Variant
  7. Dim Prefix As String
  8. Dim Suffix As String
  9. Dim db As DAO.Database, rst As DAO.Recordset
  10.  
  11. strSQL = "SELECT qryImageCopyTo.strImageLoc, qryImageCopyTo.strMoveToFilename " _
  12. & "FROM qryImageCopyTo;"
  13.  
  14. Set db = CurrentDb
  15. Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
  16.  
  17. strText = rst("strImageLoc")
  18.  
  19. On Error Resume Next
  20.  
  21. For Each strText In rst
  22.   FileCopy rst("strImageLoc"), rst("strMoveToFilename")
  23. Next strText
  24.  
  25.  
  26. End Function
I am new to using loops, so I have probably made a real Newbie error.

Regards

Tony
Mar 16 '08 #1
2 1641
Stewart Ross
2,545 Expert Mod 2GB
Hi Tony. A FOR loop isn't the right one for this application, and FOR..EACH would apply to an array or a Collection of some kind (such as looping through the Forms collection to display the names of all forms).

Replace lines 21-23 above with:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rst.EOF
  2.     FileCopy rst("strImageLoc"), rst("strMoveToFilename")
  3.     rst.Movenext
  4. Loop 
  5. rst.Close
Note also that if you open a recordset you must explicitly close it again, hence the Close immediately after the loop.

-Stewart
ps line 17 does not seem to be required - you should remove it, as the recordset's end of file (EOF) status is not tested before accessing the recordset at that point in your code. If you were to try to read the string and the recordset was empty (no records) you would get a run-time error at that point.
Mar 16 '08 #2
Thanks Stewart,
I did say I am a newbie to the process loops, and this has been bugging me all afternoon. It worked first time.

Thanks again!

Tony
Mar 16 '08 #3

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

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
2
by: Wonko | last post by:
Here's my problem if anyone could be so kind to help me out. I assume it's quite easy for an experienced programmer but I'm not one of them :) I have a JavaScript code that: - displays multiple...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
14
by: Ale K. | last post by:
i know that For...Next as a Exit For.... there is any way from the middle of my for...next code to go to the next item and jump out part of my code , like the same thing that can be done with exit...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
13
by: andreas | last post by:
Hi, I want to do some calculation like ( t1 and t2 are known) for i = t1 to t2 for j = t1 to t2 ..... ..... for p = t1 to t2 for q = t1 to t2
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
1
by: Tony Dunn | last post by:
I have inherited another developer's database (Access 2003) and with it a problem I'm struggling with. The problem is this. I have a parent-child form pair, which is populated by a table (the...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
0
by: solargovind | last post by:
Hello, I have few problem with Dlookup condition. I need to retrieve next record or previous record based on certain condition. The conditions are set in in the combo box. Here, I am trying to...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.