First, THIS IS NOT A COMPLETE SOLUTION FOR YOUR QUESTION!
Posting this because I am running out of methods to try & maybe this information along with someone else’s view may provide a complete solution.
Had a similar problem when using a button event in a dialog (see → Dialog problem when using Python). The solution was to use
Threading. The basis of the problem was that you couldn’t act on an object which still had a process running. This situation seems similar. When the form is being opened, the record can’t be set because the form won’t be completely opened until Python routine is completed. Kind of a catch-22 situation.
moveToInsertRow does work in Python and can be done from a button on an open form. So this eliminates the question of it working or of a different implementation.
The other noticable item was during a simple test, if the second form was open before the code was run the form would display a new record correctly upon execution of my code. As further evidence of this, using various events I had the routine execute two times and the form opened and was at a new record. Now it seems more obvious as to the similarities to the dialog problem.
However, tried Threading to no avail. Also tried
atexit to no success. Have tried multiple variations and other oddball mechanisms but none worked. Again, what does work is to run a script to open the form and when that script completes run a second script to set the form at “new record”. This was accomplished using the
Approve action event to open the form & the
Execute action event to set form to new record. See → Events
Have attached original sample replacing original embedded python with the two simple python routines for demonstration.
Sample ---- PostedTestPython.odb
It appears both @jimk and I had the correct approach in mind but the wrong items. I have now got a working version. Using the version published by Jim K, changed the items which were threaded. The dialog thread was replaced with the opening of the form itself. The second thread was to pause ( .3 seems to be OK ) while form completed & then set to new record.
Much thanks to @jimk ( & +1 ) for much appreciated help!
Sample #2 ----- RevisedJimKVersion.odb