Catch a click event on a tab

I need to catch a click event on a tab. And what I mean by “tab” here is a tab within a Menu application.
I’m workimg with a main grid (clients) and from there I can open one or several clienty at the same time. Each of them is opened on a separate tab under the main Menu.
A global var is used from the main grid to open the client (tab). So when I open the second client, the global var is set to 2 (client #2). Then of I click on the tabof client #1, the global var is still set to 2 which causes errors when I add a detail because the wrong master ID is used.
I turned this issue all directions and haven’t found a way. If I could reset my global var when user clicks on the tab, that would solve this.
Thx a lot.