473,698 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looping through all records with VBA

Hi,

I'm trying to write a loop that cycles through all the records. This I use
for doing comparisons between all records or to export all records to a text
file. I'm using the following code:

'************** *************** ********
....
'Finding the count of all records

DoCmd.GoToRecor d , , acLast 'Goto the last record
CountOfRecords = Form_Input.Curr entRecord 'save position

'now the loop

For RecordNr = 1 To CountOfRecords

DoCmd.GoToRecor d , , acGoTo, datensatz 'Load the record

'Now, I do things like writing the values of the record to a text file
'or using a second similar for-next-loop way to compare each record
'with every other one.

Next RecordNr
....
'************** *************** *************** ******

This however yields very strange results. Most of the time, the value of
CountOfRecords is incorrect, especially if I just added new records, the new
records are not taken into account. But there are other errors too. In fact,
it seems very much unpredictable what happens. In the case where I export to
the textfile, I frequently get the values of the same record written
CountOfRecords-times.
What code would you use to loop through all the records? Is there any other
way to find the total count of records that always yields the correct
result? Any other hints?

Thanks in advance,
Michael Goerz
Nov 12 '05 #1
6 17977
fp
Rather than use looping through records, why not use the power of a
relational database? Select distinct values for each field or field
combination you are looking for, save those then do your export based on the
saved information. Rather than save the search values in a text file, use a
temporary table. It should make your coding a lot easier.

--
*************** ***************
Fred Parker
Lynn Consulting Group, L.L.C.
http://www.lynnconsultinggroup.com
*************** ***************
Nov 12 '05 #2
rkc

"Michael Goerz" <mg********@g sp-online.org> wrote in message
news:bs******** ****@ID-167374.news.uni-berlin.de...
Hi,

I'm trying to write a loop that cycles through all the records. This I use
for doing comparisons between all records or to export all records to a text file. I'm using the following code:


If you really want to loop through all the records of a form's recordsource,
it would probably be easier to use DAO and the form's recorsetclone.
Nov 12 '05 #3
Cut down a bit from some code I wrote a while back, but it may be of some help.

Ryan

Function FRED()
On Error Resume Next
Dim DB As Database
' Microsoft DAO 3.6 Object Library MUST be available for this
' in the references
Dim RSValues As DAO.Recordset
Set DB = CurrentDb()
Set RSValues = DB.OpenRecordse t("myTableName" , dbOpenDynaset, dbSeeChanges)
RSValues.MoveLa st
RSValues.MoveFi rst ' Must do this to set the recordset properly.
While Not RSValues.EOF
' Now step through all available records and do whatever you need to do
Wend
MsgBox "Finished", vbInformation, "Whatever your app is called"
End Function
Nov 12 '05 #4
ry********@hotm ail.com (Ryan) wrote in
news:78******** *************** ***@posting.goo gle.com:
Cut down a bit from some code I wrote a while back, but it may be of
some help.

Ryan

Function FRED()
On Error Resume Next
Dim DB As Database
' Microsoft DAO 3.6 Object Library MUST be available for this
' in the references
Dim RSValues As DAO.Recordset
Set DB = CurrentDb()
Set RSValues = DB.OpenRecordse t("myTableName" , dbOpenDynaset,
dbSeeChanges) RSValues.MoveLa st
RSValues.MoveFi rst ' Must do this to set the recordset properly. No While Not RSValues.EOF
' Now step through all available records and do whatever you need to
do
Wend
MsgBox "Finished", vbInformation, "Whatever your app is called"
End Function


--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #5
Just some minor clarifications - not sure if Lyle was trying to make
them, his comments are rather sparse...
Function FRED()
On Error Resume Next
Dim DB As Database
' Microsoft DAO 3.6 Object Library MUST be available for this
' in the references
Dim RSValues As DAO.Recordset
Set DB = CurrentDb()
Set RSValues = DB.OpenRecordse t("myTableName" , dbOpenDynaset, dbSeeChanges)
RSValues.MoveLa st
RSValues.MoveFi rst ' Must do this to set the recordset properly.
You only need to do this if you wish to get the recordsets recordcount
property otherwise it's an extra overhead.
While Not RSValues.EOF
' Now step through all available records and do whatever you need to do
rsValues.MoveNe xt 'otherwise you'll just sit there and never
reach EOF
Wend
I thought I read somewhere where While and Wend were being dropped
from VBA (sorry can't find the reference so treat as hearsay). I tend
to use do While|Until..lo op, it's a little more flexible for loops.

'in-line error handling as using On Error Resume Next; unless you
really
'do want to ignore any error (not a good habit IMO)
If err.Number = 0 then MsgBox "Finished", vbInformation, "Whatever your app is called" else
msgbox err.number & " : " & err.description
end if End Function


Peter
Nov 12 '05 #6
It was only an example, if I posted all of the code it would have
overcomplicated the matter. The code I wrote does have all of this in,
but I cut it down for the example as it wasn't particularily relevant
IMHO (and was trying to get the point across quickly), however they
are still valid points and should be used.

Interesting about the While...Wend comment. Will look for more info on
that as I've not heard anything about that.

Ta

R

Pi************* @mail.com (Pink Panther) wrote in message news:<ec******* *************** ****@posting.go ogle.com>...
Just some minor clarifications - not sure if Lyle was trying to make
them, his comments are rather sparse...
Function FRED()
On Error Resume Next
Dim DB As Database
' Microsoft DAO 3.6 Object Library MUST be available for this
' in the references
Dim RSValues As DAO.Recordset
Set DB = CurrentDb()
Set RSValues = DB.OpenRecordse t("myTableName" , dbOpenDynaset, dbSeeChanges)
RSValues.MoveLa st
RSValues.MoveFi rst ' Must do this to set the recordset properly.


You only need to do this if you wish to get the recordsets recordcount
property otherwise it's an extra overhead.
While Not RSValues.EOF
' Now step through all available records and do whatever you need to do


rsValues.MoveNe xt 'otherwise you'll just sit there and never
reach EOF
Wend


I thought I read somewhere where While and Wend were being dropped
from VBA (sorry can't find the reference so treat as hearsay). I tend
to use do While|Until..lo op, it's a little more flexible for loops.

'in-line error handling as using On Error Resume Next; unless you
really
'do want to ignore any error (not a good habit IMO)
If err.Number = 0 then
MsgBox "Finished", vbInformation, "Whatever your app is called"

else
msgbox err.number & " : " & err.description
end if
End Function


Peter

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1722
by: Comcast News | last post by:
Hi , We are facing a java issue. We are looping thru a resultset which has about 40 K records. The process terminates abnormally after about 20-25K records. The Query fetches has 400 columns. We have recently modified the Query, earlier it had only 200 columns and the process used to work fine. Any suggestions !! Thanks -rahul
4
4079
by: Roy Adams | last post by:
Hi posting again because no answer to previous.. tring to loop through a recordset and update a record, thing is it only updates the first record in the table rather than searching through the entire table or records returned, and updating a record if certain criteria is met. shouldn't the while loop do this? I know my syntax must be wrong, but difficult to work out how or where table = String(Request.Cookies("table"));
8
2091
by: RC | last post by:
I have a table that lists many box numbers. Each box number has a Pallet Number (indicating which pallet the box is in). When the Pallets are loaded into a shipping Container I need to update the table to indicate which pallets and boxes are in the container. In my code below, in the table named "Products", I find the first Pallet Number that matches the Pallet Number typed in the box on my form (PalletNumberContainerFormComboBox)....
20
3055
by: Stewart Graefner | last post by:
Here is a chunk of code that works for an individual record. It evaluates dates and checks or unchecks boxes as it goes along. It may not be pretty but it works. What my problem is that I need it to evaluate all the records(200+) in my db and change those which need changing. Having to do it individually would defeat the purpose of developing this code. What I would like to be able to do is either 1. Open the db push a button and all the...
7
5467
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I loop through the filtered GridView to identify the selected rows and assemble some XML to be sent to a stored proc. The problem I have is that when looping through the GridView, it doesn't
1
3762
by: lucazz | last post by:
I have a main form (not bound to any data source) with a subform based on a query. The subform shows furniture parts according to the criteria specified on the main form. The purpose of the subform is for user to enter produced quantities. To do this I've added a quan field to the subform that is not bound to any data. The problem is that whenever I enter a value in a record the same value appears in all the subform's records. The other way was...
1
5316
by: Ryan | last post by:
Hello. I was hoping that someone may be able to assist with an issue that I am experiencing. I have created an Access DB which imports an Excel File with a particular layout and field naming. Next the user can go into a Form which basically a dynamic query with a friendly interface that eventually outputs a table that is stored in the DB as well as exported to a CSV file. The CSV file is then used with a vendor solution to fill in...
3
3655
by: DWolff | last post by:
My application is to re-assign leads to different groups of salespeople by sequentially assigning them to each salesperson. I've got an Access 2000 front end to an MS-SQL database. Currently, I do this effectively (but sloppily and slowly) by exporting Access records to Excel, doing my assignments there, importing back into a new work table in Access, and running an update query (joining on Lead.ID). However, I'm soon going to exceed the...
3
1894
by: David | last post by:
Hi, I have an asp page which lists records out in rows Each record has a checkbox with a value parameter equal to the RecordID When the form is run, it goes to a page which I am trying to create 1 report printed after the other. i.e. if the user selects 3 records on the form, the report is printed on the next asp page for record 1 and then straight after it runs the
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.