avoiding duplicates in string problem

MvP14

Registered User.
Local time
Today, 04:59
Joined
Apr 15, 2003
Messages
66
I have no education in VBA. I developped some notions of it, though. Usually, I can solve the problems in my database with some copy and paste from this excellent forum.

However, this time I don't manage.

Here's what I'm trying to do: I have a form linked to a table that contains groups of emailadresses.

How do I create those groups? I have a listbox on my form based on a query that selects all the entries with emailadresses from a table with people's personal data.

It is possible to highlight various persons in the listbox, and select a button that creates a string with all the emailaddresses from those persons, and adds the string to a memo field in the form with the groups of emailaddresses.

This worked just fine, until the following problem came up: sometimes to people have the same emailaddress. I want to avoid having the same emailaddress twice in the string.

How did I try to solve that? I created a table with only one field: EmailAddress and defined that field as the primary key, no duplicates. I figured, if I could add the emailaddresses of the highlighted persons first to that table, and then build the string from that table, I would avoid having duplicates in the string.

Since I use the same table all the time, I first have to empty it.

The problem is I just don't get the code to work. Since I'm the summum of an amateur, I figured I'd post it here, with two questions:

1. Is it even worth continuing to construct the code, or should I try a whole different path?
2. If 1 is answered positively, what (all) is wrong in the code?
If 1 is answered negatively, any suggestions on how to do it?


This is the code I have so far:

Private Sub Command8_Click()
Dim f As Form
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim intCounter As Integer
Dim i As Integer
Dim i2 As Integer
Dim strlist As String
Dim strDuplicate As String
Dim strEmailAddresses As String

Set f = Forms!MultiEmailForm
Set db = CurrentDb()
Set rst = db.OpenRecordset("TempEmailListTable", dbOpenDynaset)

'Clearing the table
For i = 0 To rst.RecordCount Step 1
rst.Delete
rst.MoveNext
rst.Update
Next i

'Filling the table with the emailaddresses from the highlighted persons
For intCounter = 0 To Me.List0.ListCount Step 1
If Me.List0.Selected(intCounter) = True Then
strlist = Me.List0.Column(3, intCounter)
With rst
.AddNew
![EmailAddress] = strlist
.Update
End With
End If
Next intCounter

'Building the string
For i2 = 0 To rst.RecordCount Step 1
If strDuplicate = rst![EmailAddress] Then
rst.MoveNext
Else
strEmailAddresses = strEmailAddresses & rst![EmailAddress] & ";"
strDuplicate = rst![EmailAddress]
rst.MoveNext
End If
Next i2

rst.Close

f.Listgroupstring = strEmailAddresses

End Sub
 
Sounds more like an issue of having your listbox populated with unique addresses only, rather than trying to remove after the fact.
 
Wish I could

Thanks for the reply and the suggestion. However, it will not do for me. The reason is that some people have the same emailaddress, but I don't always know which people. This is also why the listbox has the names of the people, and not their emailaddresses.

Any other suggestions?
 
Is this really unsolvable?

I still haven't figured this one out. Is it really not possible?
 
Hi

not sure where strDuplicate is getting it's value from...

can you not use the logic:

if Instr(strEmailAddresses, rst![EmailAddress]) >0 then
' email address already in list so move to next record

look up the Instr function if you're not familar with it - it checks to see if a string already exists in another string and at what point it occurs.

hth
steve
 
Thank you!

Thank you for the suggestion. It works just fine now. And it is about ten times as simple as what I had been messing around with!!

Sometimes one just has to know the right functions...


Here's the code:

Dim f As Form
Dim intCounter As Integer
Dim strlist As String

Set f = Forms!MultiEmailForm

For intCounter = 0 To Me.List0.ListCount Step 1
If Me.List0.Selected(intCounter) = True Then
If InStr(strlist, Me.List0.Column(3, intCounter)) = 0 Then
strlist = strlist & Me.List0.Column(3, intCounter) & ";"
End If
End If
Next intCounter

f.Listgroupstring = strlist
 

Users who are viewing this thread

Back
Top Bottom