Connecting Tech Pros Worldwide Forums | Help | Site Map

Understanding VBA Loop

7 of 9's Avatar
Newbie
 
Join Date: May 2007
Posts: 23
#1: Mar 27 '09
Hello all, I need help with a "simple" loop. I have an employee table that has 50 records and i want to run a query (qryEmp01Sel) for each employee. So far I have been doing this manually but I would like to automate it. How to I create the following loop in VBA:

Expand|Select|Wrap|Line Numbers
  1. select * from qryEmp01Sel 
  2. Where EmpID = (first employee) 
  3. output results to file ( I know how to do this part) 
  4. then move on to the next employee.
Thanks in advance

Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,175
#2: Mar 27 '09

re: Understanding VBA Loop


Here's a quick example:
Expand|Select|Wrap|Line Numbers
  1. Dim records as Object
  2. Dim strEmpID as String
  3.  
  4. Set db = CurrentDb()
  5. Set records = db.OpenRecordset("SELECT * FROM qryEmp01Sel")
  6.  
  7. While Not records.EOF
  8.     strEmpID = records!empID
  9.     ' do stuff
  10.  
  11.     records.MoveNext
  12. Wend
7 of 9's Avatar
Newbie
 
Join Date: May 2007
Posts: 23
#3: Mar 27 '09

re: Understanding VBA Loop


Quote:

Originally Posted by ChipR View Post

Here's a quick example:

Expand|Select|Wrap|Line Numbers
  1. Dim records as Object
  2. Dim strEmpID as String
  3.  
  4. Set db = CurrentDb()
  5. Set records = db.OpenRecordset("SELECT * FROM qryEmp01Sel")
  6.  
  7. While Not records.EOF
  8.     strEmpID = records!empID
  9.     ' do stuff
  10.  
  11.     records.MoveNext
  12. Wend

Thanks but I don’t think I was clear the first time around. I need the query results to show results for only 1 employee at a time because after the query runs I output the result to a file for each employee. Doing it this way runs the query and puts the results for all employees in each file. Should I be referencing the employee table (tblEmployee) somewhere?

thanks
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,175
#4: Mar 27 '09

re: Understanding VBA Loop


Quote:

Originally Posted by 7 of 9 View Post

Doing it this way runs the query and puts the results for all employees in each file.

I'm not sure what you mean. At line 9, you have 1 record, and the employee name in that one record. At line 11 you go to the next record. In between, you can do whatever you want, including open a different file each time through the loop. We can look at your code for
Expand|Select|Wrap|Line Numbers
  1. 3. output results to file ( I know how to do this part) 
if you'd like.
7 of 9's Avatar
Newbie
 
Join Date: May 2007
Posts: 23
#5: Mar 27 '09

re: Understanding VBA Loop


Quote:

Originally Posted by ChipR View Post

We can look at your code for

Expand|Select|Wrap|Line Numbers
  1. 3. output results to file ( I know how to do this part) 
if you'd like.

Sure here is what i have

Expand|Select|Wrap|Line Numbers
  1. Dim records as Object 
  2. Dim strEmpID as String 
  3.  
  4. Set db = CurrentDb() 
  5. Set records = db.OpenRecordset("SELECT * FROM qryEmp01Sel") 
  6.  
  7. While Not records.EOF 
  8.     strEmpID = records!empID 
  9.     DoCmd.OutputTo acOutputQuery,  ""& strEmpID & "SalesData", "HTML", "E:\Sales\" & strEmpId & "sales.html
  10.  
  11.     records.MoveNext 
  12. Wend
  13.  
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,175
#6: Mar 27 '09

re: Understanding VBA Loop


For each employee you are outputting a query. The data in the output file is generated by the query, so if you want to change the output, we'll have to look at the query itself.
7 of 9's Avatar
Newbie
 
Join Date: May 2007
Posts: 23
#7: Mar 27 '09

re: Understanding VBA Loop


ok I think I get it now thanks ChipR
Reply

Tags
loop