Beginner problem - testing for vacant fields

Hi.I’m new to Domino Designer so the following problem may be elementary.

I’ve written some LotusScript code to count the number of documents in a view and then extract the data from a couple of fields from a randomly selected document.

The code seems to be counting the docs okay, and walking the view to pick up field info from the random document.

But I’ve put in an If statement to try to get the program to look for another random document if one or both of the two fields on the random doc are empty.

This doesn’t seem to work - the code is still sometimes returning empty fields as its results.

Here is the code:

Sub Click(Source As Button)

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim s As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim docTotal As Long

Dim docNo As Long

Dim randDocNo As Long

Dim randChemForm As String

Dim randCASNo As String

Dim randProdName As String



Set db = s.CurrentDatabase

Set view = db.getView("PN")

REM Count number of documents in View

docTotal = view.EntryCount

REM Display result of document count

Msgbox "Total number of documents = " & docTotal, , "Document Count"

REM Generate random number between 1 and the total number of docs

randDocNo = ((Rnd()*(docTotal-1))+1)

REM Display random document number

Msgbox "Number of randomly selected document = " & randDocNo, , "Random Document Number"

REM Walk through view to find document

Set doc = view.GetFirstDocument



docNo = 1

randChemForm = ""

randCASNo = ""



Do While docNo<>randDocNo

	randChemForm = doc.GetItemValue("Prod_ChemForm")(0)

	randCASNo = doc.GetItemValue("Prod_CASNo")(0)

	If docNo = randDocNo Then

		If randChemForm = "" Or randCASNo = "" Then

			randDocNo = ((Rnd()*(docTotal-1))+1)

			Msgbox "Number of randomly selected document = " & randDocNo, , "Random Document Number"

			docNo = 0

			Set doc = view.getFirstDocument

		End If

	End If

	docNo = docNo + 1

	Set doc = view.GetNextDocument(doc)

Loop



Set uidoc = ws.CurrentDocument

Set doc = uidoc.Document



doc.Rand_ChemForm = randChemForm

doc.Rand_CASNo = randCASNo

End Sub

Any ideas? Thanks.

Subject: Beginner problem - testing for vacant fields

The problem is the logic in you code, because after having checked, if one of the items is found to be empty, you are going to select another document and if then docNo = randDocNo the do while-loop will be left without further testing.

So I think the condition

Do While docNo<>randDocNo

is not what you really need. Try something like this: Do While itemsEmpty where itemsEmpty has to be set according to the items’ contents once before entering the loop and always in the loop when testing the items.

Subject: RE: Beginner problem - testing for vacant fields

Boerries,Many thanks for your advice - I will try what you suggest.

Subject: Beginner problem - testing for vacant fields

Instead of counting through the view with GetNextDocument() you could try View.GetNthDocument(). Your loop could then be simplified …

Do

randDocNo = ((RND()*(docTotal-1))+1)

set doc = view.getNthDocument( randDocNo)

randChemForm = doc.GetItemValue(“Prod_ChemForm”)(0)

randCASNo = doc.GetItemValue(“Prod_CASNo”)(0)

Loop until ( randChemForm <> “” ) and ( randCasNo <> “” )

Are you sure that there is always a document in the view that will have non-blank fields? If not, you will need to try a different approach with more error checking.

Subject: RE: Beginner problem - testing for vacant fields

Thanks a lot, Christopher.That works well.