Solved

Using a For/Next loop to scan email inbox, every other record is skipped if the delete option is set

Posted on 2007-12-05
3
360 Views
Last Modified: 2013-12-05
First of all, apologies for the cryptic title! I couldn't really think of a way to put it concisely!

So, I have an Access ADP project (which many of you will feel you know intimately now!!) and one of its functions is to scan an email inbox looking for email from certain recipients, it then calls a stored procedure that inserts the data it finds into the SQL 2000 database.

This procedure works very well but I have the option on the form that calls the routine to delete the email when it has been imported into the database. When this is checked the routine only scans 50% of the emails (see the code snippet to see the line that does this) so I can kind of see what it is doing (when it looks for "next itm" the last one has been deleted so it gets confused I guess) but haven't a clue how to resolve it.

The function that does it is very long but the relevant bits (I guess) are: (I have omitted declarations etc)

Has anyone else come across a similar problem or any suggestions as to the solution?

Thanks.
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
 
If SelectFolder = True Then
    Set fld = nms.PickFolder
end if
 
lngItemCount = fld.Items.Count
 
For Each itm In fld.Items
    If itm.Class = 43 Then '43 is the constant olMail
            Set Safeitm = CreateObject("Redemption.SafeMailItem")
            Safeitm.Item = itm
                For Each att In Safeitm.Attachments
                    att.SaveAsFile strFilename
                
                    Set stm = New ADODB.Stream
                    With stm
                        .Type = adTypeBinary
                        .Open
                        .LoadFromFile strFilename
                    End With
                Next att
 
'THE BIT THAT OPENS THE WORD ATTACHMENT, EXTRACTS THE DATA AND CALLS THE STORED PROCEDURE
 
    end if
If Me.chkDeleteEmail True Then Safeitm.Delete
Set Safeitm = Nothing
next itm

Open in new window

0
Comment
Question by:pauldonson
3 Comments
 
LVL 38

Expert Comment

by:Jim P.
ID: 20413462
I ran into something similar when dealing with deleting from recordsets in the past.

Essentially the pointer gets "lost" when you delete the current record.

Maybe if you do something like build a recordset of the ones you imported and then go back and do a delete as a separate loop.

That was the way I ended up solving the problem.

0
 
LVL 6

Accepted Solution

by:
CRJ2000 earned 500 total points
ID: 20503647
You need to loop through the items backwards...

lngItemCount = fld.Items.Count
 
For count = lngItemCount to 1 step -1
  set itm = fld.items(Count)
  ... add your code here...
next

Another option, if you are going to delete all of the items in the folder, then you could simply look at the first item each time... this would only work properly if you are going to delete every item.

For count = 1 to lngItemCount
  set itm = fld.items(1)
  ... add your code here...
  ...Delete The Item Here...
next
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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

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