Populate ListBox from Text File - Without Repeats

Populate ListBox from Text File - Without Repeats

Anonymous
Not applicable
462 Views
1 Reply
Message 1 of 2

Populate ListBox from Text File - Without Repeats

Anonymous
Not applicable
I've seen many examples of populating a listbox from a text file, but my text file will have many repeats of the same items, and I'd like to weed these out, and just have a list of available items, with no repeats.

I'm presently working on the code, so I don't have anything to post yet. My present idea is to start an array with the first item from the text file, then compare the next item to the array entry, and if it does not match, then add it to the array. As the array grows, the text item will be compared to each item in the array, discarding any matches and adding any new items.

I'm sure I'll figure it out, but if anyone has something that's already done, I'd appreciate handouts. I'll post whatever I come up with if there are no responses.

Thanks.
0 Likes
463 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
I got it. Here's the code if anyone's interested. The first item in the array is left blank, so at the end, I check for a blank entry before populating the list. I use a listbox and a command button on my test form.




Private Sub Command1_Click()

Dim FN As Long

Dim Lister() As String

Dim Inc As Integer

Dim booMatch As Boolean



FN = FreeFile

Open "Textfile.txt" For Input As #FN

Inc = 0

ReDim Preserve Lister(0)

Do While Not EOF(FN)

Line Input #FN, linestring

booMatch = False

For lp = 0 To UBound(Lister)

If Lister(lp) = linestring Then booMatch = True

Next lp

If booMatch = False Then

Inc = Inc + 1

ReDim Preserve Lister(Inc)

Lister(Inc) = linestring

End If

Loop

Close FN

With List1

.Clear

For lp = 0 To UBound(Lister)

If Lister(lp) <> "" Then .AddItem Lister(lp)

Next lp

.ListIndex = 0

End With

End Sub

0 Likes