Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested For Loop Help

Status
Not open for further replies.

CrimsonDiva

Programmer
Sep 22, 2000
30
0
0
US
Hi,

I'm trying to iterate through two arrays. The 1st array contains values from the database. The 2nd array contains values from a form. I want to hold the 1st array constant while checking values from the 2nd array against it. If the item in the 2nd array is not found in the 1st array, then i want to add a record to the database. I have run into several problems trying to execute this. Here is some of my code:

'comments
'1st array = dbArrary(usercount)
'2nd array = newPrefArray(chkcount)

For i = 1 to chkcount
For j = 1 to usercount
If newPrefArray(i) = dbArray(j) Then countmatch = true
If countmatch = false Then
'add record
End If <== this IF statement should only execute ONCE during this inner FOR loop
Next
Next
 
If I understand what you're asking for, you want to jump out of that inner loop if you get a match and not keep hitting the If...End If every time the match is false. If so then just change:
Code:
  If newPrefArray(i) = dbArray(j) Then countmatch = true
  If countmatch = false Then
    'add record  
  End If
into
Code:
  If newPrefArray(i) = dbArray(j) Then
    countmatch = true
  Else
    'add record
    Exit For
  End If
 
Thanks, Genimuse. That almost worked. I think I may have solved my own problem with the following:


For i = 1 to chkcount
For j = 1 to usercount
If newPrefArray(i) = dbArray(j) Then countmatch = countmatch + 1
Next
if countmatch = 0 Then 'add record
countmatch = 0 <== reset the count for each item in 2nd array
Next


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top