[Base, Python] Tiny Macro Refreshing Forms, List/Combo Boxes
Posted: Sat May 20, 2017 3:10 pm
Hands-on example for testing
Open movies_5.odb with 2 forms. Both forms reflect a many-to-many relation between movies and genres. One form lets you assign movies to genres, the other lets you assign genres to movies. No macro involved. Just an ordinary many-to-many relation, reflected in both directions by 2 Base forms. Both forms have an additional form to add new genres or movies respectively.
After you have saved a genre name, you can go back to the grid of genres belonging to the current movie, refresh the list box in that grid and then assign the newly inserted genre to that move.
In the other form: After you saved a full set of movie attributes, you can go back to the grid of movies belonging to the current genre, refresh the list box in that grid and then assign the newly inserted movie to that genre.
The navigation toolbar provides a second refresh button when a combo box or list box is focussed. This button allows you to refresh only the focussed list/combo box without reloading the entire form.
How to refresh the list boxes of genres/movies automatically?
1. Open attached Writer document, save it in a "trusted directory" (according to the macro security settings) and hit the install button to install the Python code to subfolder Scripts/python/pyDBA/AutoRefresh.py within your profile folder.
2. Use the form navigator to add a new hidden control named "AutoRefresh" (case sensitive) to the "NewMovie" form (or to the "NewGenre" respectively) and add the following hierarchical path to the item you want to refresh, so the value reads ../MainForm/SubForm/SubForm_Grid/MovieID (or with trailing /GenreID if you are editing the other form).
From its containing form, the path goes one level up (..) to the parent container "Forms", then down to the MainForm/SubForm/SubForm_Grid and the list box named "MovieID" or "GenreID respectively.
3. Assign the above macro [MyMacros > pyDBA > AutoRefresh > formAction] to the form's event "After Record Action". Do not assign the code to any form control's event!
4. Save and reload the form document.
How does it work?
The little macro is triggered by form event "After Record Action". A form's record action is either one of insert, delete or update (in other words adding, removing, editing of a whole record). In this particular example it is row insertion since this is the only action provided by the "NewGenre"/NewMovie" forms.
When this has happened, the macro reads the text value from the named control "AutoRefresh", navigates from the containing form to the form control that is specified by the hierarchical path name and refreshes that item.
Without any modifications to the Python code, this tiny macro is able to refresh forms, combo boxes and list boxes. The same form event can refresh multiple items. Just separate their path names with semicolons. All you need is a form event, a hidden control "AutoRefresh" (case sensitive) with one or more path names (case sensitive) pointing to one or more refreshable items (forms, combo/list boxes).
Caveat
There is barely any error handling. You get plain Python errors as message boxes.
no-such-element errors when an addressed item does not exist
attribute errors when you use control events instead of form events or when the addressed item is not refreshable (not a form, combo, list box).
Open movies_5.odb with 2 forms. Both forms reflect a many-to-many relation between movies and genres. One form lets you assign movies to genres, the other lets you assign genres to movies. No macro involved. Just an ordinary many-to-many relation, reflected in both directions by 2 Base forms. Both forms have an additional form to add new genres or movies respectively.
After you have saved a genre name, you can go back to the grid of genres belonging to the current movie, refresh the list box in that grid and then assign the newly inserted genre to that move.
In the other form: After you saved a full set of movie attributes, you can go back to the grid of movies belonging to the current genre, refresh the list box in that grid and then assign the newly inserted movie to that genre.
The navigation toolbar provides a second refresh button when a combo box or list box is focussed. This button allows you to refresh only the focussed list/combo box without reloading the entire form.
How to refresh the list boxes of genres/movies automatically?
1. Open attached Writer document, save it in a "trusted directory" (according to the macro security settings) and hit the install button to install the Python code to subfolder Scripts/python/pyDBA/AutoRefresh.py within your profile folder.
2. Use the form navigator to add a new hidden control named "AutoRefresh" (case sensitive) to the "NewMovie" form (or to the "NewGenre" respectively) and add the following hierarchical path to the item you want to refresh, so the value reads ../MainForm/SubForm/SubForm_Grid/MovieID (or with trailing /GenreID if you are editing the other form).
From its containing form, the path goes one level up (..) to the parent container "Forms", then down to the MainForm/SubForm/SubForm_Grid and the list box named "MovieID" or "GenreID respectively.
3. Assign the above macro [MyMacros > pyDBA > AutoRefresh > formAction] to the form's event "After Record Action". Do not assign the code to any form control's event!
4. Save and reload the form document.
How does it work?
The little macro is triggered by form event "After Record Action". A form's record action is either one of insert, delete or update (in other words adding, removing, editing of a whole record). In this particular example it is row insertion since this is the only action provided by the "NewGenre"/NewMovie" forms.
When this has happened, the macro reads the text value from the named control "AutoRefresh", navigates from the containing form to the form control that is specified by the hierarchical path name and refreshes that item.
Without any modifications to the Python code, this tiny macro is able to refresh forms, combo boxes and list boxes. The same form event can refresh multiple items. Just separate their path names with semicolons. All you need is a form event, a hidden control "AutoRefresh" (case sensitive) with one or more path names (case sensitive) pointing to one or more refreshable items (forms, combo/list boxes).
Caveat
There is barely any error handling. You get plain Python errors as message boxes.
no-such-element errors when an addressed item does not exist
attribute errors when you use control events instead of form events or when the addressed item is not refreshable (not a form, combo, list box).