Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / January 2008

Tip: Looking for answers? Try searching our database.

Infinite loop in OnAfterChange event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bev Kaufman - 25 Jan 2008 21:59 GMT
I just upgraded to InfoPath 2007, and my first disappointment was that it
still doesn't have the standard textbox formatting that would allow me to
display a formatted phone number.  So today I wrote my first InfoPath
function that can take an input string and output it in the correct format.

But when I tried to implement it in the OnAfterChange event, it caused an
infinite loop because the act of loading back the formatted string is
changing the field, so that the event fires again.

I found advice in the newsgroup to use this code:
    if(eventObj.IsUndoRedo || "Insert" != eventObj.Operation)
        {
            return;
        }
but it doesn't work.
I thought of using a boolean variable to check whether the change is the
result of the function, but keeping track of that variable would be really
hairy, particularly since I have five phone fields.  There doesn't appear to
be a GotFocus event to tell me to turn the boolean off.

I saw advice to use the validation rules instead of writing code, but the
function involves more than just a simple concat statement.  There must be a
simpler way to do this, but right now it's got 12 lines of code.

How can I get this working?
Greg Collins - 29 Jan 2008 22:51 GMT
Changes in your event occur before the event finishes. So you can set a flag
(fChangingFieldX = true) and then check for this flag at the start of the
event handler (if(fChangingFieldX == true) return;). Then at the end of your
event hanlder, set the flag back to false. This will prevent the event
handler from being allowed to call itself (or process again as a result of a
change it caused).

Signature

Greg Collins
Microsoft MVP
Visit Braintrove at http://www.braintrove.com
Visit InfoPathDev at http://www.infopathdev.com

Bev Kaufman - 30 Jan 2008 14:26 GMT
Thank you.  That did the trick

> Changes in your event occur before the event finishes. So you can set a flag
> (fChangingFieldX = true) and then check for this flag at the start of the
> event handler (if(fChangingFieldX == true) return;). Then at the end of your
> event hanlder, set the flag back to false. This will prevent the event
> handler from being allowed to call itself (or process again as a result of a
> change it caused).
Greg Collins - 31 Jan 2008 00:23 GMT
Awesome! Glad to have helped. You will use that trick a lot in your code
development.

Signature

Greg Collins
Microsoft MVP
Visit Braintrove at http://www.braintrove.com
Visit InfoPathDev at http://www.infopathdev.com


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.