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

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.GoToRecord , , acLast 'Goto the last record
CountOfRecords = Form_Input.CurrentRecord 'save position

'now the loop

For RecordNr = 1 To CountOfRecords

DoCmd.GoToRecord , , 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 17959
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********@gsp-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.OpenRecordset("myTableName", dbOpenDynaset, dbSeeChanges)
RSValues.MoveLast
RSValues.MoveFirst ' 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********@hotmail.com (Ryan) wrote in
news:78**************************@posting.google.c om:
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.OpenRecordset("myTableName", dbOpenDynaset,
dbSeeChanges) RSValues.MoveLast
RSValues.MoveFirst ' 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.OpenRecordset("myTableName", dbOpenDynaset, dbSeeChanges)
RSValues.MoveLast
RSValues.MoveFirst ' 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.MoveNext '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..loop, 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.google. 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.OpenRecordset("myTableName", dbOpenDynaset, dbSeeChanges)
RSValues.MoveLast
RSValues.MoveFirst ' 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.MoveNext '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..loop, 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
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...
4
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...
8
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...
20
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...
7
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...
1
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...
1
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. ...
3
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...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.