Module code is executed twice

PRD

Registered User.
Local time
Today, 06:37
Joined
May 18, 2011
Messages
72
I wrote a Macro which passes seven parameters to a Module. The Module simply takes these parameters and writes a record to a table. The problem is the Module is being executed two times, the first time writes a new record to the table and the second pass gives me a ‘duplicate key’ error. There are no ‘Do-Loops’ in the Module and when I step it through the debugger when it gets to the ‘End Function’ line (the last line in the Module) instead of exiting the program it jumps back up to the first line of the Module and re-executes the code. I have written Modules before but never encountered one that ran two times. Can anyone tell me why this is happening. Thank you for your time.
 
Might help if we could see the Module and one of the Macros, well tell us the steps the Macro does.
 
Can't say why it fires twice - something is triggering it obviously.
But what you can try is have a module-scoped or static flag that checks whether the function has already run, and if the flag is true it can just bypass the 'meat' of the function. Obviously you must have some method to set the flag to false again.
Can be OnCurrent of a form, or perhaps it can do a time check so that the function will not run if the function is called twice in a 2 second interval.
 
For anyone who is interested I found and corrected the problem…


The Macro which ran the Module (called ’SaveRecord’) consisted of seven ‘SetVal’ statements (to load the seven parameters) and a ‘Runcode’ statement which ran the SaveRecord Module. In the last line of the SaveRecord Module (prior to the ‘End function’ statement) I set the Module name to the message (called ’PGMMessage’) that I wanted to pass back to the Form…


SaveRecord = (PGMMessage)


Then after the ‘Runcode’ statement in the Macro I had one more ‘SetVal’ statement to set the message field in the Form (called ’FormMessage’) to the value of the Module name…


SetVal
[FormMessage]
SaveRecord (Parm1, Parm2,……Parm7)


As it turns out this last ‘SetVal’ statement was re-running the SaveRecord Module and it was here that I got the ‘duplicate key’ error because it was trying the write the same record a second time.


I did not realize that a ‘SetVal’ statement could actually run a Module. I thought it would just retrieve the value of the Module name that I set in the SaveRecord Module.


The fix to the problem was simple, I just deleted the ‘Runcode’ statement from the Macro and let the last ‘SetVal’ statement both run the SaveRecord Module AND pass the message back to the Form. Everything works fine now. Thank you.
 
Thank you for sharing it is bbound to help someone else!
 

Users who are viewing this thread

Back
Top Bottom