loops

AccessWillKill

Registered User.
Local time
Today, 15:53
Joined
Oct 2, 2010
Messages
50
is it possible to run 2 for loops alongside each other

for example can you set one loop to loop through one array while another array is doing the same then compare the two

like if array1val = arrayval2 then
message box array is equal
else
message box array is not equal
 
yes it is. you can do it for as many times as there are different loops syntaxes. for example, you can do this:
Code:
do until x = y

     while x <> y

          for z = 1 to 10

               'do something

          
          next z

     wend

loop
you cannot do this:
Code:
do until x = y

     do

          'do something

     loop until z = 10

loop
two of same types of loops cannot be nested.
 
two of same types of loops cannot be nested.

I don't think that's accurate. You can definitely nest For/Next loops, and I just tested nesting 2 Do While Not rs.EOF loops:

Code:
    Do While Not rs.EOF
        Do While Not rs1.EOF
            Debug.Print rs!Version & ":" & rs1!Version
            rs1.MoveNext
        Loop
        rs1.MoveFirst
        rs.MoveNext
    Loop
 
thanks for correcting that Paul. Indeed I haven't tested it, so it was my mistake to assume they all operated the same.
 
I don't think that's accurate. You can definitely nest For/Next loops, and I just tested nesting 2 Do While Not rs.EOF loops:

i've been trying to nest 2 For/Next loops however i've had a number of problems with it, the main one now is that one loop gets cycled through once and then ignored and the other works fine (which mean when the loop compares the two values it keeps comparing one value with a different value rather than comparing

array1value1 with array2value1,
array1value2 with array2value2,

don't mean to sound like an idiot, I haven't touched access for six years now i'm employed to do it i remember little of what I learned before
 
pbaldy gave you the syntax:
Code:
dim item1 as Variant, item2 as variant

for each item1 in array1
     for each item2 in array2
           if item1 = item2 then
                ' do something here
                exit for   ' exit inner loop so looping can continue to next item1 value
           end if
     next
next
 
but i presume the last example isnt what you want either

i presume you have two arrays,

some items in array 1 may not be in array 2
some items in array 2 may not be in array 1

so i presume you are trying to step through both in sequence, examining and comparing both for equality/inequality
 
yeah. basically the form needs to make sure that they're both running in sequence and checking to see if one field is the same as the other. However only one loop runs through. Would it matter what they are declared as?

i currently have them set to variants.


One loop runs fine but the second doesn't run after the first loop when it gets to the Next it just skips over
 
Code:
Y = UBound(array1val())
If Y > UBound(array2val()) Then Y = UBound(array2val())

For X = 0 To Y - 1
    If array1val(X) = array2val(X) Then
        MsgBox "box array is equal"
    Else
        MsgBox "box array is not equal"
    End If
Next X

:confused:
 
Sadly that didn't work. well it did work just not what i wanted it to do. it is comparing the two

this only collects the last value in the first array and compares it to the index value of the second IE it will compare say

String19 (last value in the array) with 0
String19 (last value in the array) with 1
String19 (last value in the array) with 2
etc

array 1 is the new set of (global)variables set on a new form with any potentially edited fields
array2 is the old set of (global) that stores original values
 
So you want to check all the items in array1 against all the items in array 2 regardless of the order that the items are in and it should pop a message saying whether they are the same? Also, regardless of case as well?
 
not exactly i want item 1 in array one to match to item 1 in array 2
then item two in array 1 to match item 2 in array 2 and so on until the arrays have finished (both arrays have same number of values)
 
That's what ChrisO's code does, but you would need to change the For line to this:

For X = 0 To Y

(without the -1)
 
Just jumping into this thread, in realistic terms you are populating an array with a known set of values. Then making changes to any one of the elements that made up that array. Then you save the new changes to another array then you want to compare the values to see if any element in the second array has changed from the first array.

Eg

Array 1

0 = A
1 = B
2 = C
3 = D
4 = E

Array 2
0 = A
1 = A
2 = C
3 = D
4 = D

Result
elements 0, 2 & 3 are unchanged
elements 1 & 4 have changed

? correct?
 
i got chriso's code to work however it only runs through once, rather than the amount of times needed. even without the -1 it does the same thing

code as follows
Code:
field = UBound(FieldArray())
If field > UBound(editedField()) Then editedvalue = UBound(editedField())
For field = 0 To editedvalue
    If FieldArray(field) = editedField(editedvalue) Then
        MsgBox ("box array is equal")
    Else
        MsgBox ("box array is not equal")
    End If
Next field
 
just to go back on this

how are you getting two arrays.

if you know the two arrays have the same set of indexvalues, then it isnt a problem at all


for x=0 to sizeofarray
if array1(x)=arraay2(x) then
whatever
else
whatever
end if
next

it only becomes an issue if the index values are different in thev two arrays
 

Users who are viewing this thread

Back
Top Bottom