473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to compare rows in a table

27 New Member
I have a table that receives data from an external source.
The data is sorted by time in the format of "dd/mm/yyyy hh:nn:ss"
Some of the time cells have the same time as the follwing rows (due to rounding problems that are not connected to Access).
I want to compare each row to the next and fix the time.

I usually use querries to append data in tables but I can't manage to access a specific row using querries, so I tried a sub.

What I wrote so far is:
Expand|Select|Wrap|Line Numbers
  1. Public Sub FindDuplicates(varTime As Variant)
  2. Dim i As Integer
  3. i = 0
  4. Do While Not EOF(1)
  5.     If varTime(i) = varTime(i + 1) Then
  6.         If Second(varTime(i)) = (Second(varTime(i - 1)) + 10) Then
  7.             'FindDuplicates = DateAdd("s", 10, DateValue(varTime(i + 1)))
  8.             Second(varTime(i + 1)) = Second(varTime(i + 1)) + 10
  9.         ElseIf Second(varTime(i)) = (Second(varTime(i - 1)) + 20) Then
  10.             'FindDuplicates = DateAdd("s", -10, DateValue(varTime(i)))
  11.             Second(varTime(i)) = Second(varTime(i)) - 10
  12.         End If
  13.     End If
  14. i = i + 1
  15. Loop
  16. End Sub
when I call it (in VBA):
FindDuplicates (T_table1.Time)

I get "object required"
May 23 '10 #1
3 9014
Jim Doherty
897 Recognized Expert Contributor
@izharmel
If you are working in tables let us assume your existing tablename called T_Table1 that has a field called 'Time' (not a good idea to name fields as internal functions or reserved words by the way)

If you have a time value duplicated how many times is 'each' date value actually duplicated? If it was just once then ok you could do a quick 'move next' , do your amendments and 'move back' and keep doing that as you traverse a single recordset to the EOF.

But working on the logic that you have no idea how many occurrences there are then sorting your recordset by the time column at least groups the 'identicals' together to reflect a visual on this.

The following recordset code logic opens TWO recordsets. The first recordset basically grouping by the Time column and 'counting' the instances of each individual time returning ONLY those times where the count is greater than ONE......this identifies your duplicates.

The idea then is to trundle down this recordset in a loop and open a 'second' recordset that relates only to the individual Time value encountered. It is in this second recordset that any 'DateAdd' function field editing logic takes place to increment or change the time value to suit yourself (ie: rs.edit.....rs.Update and so on). For each of these individual recordsets you can amend the time value for each and every row encountered.

I am sure you will get the logic looking at the recordset code below. You can safely run it in the immediate window to see how it reflects your data as it is, It is heavy on the Debug.Print statements just to illustrate the flow. The SQL should work hopefully given I have taken your existing naming convention

I havent gone into your 'DateAdd' logic in this post, as I feel you are well familiar with that and what it is you need to do there

Post the following code into a new module and save it. Then type DoMyDates in the immediate window and hit the enter key.


Expand|Select|Wrap|Line Numbers
  1. Function DoMyDates()
  2. Dim db As DAO.Database
  3. Dim rsDup As DAO.Recordset
  4. Dim rs As DAO.Recordset
  5. Set db = CurrentDb
  6. SQLDup = "SELECT T_Table1.Time, Count(T_Table1.Time) AS TotalOf "
  7. SQLDup = SQLDup & "FROM T_Table1 GROUP BY T_Table1.Time HAVING (((Count(T_Table1.Time))>1));"
  8. Set rsDup = db.OpenRecordset(SQLDup, dbOpenSnapshot)
  9. rsDup.MoveFirst
  10. lc = 1
  11. Do While Not rsDup.EOF
  12.     Debug.Print "--------------------------------------------------------------------------"
  13.     Debug.Print "THIS IS THE DUPLICATED OUTER LOOP TIME VALUE NO(" & lc & ") " & rsDup!Time
  14.     Debug.Print "--------------------------------------------------------------------------"
  15.     Set rs = db.OpenRecordset("SELECT * FROM T_Table1 WHERE [Time]=#" & rsDup!Time & "#", dbOpenDynaset)
  16.     rcnt = 1
  17.         Do While Not rs.EOF
  18.             If rcnt = 1 Then
  19.             Debug.Print "We are going to skip the first row (" & rcnt & ") time value " & rs!Time
  20.             Else
  21.             'This is where you do you DateAdd logic each time in the loop to suit you
  22.                     Debug.Print "...and amend this inner loop row (" & rcnt & ") " & rs!Time 'do the dateadd logic
  23.  
  24.             End If
  25.         rcnt = rcnt + 1
  26.         rs.MoveNext
  27.         Loop
  28.     rs.Close
  29. rsDup.MoveNext
  30. lc = lc + 1
  31. Loop
  32. rsDup.Close
  33. db.Close
  34.  
  35. Set rs = Nothing
  36. Set rsDup = Nothing
  37. Set db = Nothing
  38. End Function 
May 23 '10 #2
izharmel
27 New Member
Hi,

Thanks for the help. I really learned a lot from your code.

There is another topic I didn't address properly in the previous post.
As a result of some cells appearing twice or more, other cells may not appear at all.
For instance I may get a table looking like:
27/05/2010 16:58:30
27/05/2010 16:59:10
27/05/2010 16:59:10
27/05/2010 16:59:10
etc.

That’s the reason I wrote my initial code the way I did…
Is there a way to solve this?

Another thing is that I have several tables with the same problem. How can I make the function you wrote more universal?

Thanks in advance,

Izhar
May 27 '10 #3
Jim Doherty
897 Recognized Expert Contributor
@izharmel
Am not sure I understand you. Any routine is specific to the case on which it is based. Yours was specific so the example was specific. If you want to pass in string field names and table name for the function to examine and work on some kind of universality principle then yes..... but you would still have some logical SQL string to build and design the thing according to specific needs

I am pleased you learned some concepts is how to throw this data around but I cannot answer every eventuality of your data scope unfortunately...I dont have it
May 27 '10 #4

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

Similar topics

1
by: dave_wheels | last post by:
hi there, new to this database mularkey and needing some advice. I have two tables that I want to compare. If the results of each are the same I award a mark. Can someone give me the general SQL...
0
by: Shaw | last post by:
Our database is constantly updated (input data) from another DB, and sometimes it crashes our ASP.NET applications. My boss told me to write a DB utility app to check DB and make sure all apps are...
1
by: The Rookie | last post by:
Hi everybody, The rookie is back... Ok I'm working actually on a lot of differents financial excel tables (some with calculation'macro) and most of the time I need to check manually if every...
3
by: DONE1 | last post by:
I wrote a code as follows to compare 2 tables on diff worksheet. Each work sheet has about 6000 Rows. The code works but appears to Work very slow. Is there a more efficient way to do it. Sub...
11
by: inpuarg | last post by:
I have 2 datatables. They are identical. I want to compare them by cell's content. They are all same. But dt1 == dt2 or dt1.GetHashCode() == dt2.GetHashCode() doesn 't work. There are big...
3
by: ago | last post by:
Hi, Is there any way to compare two identical table structures in access for different values in them. EG: Table 1: Name occupation rob plumber Table 2: Name occupation
2
by: fespinosaj | last post by:
Hi I need to caompare some records resulting from a query vs a string like this SKU ------------- 61386319 61416021 61416064 61416072
1
by: Spoogledrummer | last post by:
Hi, I'm working on a report that uses a gridview to display the information, I've got to the rowdatabound part where I need to blank out some repeated fields if the values are the same as that of the...
39
by: capablanca | last post by:
How to compare one row with the other ones to see if that row has a match into a 2D array and if there is any match, display it . It is this possible to do it? example: ...
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
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...
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...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.