473,386 Members | 1,694 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,386 software developers and data experts.

Looping in recordset evaluating Object in Rows-remove the record if the object equals

I am needing to evaluate a record set and remove records. My table has multiple Records with Unique Effective Dates by Action. I need to compare each record to the next record and remove the record in the table if the Action in the record below it is equal. I started with a simple OpenRecordset for printing to identify database and record set below. I am a beginner of Access/VBA
Expand|Select|Wrap|Line Numbers
  1. EFFDT       ACTION    
  2. 8/23/2015  PLA
  3. 12/4/2015  PLA    Need to remove
  4. 3/6/2016   RFL
  5. 10/18/2018 PLA
  6. 1/28/2019  PLA    Need to remove
  7. 6/9/2019   RFL
Expand|Select|Wrap|Line Numbers
  1. Public Sub OpenRecordset()
  2. Dim db As Database
  3. Dim rs As Recordset
  4.  
  5. Set db = CurrentDb
  6.  
  7. Set rs = db.OpenRecordset("tblLOA")
  8.  
  9. '---------Work in Process for evaluating Records and Delete
  10.             Last = rs.MoveLast
  11.         For i = Last To 2 Step -1
  12.                 If (Fields(i, "Action").Value) = (Fileds(i - 1, "Action").Value) Then
  13.                 Fields(i, "Action").EntrieRow.Delete
  14.                 End If
  15. '--------------------------------------------------------------------------------------
  16. For i = 0 To rs.RecordCount - 1
  17. Debug.Print rs.Fields("ACTION")
  18.         rs.MoveNext
  19. Next i
  20.  
  21. rs.Close
  22. Set rs = Nothing
  23. db.Close
Mar 30 '21 #1
1 3629
NeoPa
32,556 Expert Mod 16PB
Hi & welcome to Bytes.com.

Let's start with a quick tip (Before Posting (VBA or SQL) Code) about posting code that will help everyone involved. Lots of time & effort can be saved that way.

Moving on to your fundamental question. Again, let's start with some basic advice. Database tables (Access or anywhere) are sets of data, but they are not generally considered to be ordered sets of data. It may be that in some circumstances they can be used that way - but it's not good practice ever to assume that data from a table has any order. That's not a serious problem as the SQL ORDER BY clause can produce an ordered set for you. You must design the table in such a case to ensure it has data that you can use to specify the order. Options for this are to set a standard AutoNumber field or to set a field where the DefaultValue is =Now().

At this point I'll assume you're now working with a Recordset in your code that includes ordering properly. It's actually one of those rare areas where processing through the Recordset in VBA is easier & more straightforward than trying to execute the same operation directly in SQL. However, I strongly advise against a procedure called OpenRecordset() as that is a reserved name. It can work for you, but it will throw you over many pitfalls and is certainly better to avoid.

Processing through a DAO.Recordset is normally handled using a Do loop such as :
Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset(...)
  2. With rs
  3.     Do Until .EOF
  4.         ...
  5.         Call .MoveNext
  6.     Loop
  7. End With
Your procedure should have a String variable (EG. strLast) Dimmed which you can use to save the value for the last (previous) record. When the new value equals the previous one then delete the current record.

Deleting a record, within your With structure & Do loop of course, is done something like :
Expand|Select|Wrap|Line Numbers
  1. Call .Delete
In case it helps, MS have a page (Delete a record from a DAO Recordset) with example code for doing very similar work you can use as a starting point. The style isn't great (It doesn't even use With ... End With.) but it illustrates what's what.
Mar 30 '21 #2

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

Similar topics

1
by: Mad Hot dog | last post by:
Hello group! I use MS Visual C++ 6.0, ADO, MS SQL Server 2000. When I attempt to open my database I meet with a following problem: when I try to get a bookmark of the current record in a...
1
by: Alex | last post by:
Is there a way to reference the "parent" object that creates a child object from within the child object? Or do I have to pass a reference to the parent object to the constructor of the child...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: Ian | last post by:
I want to open a form at a particular record, but I think I'm running into problems because the recordsource query is executing asynchronously. In the form's open event I use...
0
by: Chua Wen Ching | last post by:
Hi there, I am super confuse on this recommendation by FxCop. 1) http://www.gotdotnet.com/team/fxcop/docs/rules/UsageRules/OperatorEqualsOverridesRequireEqualsOverridel.html First of all,...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
21
by: Gaemic | last post by:
Some argues that you should not use the C# keyword "object" instead of the real type System.Object. What would be the general rule of thumb for choosing one over the other? Thanks in advanced....
1
by: nick | last post by:
I have the following code in JScript. Should I close and release the database objects before run response.redirect("some url")? or will the response.redirect automatically close the database...
2
by: tlk | last post by:
Hi, everyone. I'm having a problem that I hope some more experienced developers can help me figure-out. We have a project called Records that has been around for a year or so. We're currently...
3
by: Grant Schenck | last post by:
Hello, I have a Windows Service developed in C# .NET. I'm making it a remote server and I can, via an IPC Channel, expose methods and call them from a client. However, I now want my remoted...
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: 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...
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...
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...
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
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,...
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.