Solved

Access 2007 - VB Code checking for duplicates

Posted on 2011-02-20
8
793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

751 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