Document Collection - Final Post

Thank you ALL for your help and input! I have just one final piece. I am getting all of the documents created, however am then getting lost in a continuous loop. What is missing to have the agent stop when it has reached the end of the result set? Here is the final code:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim workspace As NotesUIWorkspace

Dim codeview As NotesView	



Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet



Dim Mnemonic As String



Dim doc As NotesDocument

Dim collect As NotesDocumentCollection



Set workspace = New NotesUIWorkspace

Set db = Session.CurrentDatabase

Set collect = db.AllDocuments



Call con.ConnectTo("AIS Warehouse")



Set qry.Connection = con

Set result.Query = qry



Set doc = collect.GetFirstDocument( )



While Not doc Is Nothing

	

	qry.SQL = "select rtrim(code_value) as Mnemonic, description as AccessCode, " + _

	"rtrim(case " + _ 

	"when description like '__00000000000' then substring(description, 1, 2) + '%' " + _

	"when description like '____000000000' then substring(description, 1, 4) + '%' " + _

	"when description like '_____00000000' then substring(description, 1, 5) + '%' " + _

	"when description like '______0000000' then substring(description, 1, 6) + '%' " + _

	"when description like '_________0000' then substring(description, 1, 9) + '%' " + _

	"when description like '__________000' then substring(description, 1, 10) + '%' " + _

	"else description " + _

	"end) as AccessFilter, " + _

	"rtrim(cast(text1 as char(30))) as MDesc, " + _

	"0 as MLevel " + _

	"into #t " + _

	"from caccess1 " + _

	"where getdate() between begin_date and end_date " + _

	"SELECT c.Mnemonic, c.AccessCode, c.AccessFilter, c.MDesc, " + _

	"(SELECT COUNT(*) FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter) As MLevel, " + _

	"(SELECT TOP 1 p.Mnemonic FROM #t p WHERE c.AccessCode <> p.AccessCode and c.AccessCode like p.AccessFilter ORDER BY p.AccessCode DESC) As Parent, " + _

	"CASE WHEN Exists (SELECT * FROM #t p WHERE c.AccessCode <> p.AccessCode and p.AccessCode like c.AccessFilter) THEN '1' ELSE '0' END As hasChildren INTO #t2 FROM " + _

	"#t c " + _

	"SELECT SPACE(MLevel * 2) + MDesc + '(' + Mnemonic + ')' As Mnemonic FROM #t2 ORDER BY AccessCode " 

	

	Set result.Query = qry

	Call result.Execute

	

	If result.IsResultSetAvailable Then

		Do Until result.IsEndofData

			Set doc = db.CreateDocument

			doc.form = "Codes"

			doc.Mnemonic = result.GetValue("Mnemonic")

			Call doc.Save(True,False)

			'Set doc = collect.GetNextDocument(doc)

			result.NextRow

		Loop

	Else

	End If

	

Wend

result.Close(DB_Close)

con.Disconnect

End Sub

Subject: Document Collection - Final Post

Your problem is the While statement

While Not doc Is Nothing

You never actually iterate the doc in the document collection, but rather create a new doc in the result loop. So doc will never be nothing, since you are creating a new one. I guess I don’t understand why the while statement is in there since you never use any of the values from the document and overwrite doc var with the creation of a new document. What is this loop trying to accomplish?

Subject: Document Collection - Final Post

Use this:

Loop Until result.IsEndofData

instead of just Loop

Subject: RE: Document Collection - Final Post

Thank you, but that didn’t change anything. It still just keeps going through the loop. As long as I stop the agent after it runs a bit I get my list. I’ll keep plugging away. Thanks!

Subject: RE: Document Collection - Final Post

I think you are assuming you’re not getting out of your inner loop, but it’s really the outer loop that causes the problem, because you repeat based on doc and you’re never changing its value.

Subject: Document Collection - Final Post

'Set doc = collect.GetNextDocument(doc)

This statement has a REM mark, remove it.