Two grids linked to same edit form app in tabbed page - grid to redirect back to?

Hi. I have an interesting challenge, which I am sure is easy to solve, but I can’t quite work it out.

I have two grids that both access the same DB table, but the set of records they display/edit is different base on a where clause. These two grid apps are launched off different items in the menu - I am using tabs, so each grid will open it’s own tab.

I have one form I use to edit the data. The two grids both have application links to the same form which opens in the same window as the grid “calls” it. The form uses my own “close” button (not the standard SC buttons) because I want to do some custom processing before I close the form.

Assuming I have opened Grid A in one tab, and Grid B in another tab. The user then clicks on “Add New” in Grid A, which opens Form A. When the user clicks on my Close button, I want to return to Grid A in that tab. The current behaviour, using sc_exit() in my Close button code, causes the form to exit insert mode and simply displays the first record in the DB table ready to edit it - it does not go back to the grid. I don’t mind using sc_redir() behind the button to go back to Grid A, but how does my form know if Grid A or Grid B called it (IE, which tab it is running in)?

At one stage I tried setting up a global variable - the link in each grid sets the variable with the calling grid’s name as a fixed value, and my Close button would simply sc_redir() to the grid app in the global variable. But the problem here is that if Form A is called by Grid A then global will have Grid A’s name in it, but if (while leaving Form A open) I then select Grid B (in it’s tab) and open Form A to edit a different row, this will change the global variable contents with Grid B’s name. If I then exit Form A using the global I then get Grid B displayed in Grid A’s tab.

Is there anyway I can code Form A to return to the “calling” grid, whichever tab it runs in? Is there anyway I can access the tab ID (menu “item_??”) that the Form A app is running in (I can work out which Grid to redirect to from that)? I note there is an SC macro for this but it is only available in the Menu app (onExecute).

BTW, I don’t want to create two copies of Form A, one dedicated to Grid A (with a dedicated re-direct code back to Grid A) and the other Form A(B) dedicated to handle Grid B. I’ll end up maintaining the same form/code in two places.

Any ideas appreciated.