Check for empty answer fields - amend script agent

The below agent is in a db which I have taken over which I want to tweak. I have basic script knowledge and I’m not too sure how to change it.

The code is in querysave and it checks to see what fields\questions on a form a user hasn’t filled in and then populates a field with the number of fields left empty i.e. “Unaswered questions: 22”

I wanted to build on this and display what fields have been left empty i.e. instead of:“Number of unaswered: 22”, I want “Question 1, Question 2 unaswered”.

Any ideas?Thanks,

Caroline

=======================================

Sub Querysave(Source As Notesuidocument, Continue As Variant)

%REM

This script checks for empty answer fields and ask the user whether he/she wants to continue with saving the document

or that he/she wants to go back to the document.

%END REM

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument     

Dim item As NotesItem



Set uidoc = ws.CurrentDocument

Set doc = uidoc.document



items = doc.items

Dim T_Number As Integer

Dim Y_Number As Integer

Dim NA_Number As Integer



count = 0

Forall i In items

	If Instr(i.name, "Q_") Then          

		If i.values(0) = "No" Then

			count = count + 1

		End If

	End If     

	T_Number = count     

End Forall



count = 0

Forall i In items

	If Instr(i.name, "Q_") Then          

		If i.values(0) = "Yes" Then

			count = count + 1

		End If  

	End If     

	Y_Number = count     

End Forall



count = 0

Forall i In items

	If Instr(i.name, "Q_") Then          

		If i.values(0) = "N/A" Then

			count = count + 1

		End If       

	End If     

	NA_Number = count     

End Forall



''

count = 0

Forall i In items

	If Instr(i.name, "Q_") Then          

		If i.values(0) = "" Then

			count = count + 1

		End If     

	End If     

	Blank_Number = count     

End Forall

''



Call uidoc.FieldSetText("TotalNoQR", Cstr(T_Number))

Call uidoc.FieldSetText("TotalYesQR", Cstr(Y_Number))

Call uidoc.FieldSetText("TotalNAQR", Cstr(NA_Number))

Call uidoc.FieldSetText("TotalBlank", Cstr(Blank_Number))

End Sub

Subject: Check for empty answer fields - amend script agent

Try the following updated code and check out the dispNames… variables for the information you wish to return.

Hope this helps!

Sub Querysave(Source As Notesuidocument, Continue As Variant)

%REM

This script checks for empty answer fields and ask the user whether he/she wants to continue with saving the document

or that he/she wants to go back to the document.

%END REM

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument 

Dim item As NotesItem



Set uidoc = ws.CurrentDocument

Set doc = uidoc.document



items = doc.items

Dim T_Number As Integer

Dim Y_Number As Integer

Dim NA_Number As Integer

Dim Blank_Number As Integer



Dim dispFieldNamesNo As String, dispFieldNamesYes As String, dispFieldNamesNA As String, dispFieldNamesBlank As String



T_Number = 0

Y_Number = 0

NA_Number = 0

Blank_Number = 0



'Rather than loop through all of the items 4 times, loop once and then use the Select case statement to build your strings

Forall i In items

	If Instr(i.name, "Q_") Then 

		Select Case i.values(0)

		Case "No":

			T_Number = T_Number + 1

			'Check to see if your string variable is empty.  This is only done so that the comma will show up in the right places

			If dispFieldNamesNo = "" Then

				dispFieldNamesNo = "Question " & Right(i.name, Len(i.name -2))

			Else

				dispFieldNamesNo = dispFieldNamesNo & ", Question " & Right(i.name, Len(i.name - 2))

			End If

		Case "Yes":

			Y_Number = Y_Number + 1

			'Check to see if your string variable is empty.  This is only done so that the comma will show up in the right places

			If dispFieldNamesYes = "" Then

				dispFieldNamesYes = "Question " & Right(i.name, Len(i.name -2))

			Else

				dispFieldNamesYes = dispFieldNamesYes & ", Question " & Right(i.name, Len(i.name - 2))

			End If	

		Case "N/A":

			NA_Number = NA_Number + 1

			'Check to see if your string variable is empty.  This is only done so that the comma will show up in the right places

			If dispFieldNamesNA = "" Then

				dispFieldNamesNA = "Question " & Right(i.name, Len(i.name -2))

			Else

				dispFieldNamesYes = dispFieldNamesYes & ", Question " & Right(i.name, Len(i.name - 2))

			End If		

		Case ""

			Blank_Number = Blank_Number + 1

			'Check to see if your string variable is empty.  This is only done so that the comma will show up in the right places

			If dispFieldNamesBlank = "" Then

				dispFieldNamesBlank = "Question " & Right(i.name, Len(i.name -2))

			Else

				dispFieldNamesBlank = dispFieldNamesBlank & ", Question " & Right(i.name, Len(i.name - 2))

			End If	

		End Select

		

	End If 

	

End Forall



'At this point you should have up to 4 formatted strings (dispFieldNamesYes, dispFieldNamesNo, dispFieldNamesNA, dispFieldNamesBlank)

'that you could display to the user by setting their values into fields or using a messagebox





Call uidoc.FieldSetText("TotalNoQR", Cstr(T_Number))

Call uidoc.FieldSetText("TotalYesQR", Cstr(Y_Number))

Call uidoc.FieldSetText("TotalNAQR", Cstr(NA_Number))

Call uidoc.FieldSetText("TotalBlank", Cstr(Blank_Number))

End Sub

Subject: RE: Check for empty answer fields - amend script agent

Please note…This code makes an assumption that the question number is the number following the Q_ in the item name.

Subject: Check for empty answer fields - amend script agent

You can simplify your code this way so that it won’t loop thru the all the fields 4 times and add up different counts.

This code will do the same in a simple way:

dim count as integer

Dim T_Number As Integer

Dim Y_Number As Integer

Dim NA_Number As Integer

Dim Blank_Number As Integer

dim unanswer as variant

'Get the unanswered list and all other number counts in one for loop.

Forall i In items

If Instr(i.name, "Q_") > 0 Then 

      count = count +1

  If i.values(0) = "No" Then

T_Number = T_Number + 1

   Elseif i.values(0) = "Yes" Then

Y_Number = Y_Number +1

   Elseif i.values(0) = "N/A" Then

NA_Number = NA_Number +1

   Elseif i.values(0) = "" Then

Blank_Number = Blank_Number +1

            if Blank_Number = 1 then 

                redim unanswer(Blank_Number-1)

            else

                redim preserve unanswer(Blank_Number-1)

            end if

           unanswer(Blank_Number -1) = count

   Else

   End If



 End If 		

End Forall


Now get the list of unanswered questions as follows:

Dim tmp As String

Dim k As Integer

Dim tmplst As String

tmp = “Question”

For k = 0 To Ubound(unanswer)

tmp = tmp & " " & unanswer(k)

If k =0 Then

   tmplst = tmp

Else

   tmplst = tmplst & Chr(13) & tmp

End If

Next

Chr(13) denotes a Newline character.

If you do not want the Question numbers to be displayed one below the other, you can replace Chr(13) in the code with a “comma” or “semicolon”.

Find the line of code with —> “Number of unaswered” and replace it with the “tmplst” which contains the result of the Blank Question numbers.

Hope this helps

Subject: RE: Check for empty answer fields - amend script agent

Thanks for your help.

I’ve put this code into the query save event and it does record the blank questions. The only problem is each time it loops it records the blank questions i.e.

Question 1Question 1 2Question 1 2 3Question 1 2 3 4.

Is there a way around this?

Caroline

Subject: RE: Check for empty answer fields - amend script agent

Move this into the loop as first line which will solve the problem.

tmp= “Question”

Sorry for the inconvenience.

Subject: RE: Check for empty answer fields - amend script agent

Thanks that worked great.

Caroline.

Subject: RE: Check for empty answer fields - amend script agent

I’ve been having some problems with this agent lately. We’ve updated the choices open to users and added the options ‘No - Other’ and ‘No - Documentation’.

The agent throws up the error ‘Attempt to access uninitialized dynamic error’ when all the questions have been answered. I ran the agent through debugger and it’s stopping at the line:

For k = 0 To Ubound(unanswer)

Any ideas why this is occuring?

Caroline.


%REM

Checks for empty answer fields & asks the user whether he/she wants to continue with saving the doc or that he/she wants to go back to the doc.

%END REM

Sub Querysave(Source As Notesuidocument, Continue As Variant)

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument     

Dim item As NotesItem

Dim T_Number As Integer

Dim Y_Number As Integer

Dim NA_Number As Integer

Dim tmp As String

Dim k As Integer

Dim tmplst As String



Set uidoc = ws.CurrentDocument

Set doc = uidoc.document



items = doc.items



Forall i In items

	

	If Instr(i.name, "Q_") > 0 Then 

		count = count +1

		If i.values(0) = "No" Then

			T_Number = T_Number + 1

		Elseif i.values(0) = "Yes" Then

			Y_Number = Y_Number +1

		Elseif i.values(0) = "N/A" Then

			NA_Number = NA_Number +1

		Elseif i.values(0) = "No - Documentation" Then

			T_Number = T_Number +1

		Elseif i.values(0) = "No - Other" Then

			T_Number = T_Number +1

		Elseif i.values(0) = "" Then

			Blank_Number = Blank_Number +1

			If Blank_Number = 1 Then 

				Redim unanswer(Blank_Number-1)

			Else

				Redim Preserve unanswer(Blank_Number-1)

			End If

			unanswer(Blank_Number -1) = count

		Else

		End If

		

	End If 

	

End Forall	



For k = 0 To Ubound(unanswer)

	tmp = ""

	tmp = tmp & " " & unanswer(k)

	If k =0 Then 

		tmplst = tmp

	Else

		tmplst = tmplst & "; " & tmp

	End If

Next



Call uidoc.FieldSetText("TotalNoQR", Cstr(T_Number))

Call uidoc.FieldSetText("TotalYesQR", Cstr(Y_Number))

Call uidoc.FieldSetText("TotalNAQR", Cstr(NA_Number))

Call uidoc.FieldSetText("TotalBlank", Cstr(Blank_Number))

Call uidoc.FieldSetText("TotalNumber", Cstr(tmplst))

End Sub