Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a document in a While loop.

Status
Not open for further replies.

kennethl

Programmer
Apr 8, 2004
4
GB
Hi,

I am writing an agent to archive old documents in a database. Everything is working fine until I try to delete the document that has been processed. I know its to do with where I have the line of code "Call doc.Remove( True )" placed because the agent is working as it should (without deleting the doc of course) when I don't have this line "Call doc.Remove (True)" in the script.

Basically what I want the script to do is:

1) Get first doc in view
2) If AStatus field is "Live", move subforms to archive db
3) Move main form to archive db
4) delete the doc from the db
5) get next doc in view and then back to 2)

Here a shortened version of the code:

' get the first document
Set doc = v.GetFirstDocument

Do While Not (doc Is Nothing)

' get the status of the reord
Select Case doc.AStatus(0)

' if astatus live
Case "Live"

/////////////// archive document code ////////////////////

End Select


Call doc.CopyToDatabase(archivedb)

****************here I want to remove the document that is after been archived but I am getting an error *************
Call doc.Remove( True )

Set doc = v.GetNextDocument (doc)
Loop

Any help would be greatly appreciated.

Kind regards,
Kenneth
 
Hey,

I would look at the code in the default archiving agent that ships with the project. Maybe it would help? Just an idea.

Rgds,

John
 
In my opinion, it is your GetNextDoc that is making the error.

Think about it for a second : you copy a document, then delete it from the current database.
When that happens, the handle to the deleted document is rendered invalid.
So you cannot GetNext, since there is no more First.

What you need to do is change your code to get the first document in the view every time.

Hint : this only works if you are sure you want to delete EVERY document in the view. If not, you must create a collection first, then cycle through the collection in the same way.

Pascal.
 
I think he should not get the first document in the view every time. Because i think he doesn't want to delete all documents....that could be easier :)

You should declare a delete-document. If you want to delete a doc, just set the delete-document on this doc too.

Now go to the next Document (with your doc, not with the delete-document). And now you can remove the delete-document.

Greetings...

LuckyMan

-----------------------------------------------------------

95 Server with Win NT 4 SP6
10000 Clients with Notes 5.0.2c
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top