Solved

Access 2007 - VB Code checking for duplicates

Posted on 2011-02-20
8
792 Views
Last Modified: 2012-05-11
My OS is windows 7.  The code I have here was developed by the Gurus here on this Forum.  This is a small database.  And it extracts the database,concaternates into a string, so that I could copy and paste it into an email client.
However the database also contains the names of spouses who use the husband's or wive's email address, thus there will be duplicates in the present code.
For ease of reference, I think it is should be implemented from the Else line.
I would like the code, when it first reiterates for the first record, to add the name to the list, but after that from the 2nd record to the end of the file, to first check if this email is already in the List, if Yes, do not add it, if No, then add.

Those are my novice thoughts of how the program should run, but I am sure the Experts would be able to suggest a better version of it.  Thank you.
Private Sub mailemailreport_Click()
Dim rst As DAO.Recordset
Dim strDistList As String
Set rst = CurrentDb.OpenRecordset("Seniors Club")


Do While Not rst.EOF
    strDistList = strDistList & rst!Email & ";"
    rst.MoveNext
Loop
If MsgBox("Print Email distribution list to file?", vbYesNo, "Print?") = vbYes Then
  Dim lFile As Long
  '/get the next available File handle
  lFile = FreeFileHandle
  '/delete the file if it exists
  If Dir(CurrentProject.Path & "\EmailList.txt") <> "" Then Kill CurrentProject.Path & "\EmailList.txt"
  '/Now open the file
  Open CurrentProject.Path & "\EmailList.txt" For Output As #lFile
  '/write to that file
  Print #lFile, strDistList
  '/now close the file
  Close #lFile
Else
    strDistList = Left(strDistList, Len(strDistList) - 1)

    Me.Text46 = strDistList
End If

    

rst.Close
Set rst = Nothing

End Sub

Open in new window

0
Comment
Question by:jegajothy
  • 4
  • 4
8 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34937205
Personally i'd use a dictionary as the control mechanism ... try this change

Chris
Private Sub mailemailreport_Click()
Dim rst As DAO.Recordset
Dim strDistList As String
Dim dict As Object

    Set dict = CreateObject("scripting.dictionary")
    dict.comparemode = vbTextCompare
    Set rst = CurrentDb.OpenRecordset("Seniors Club")


Do While Not rst.EOF
    If Not dict.exists(rst!Email) Then
        dict.Add rst!Email, rst!Email
        strDistList = strDistList & rst!Email & ";"
    End If
    rst.MoveNext
Loop
If MsgBox("Print Email distribution list to file?", vbYesNo, "Print?") = vbYes Then
  Dim lFile As Long
  '/get the next available File handle
  lFile = FreeFileHandle
  '/delete the file if it exists
  If Dir(CurrentProject.Path & "\EmailList.txt") <> "" Then Kill CurrentProject.Path & "\EmailList.txt"
  '/Now open the file
  Open CurrentProject.Path & "\EmailList.txt" For Output As #lFile
  '/write to that file
  Print #lFile, strDistList
  '/now close the file
  Close #lFile
Else
    strDistList = Left(strDistList, Len(strDistList) - 1)

    Me.Text46 = strDistList
End If

    

rst.Close
Set rst = Nothing

End Sub

Open in new window

0
 

Author Comment

by:jegajothy
ID: 34937516
in response to chris, I tried your code, and it gave me one result, which was the wrong one.  a query run to find duplicates revealed  7 duplicates.  So I am not sure how the code ran.  How do I step thru the code.  in the design, I clicked on the step into, but after I closed it and then hit the command button, it just ran thru the entire code. thank u.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34937609
Obviously I don't have your database so I cannot test in situ but the dictionary mechanism removes any duplicates.  I am using the same rst!Email parameter that you used so I don't see how the datum can be wrong ... and if there is only the one result was there only the one unique datum in the data set?

Chris
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:jegajothy
ID: 34937718
in reply to Chris, there are more than 1 unique datum.  Out of a total of 112 records,only 7 records are duplicates.  
While I am no Guru, but looking at the following syntax,     dict.comparemode = vbTextCompare
vbTextCompare has not been assigned or declared anywhere.
Thank u.
0
 

Author Comment

by:jegajothy
ID: 34937724
Attached is the database for u to take a look.  Please use the Form, where the code is in the command button. Thank u. email-database-Feb-21.accdb
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34938049
The greater than and less than symbols were affecting the comparison

The following removes them on the assumption that is ok.

Chris
Private Sub btnDistList_Click()
Dim rst As DAO.Recordset
Dim strDistList As String
Dim dict As Object

    Set rst = CurrentDb.OpenRecordset("Seniors Club")
    Set dict = CreateObject("scripting.dictionary")
Stop
Do While Not rst.EOF
    If IsNull(rst!Email) Then
    Else
        If Not dict.exists(Replace(Replace(rst!Email, "<", ""), ">", "")) Then
            dict.Add Replace(Replace(rst!Email, "<", ""), ">", ""), Replace(Replace(rst!Email, "<", ""), ">", "")
            strDistList = strDistList & rst!Email & ";"
        End If
    End If
    rst.MoveNext
Loop

    strDistList = Left(strDistList, Len(strDistList) - 1)

    Me.txtDistList = strDistList

rst.Close
Set rst = Nothing
End Sub

Open in new window

0
 

Author Closing Comment

by:jegajothy
ID: 34938866
thank u this works.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34938974
Glad to help but I should also express my appreciation at your providing the sample database as this made understanding the issue easy.

Chris
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My experience with Windows 10 over a one year period and suggestions for smooth operation
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

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

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

Join & Ask a Question