473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Rows in Succession in a Recordset using Arrays and Conditionals

37 New Member
I am working in Access 2003 in Windows XP. I am using Visual Basic for Applications, using DAO to write my modules. What I need to do is write a module that will compare each row to see if they are equal to each other or not. Basically, equal numbers in succession indicate a "music chord" which is why I need to do this. For example, if the table is like so:

Notes.........Chord
A.....................111
B.....................111
C.....................111
F.....................56
G....................56
A....................56
A....................43
B....................23
...
Then, I would need output like this:

<<A
B
C>>
<<F
G
A>>
A
B

And the module that I wrote which transposes the columns to rows, produces this output:
<<A B C>> <<F G A>> A B

And this is the correct format I am looking for.

The brackets around the note names indicate a music chord. According to the example above, what I need to do is compare the current recordset under "Chord", to the adjacent "Chord" recordset. If equal, put brackets around note A. Then compare the second recordset with the next recordset. If equal, do nothing. Then compare the 3rd recordset with the 4th recordset. Since it is NOT equal, put opposite backets around the note "c". And so on.

What I don't know how to do is fetch a row, analyze it, manipulate it and move to the next row using DAO. For simplicity, here is a piece of code that I would like to get working, in order to crack this: (Code does NOT work, I need to use an array but I do not know how to implement it.)

Expand|Select|Wrap|Line Numbers
  1. Do Until rst.EOF
  2.     strChord = rst!Chord
  3.     If strChord = "111" Then
  4.     sSQL = "INSERT INTO Test (Notes)" & "VALUES('ItWorks' & '" & strNote & "')"
  5.     db.Execute sSQL
  6.     Else
  7.     sSQL = "INSERT INTO Test (Notes)" & "VALUES('" & strNote & "')"
  8.     db.Execute sSQL
  9.     End If
  10.     rst.MoveNext
  11. Loop
  12.  
The logic in words....If Chord = "111" then insert values "ItWorksX", into table Test under column Notes where X is a Note letter, for any Chord value that equals "111". So the output according to the above data set (if written correctly) would be:

Notes
ItWorksA
ItWorksB
ItWorksC
F
G
A
A
B

I hope this is clear enough. I think I need to use an array "getrows?" but I am not sure how to use it. I looked all over the web for examples, but I think my situation is unique. I will continue to clarify if need be. Thanks in advance!
Mar 21 '07 #1
6 2190
Denburt
1,356 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. Cnt=1
  2. Do Until rst.EOF
  3.     strNote =rs!Note
  4.     strChord = rst!Chord
  5.     If strChord = strChord1 and Cnt=1 Then
  6.     sSQL = "INSERT INTO Test (Notes)" & "VALUES('<<' & '" & strNote & "')"
  7.     db.Execute sSQL
  8.     Elseif strChord <> strChord1 and Cnt>1 Then
  9.       sSQL = "INSERT INTO Test (Notes)" & "VALUES('" & strNote & ">>')"
  10.     strChord1 =strChord
  11.    Cnt=1
  12.    else
  13.       sSQL = "INSERT INTO Test (Notes)" & "VALUES('" & strNote & "')"
  14.       db.Execute sSQL
  15.    End If
  16.     rst.MoveNext
  17. Loop
  18.  
It may need a little touching up but it should produce the desired results as I understand them.
Mar 21 '07 #2
Denburt
1,356 Recognized Expert Top Contributor
After I posted my previous I saw that iwas full of holes this one should be a bit closer.I do tend to over think things at times so if any else has a better idea...
Expand|Select|Wrap|Line Numbers
  1. Cnt=1
  2. Do Until rst.EOF
  3.     strChord = rst!Chord
  4.  strNote =rs!Note
  5.  if rs.bof then rs.movenext
  6.  strChord1 = rs!Chord
  7.  rs.moveprevious
  8.  end if
  9.     If strChord = strChord1 and Cnt=1 Then
  10.     sSQL = "INSERT INTO Test (Notes)" & "VALUES('<<'" &  strNote & "')"
  11.     db.Execute sSQL
  12.     Cnt=Cnt+1
  13.     Elseif strChord <> strChord1 and Cnt>1 Then
  14.       sSQL = "INSERT INTO Test (Notes)" & "VALUES('" & strNote & ">>')"
  15.      strChord1 =strChord
  16.     Cnt=1
  17.    else
  18.       sSQL = "INSERT INTO Test (Notes)" & "VALUES('" & strNote & "')"
  19.     db.Execute sSQL
  20.     End If
  21.     rst.MoveNext
  22. Loop
  23.  
Mar 21 '07 #3
chopin
37 New Member
Thank you for getting back to me so quickly. I tried running the code, and all it did was output the Note letters. Furthermore, I think we need one more "else if" for the notes between the brackets. My addition is in bold:

Expand|Select|Wrap|Line Numbers
  1. If rst.BOF Then
  2.  rst.MoveNext
  3.  strChord1 = rst!Chord
  4.  rst.MovePrevious
  5.  End If
  6.     If strChord = strChord1 And cnt = 1 Then
  7.     sSQL = "INSERT INTO Test (Notes)" & "VALUES('<<' " & strNote & " ')"
  8.     db.Execute sSQL
  9.     cnt = cnt + 1
  10.   ElseIf strChord = strChord1 And cnt > 1 Then
  11.       sSQL = "INSERT INTO Test (Notes)" & "VALUES(' " & strNote & " ' )"
  12.   ElseIf strChord <> strChord1 And cnt > 1 Then
  13.       sSQL = "INSERT INTO Test (Notes)" & "VALUES(' " & strNote & ">>')"
  14.      strChord1 = strChord
  15.     cnt = 1
  16.    Else
  17.       sSQL = "INSERT INTO Test (Notes)" & "VALUES(' " & strNote & " ')"
  18.     db.Execute sSQL
  19.     End If
  20.     rst.MoveNext
  21. Loop
  22.  
I think the code I added takes into account the notes in the chord between the brackets, like the B and the C in this example:

<<A B C D>>

Let me know if I am wrong. I appreciate your help, and I do understand the logic. This code still does not seem to be working.
Mar 21 '07 #4
Denburt
1,356 Recognized Expert Top Contributor
OK Cleaned it up tried and tested example below I think you will find this satisfactory. Let me know if you find a combination I missed.

Expand|Select|Wrap|Line Numbers
  1. Set db = CurrentDb
  2. Set rst = db.OpenRecordset("Table1")
  3. cnt = 1
  4. If Not rst.EOF Then
  5. rst.MoveFirst
  6. Do Until rst.EOF
  7.  strChord = rst!Chord
  8.   strNote = rst!Notes
  9.  rst.MoveNext
  10. If Not rst.EOF Then
  11.     strChord1 = rst!Chord
  12. Else
  13.     strChord1 = ""
  14. End If
  15.  rst.MovePrevious
  16.     If strChord = strChord1 And cnt = 1 Then
  17.     sSql = "INSERT INTO Test (Notes) VALUES('<<" & strNote & "')"
  18.     db.Execute sSql
  19.     cnt = cnt + 1
  20.     ElseIf strChord <> strChord1 And cnt > 1 Then
  21.  
  22.      sSql = "INSERT INTO Test (Notes) VALUES(""" & strNote & ">>"")"
  23.     db.Execute sSql
  24.     cnt = 1
  25.    Else
  26.       sSql = "INSERT INTO Test (Notes) VALUES('" & strNote & "')"
  27.         Debug.Print sSql
  28.         db.Execute sSql
  29.     End If
  30.     rst.MoveNext
  31. Loop
  32. End If
  33.  
Mar 21 '07 #5
chopin
37 New Member
Well well, thank you very much, problem solved. I am surprised I was able to portray exactly what I needed done, and you solved this...the hardest part of my project.

Anyway, in case you are curious with what I am doing, I am trying to convert musicXML into lilypond format (lilypond is a free music typesetter which requires a special formatting, which then compiles the lilypond code into sheet music that is a PDF). Since I am a composer, converting musicXML into lilypond format would save me a tremendous amount of time rather than to manually do it by hand.

I will bump this thread if I have further questions, as I am working on this project currently, and so far, this was the biggest obstacle.

Again, thank you!
Mar 21 '07 #6
Denburt
1,356 Recognized Expert Top Contributor
My pleasure glad I could help :)
Mar 21 '07 #7

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

Similar topics

11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
495
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
4
10519
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
6
3431
by: Taxi Driver | last post by:
Hi Everyone - Can I get your help with this, it is driving me crazy. I have 2 arrays listed below: @req=75 @req=76 @req=77 @req=78 --- @bid=75 @bid=76
1
1996
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
19
3808
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
3
8651
by: riauvision | last post by:
Hello, I have problem in writing ms Access vba code to compare two array taken from two different recordset. One is from table: Detail Orders (PK:,POID,Product.Code,Qty) and the other is from table...
21
2308
by: Kristaps | last post by:
Hi everyone! I have some questions, maybe someone can help me... I write script for table comparing, but it works wery slovly. There is the script, can anyone give some tip how can I make this...
18
2865
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
0
7083
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
7278
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
7328
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...
0
7456
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...
0
5578
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,...
1
5011
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...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.