Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access 2007 - VB Code checking for duplicates

Posted on 2011-02-20
8
Medium Priority
?
796 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

877 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