473,385 Members | 1,523 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.

Understanding VBA Loop

7 of 9
23
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
Mar 27 '09 #1
6 2040
ChipR
1,287 Expert 1GB
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
Mar 27 '09 #2
7 of 9
23
@ChipR
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
Mar 27 '09 #3
ChipR
1,287 Expert 1GB
@7 of 9
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.
Mar 27 '09 #4
7 of 9
23
@ChipR
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.  
Mar 27 '09 #5
ChipR
1,287 Expert 1GB
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.
Mar 27 '09 #6
7 of 9
23
ok I think I get it now thanks ChipR
Mar 27 '09 #7

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

Similar topics

2
by: Bruce | last post by:
I don't quite understand this. $result is the result of a mysql_query. $requests = array(); while ($request = mysql_fetch_array($result)) { print("<p>"); print_r($request);...
2
by: Samuele Giovanni Tonon | last post by:
hi, i'm trying to develop a trivial application which random copy files from a directory to another one. i made it using pygtk for the graphical interface, however i find some problem with...
1
by: nightsaber | last post by:
<script language="JavaScript"> <!-- hide me var the_number = prompt("how many words (3-5 is good)?", "4"); var the_string = ""; var a_word; for (loop = 0; loop < the_number; loop++) {...
24
by: bazad | last post by:
Hi, I'd like to understand consequences of Application.DoEvents call. Does it create a new thread? Thank you
10
by: yes | last post by:
Could someone help me understand this: { int i=-3,j=2,k=0,m; m=++i && ++j || ++k; } { int i=-3,j=2,k=0,m; m=++i || ++j && ++k; }
26
by: Bail | last post by:
I will have a exam on the oncoming friday, my professor told us that it will base upon this program. i am having troubles understanding this program, for example what if i want to add all the...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
5
by: teddysnips | last post by:
Is this a bug? I open a DataReader dr from a SQL Server SPROC Do While dr.Read Dim ClubDay As New ChildrensClubDay With ClubDay Dim MPInfo As PaymentInformation If Not dr.GetDateTime(1) =...
3
by: Tom Plunket | last post by:
....at least, I think that I'm having a problem understanding the way closures work. I'm trying to define a function for an object which will take certain objects from the parent scope at the...
1
by: silverburgh.meryl | last post by:
Hi, I need some help in understanding the following for() loop: for (PRBool isRoot; mCSSUtils->IsRuleNodeRoot(ruleNode, &isRoot), !isRoot; mCSSUtils->GetRuleNodeParent(ruleNode, &ruleNode)) ...
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
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
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.