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

x
?
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
Medium Priority
?
369 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 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

963 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