GotoField in field Exiting event

I am using some script in a Exiting event of a field that check if a GetDocumentByKey returns a value. If it does not I display a MsgBox and returns the cursor to the field using the GotoField method.

However after the user click OK in the MsgBox it returns the cursor to the field and displays the MsgBox 2 more times after that.

It is appear it is executing the Exiting event when the cursor returns to the field with the GotoField method.

(script below)

Any thoughts?

'********************

Sub Exiting(Source As Field)

Dim db As NotesDatabase

Dim session As NotesSession



Dim workspace As New NotesUIWorkspace



Dim  uidoc As NotesUIDocument

Dim doc As NotesDocument



Set Session = New NotesSession

Set db = session.CurrentDatabase 

Set uidoc = workspace.CurrentDocument

Set doc=uidoc.Document



Dim lookupDB As NotesDatabase

Dim lookupView As NotesView

Dim lookupDoc As NotesDocument



Dim empName As Variant



empName = doc.ReportTo



Set lookupDB = New NotesDatabase( db.Server, "names.nsf" )



Set lookupView = lookupDB.GetView("PeopleLookupCN")



'Look up Manger 1's manager

Set lookupDoc = lookupView.GetDocumentByKey(empName,True)



If lookupDoc Is Nothing Then

	answer = Msgbox (Cstr(doc.ReportTo(0)) +  " not found in the Domino Directory." ,0 + 48, "Name Not Found" )

	If answer = 1 Then Goto leave

End If

leave:

Call uidoc.GotoField("ReportTo")

End Sub

Subject: GotoField in field Exiting event

Using “Goto” in your code is not a good practice if it can be avoided by writing better tests.

Your code as written will always execute the “leave” code because of the way you wrote your test and the lack of any code exiting the subroutine if it finds the document, so it appears to be doing the event multiple times in that case.

Try changing your code to execute if the lookup is unsuccessful and to bypass it and exit the subroutine if successful, i.e.:

Set lookupDoc = lookupView.GetDocumentByKey(empName,True)

If lookupDoc Is Nothing Then

Msgbox Cstr(doc.ReportTo(0)) + " not found in the Domino Directory.", 0 + 48, “Name Not Found”

Call uidoc.GotoField(“ReportTo”)

End If

End Sub

I don’t know if that will fix the problem, but it should help because you won’t be returning the user to the field if the lookup is successful, so it shouldn’t execute the Exiting code multiple times in that situation.

Subject: RE: GotoField in field Exiting event

Makes sense now that you pointed it out. Thanks for the insight.