Improve company productivity with a Business Account.Sign Up

x
Solved

# Array Checking

Posted on 2002-05-09
Medium Priority
200 Views
Last Modified: 2010-05-02
I have two separate arrays. One is called strAddArray and the other is called strRemoveArray. What I am trying to do is select an item from a listbox. and add or remove it into another listbox. Evertime I add it it goes into strAddArray and evertyime I remove an item it goes into strRemoveArray.  However, if I add and remove the same several times I create multiple copies of the same item within both arrays. Before I can update the DB I need to have the arrays setup correctly.  Below is what I have thus far. The problem that I am running into is if I have a duplicate I remove it completely and have no copy of it all together.  This is not as easy as it looks for me because there are so many scenarios, and I do not want anything to slip through the cracks.

Thanks,
Roger.
====================================

'Checks items added

If intRemove > 0 Then
For x = 0 To 500
For y = (x + 1) To 500
If strRemoveArray(x) = strRemoveArray(y) And strRemoveArray(x) <> "" Then
strRemoveArray(y) = Empty
intRemove = intRemove - 1
End If
Next y
Next x
End If

'Checks items removed

If intAdd > 0 Then
For x1 = 0 To 500
For y1 = (x1 + 1) To 500
If strAddArray(x1) = strAddArray(y1) And strAddArray(x1) <> "" Then
strAddArray(x1) = Empty
intAdd = intAdd - 1
End If
Next y1
Next x1
End If

'Compares the two above arrays for any lingering duplicates

If intAdd > 0 And intRemove > 0 Then
For x2 = 0 To 500
For y2 = 0 To 500
If strAddArray(x2) = strRemoveArray(y2) And strAddArray(x2) <> "" Then
strAddArray(x2) = Empty
intAdd = intAdd - 1
If strRemoveArray(y2) = strAddArray(x2) And strRemoveArray(y2) <> "" Then
strRemoveArray(y2) = Empty
intRemove = intRemove - 1
End If
End If
Next y2
Next x2
End If
0
Question by:RogerH1
8 Comments

LVL 75

Accepted Solution

Anthony Perkins earned 300 total points
ID: 6999378
As an alternative you may want to use Collections instead of arrays.  They have built in methods for adding and removing.

Just a thought,
Anthony
0

Author Comment

ID: 6999388
I am not familiar with what you mean by "Collections". Would you explain to me please.

0

LVL 75

Expert Comment

ID: 6999530
Please maintain your open questions:
Rollback will not work Date: 02/07/2002 12:17PM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20264417
Table locking problems Date: 02/11/2002 10:57AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20265663
Lock out a user Date: 02/26/2002 10:03AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20271250
Window placement when minimized Date: 03/07/2002 07:17AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20274441
Unable to pull information properly into report Date: 02/22/2002 02:52PM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20270066
Unable to reprint the first page Date: 03/08/2002 07:21AM PST
http://www.experts-exchange.com/jsp/qShow.jsp?ta=crystal&qid=20274932

Thanks,
Anthony
0

Expert Comment

ID: 7000165
you could just use binary trees instead of arrays, then when you stuff duplicates you can just ignore them and each tree will maintain exactly one of each of the things you put in it, so when you unwind it into the db, you have no duplicates
0

LVL 2

Expert Comment

ID: 7000381
Why have arrays at all? The list boxes are working like arrays already.

After you are ready to update your database:

For ndx = 0 to addlist.listcount-1
' do add processing for addlist.list(ndx)
next

for ndx = 0 to removelist.listcount-1
' do remove processing for removelist.list(ndx)
next
0

Author Comment

ID: 7001288
These are all great ideas, but please understand something that I did not explain. That is, when I open this window it reads in from the DB and then the user can add and remove from their initial choices.

Unless I have made this more complicated than it needs to be, I can ONLY see it being done with some sort of array.  Since yesterday I have started working on understanding collections and worked up some code that is absolutely great. It checks current collection for duplicates and if they exists it never gets entered.  It is far less code than using an array, and seems to be easier to work with for what I am doing.

Now on the other hand, if someone feels that I am still making this harder than it needs to be please let me know.  I will leave this open until Monday at which time I will go ahead and close it if no one has a better idea.

Thanks,
Roger.
0

Author Comment

ID: 7006019
I looked into using the Collections class. It took me a while to find something good on the subject, but once I did I tried it in a sample program that I created. I feel that it works much easier than using arrays, and in less the code.

I wanted to give you an "A", but I your answer was only a direction to try, and no explanation or sample code came with it.  However, thank you for telling me about it. I learned something new.

Below is the sample program that I put together in case there are others that are not familiar with it like I was. It is nothing fancy, just enough to get a basic feel for how it works.

The Add button adds item typed in into a listbox, but also updates the Collections. The Show button displays what is in the Collections itself. Highlighting one of the items in the listbox showing the Collections will remove it from the Collections.

Roger.

===========================================
Private Sub cmdClear_Click()
lstShow.Clear
lstList.Clear
txtItem.Text = ""
Label1.Caption = ""
num = 0
Debug.Print test.Count
For x = 1 To test.Count
test.Remove 1
Next x
cmdShow.Enabled = False
cmdClear.Enabled = False
txtItem.SetFocus
End Sub

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdRemove_Click()
Dim x As Integer
Dim y As Integer
Dim str As String

str = lstShow.Text
test.Add " "
For x = 1 To test.Count - 1
'   Debug.Print test.Item(x)
If test.Item(x) = str Then
test.Remove (x)
End If
lstShow.Clear
Next x
End Sub

Private Sub cmdShow_Click()
For x = 1 To test.Count
lstShow.AddItem test.Item(x)
Next x
End Sub

Private Sub cmdAdd_Click()
Dim str As String

str = Trim(txtItem.Text)

' Do not add a blank field
If str = "" Then
txtItem.Text = ""
txtItem.SetFocus
Exit Sub
End If
num = num + 1

'Checks for duplicates within the collection and does not add it
For x = 1 To test.Count
If test.Item(x) = str Then
MsgBox "Match"
'      num = num - 1
txtItem.Text = ""
txtItem.SetFocus
Exit Sub
End If
Next x
lstList.AddItem txtItem.Text
Label1.Caption = num
test.Add str
txtItem.Text = ""
txtItem.SetFocus
cmdShow.Enabled = True
cmdClear.Enabled = True

End Sub
0

LVL 75

Expert Comment

ID: 7009896
>> I wanted to give you an "A", but I your answer was only a direction to try, and no explanation or sample
code came with it.  However, thank you for telling me about it. I learned something new.<<

The reason I did not elaborate beyond stating that it could be resolved with Collections was because I have no interest in suggesting solutions to questioners that do not abide by the EE Guidelines (http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp) and leave questions open indefinitely.

In any case, thank-you for the points and for the opportunity of explaining my position.

Anthony
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Join & Write a Comment Already a member? Login.

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll

#### 595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.