How to download unique data into ms access table

Voyager

Registered User.
Local time
Tomorrow, 04:26
Joined
Sep 7, 2017
Messages
95
I am using this code to download certain outlook mail fields into access. This works well however the code is keep on downloading duplicate mails. Is there a way to check for existing records and download records which are not in the table? Your answers would help a lot in my project

Private Sub getml()
Dim rst As DAO.Recordset
Dim OlApp As Outlook.Application

Dim inbox As Outlook.MAPIFolder
Dim inboxItems As Outlook.Items
Dim Mailobject As Object
Dim db As DAO.Database
Dim dealer As Integer
Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")
Set inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set rst= CurrentDb.OpenRecordset("mls")
Set inboxItems = inbox.Items
For Each Mailobject In inboxItems

With rst
.AddNew
!task= Mailobject.UserProperties.Find("taskID")
!tsktml= Mailobject.UserProperties.Find("timeline")
.Update

Mailobject.UnRead = False
End With
End If
Next
Set OlApp = Nothing
Set inbox = Nothing
Set inboxItems = Nothing
Set Mailobject = Nothi
End Sub
 
Create primary key to I'd+timeline.
 
I tried assigning a PK for the fields once I do that update queries in the db throws key violation error. Hence I need a vba approach to solve my issue
 
you need to delete the duplicate records first
when you need to add pk.

if you are not able to create one, you must
search the table for "taskID" and "timeline" if
it already exists on the table. this can be done
before you issue .AddNew command on the recordset:

dim varID as Variant
dim varTimeLine as Variant
With rst

varID = Mailobject.UserProperties.Find("taskID")
varTimeLine=Mailobject.UserProperties.Find("TimeLine")

.FindFirst "[Task] = '" & varID "' And [tsktml]='" & varTimeLine & "'"

If .Nomatch
.AddNew
!Task=varID
!tsktml=varTimeLine
.Update
End If

End With
 
FYI, I moved your thread out of the introductions forum. I do a number of automated imports. What I do is move the email to a subfolder so it doesn't get handled twice.
 
The taskid is a field which contains both alpha numeric characters separated by a hyphen e.g. "Task-1234" After applying new code I am unable to download mails is there any suggestion from your end?
 

Users who are viewing this thread

Back
Top Bottom