I believe Modeler is on to something with his post, as well as Robzguns and Rastoff to a similar extent. One: caliber. The 9mm can be an extremely accurate caliber. Two: gun style/grip angle.
You went from a 40 to a 9. And additionally you changed the gun feel as it sits in your hand. Grips, and their accompanying "feel" in the shooters hand can make a world of difference in the shooters ability to hit the target. For example, I cannot use the large, beautifully styled and manufactured, Smith and Wesson wooden grips.... They just don't help me. Put a set of small Pachmyar's on the gun or a set of Hogue's and I can't miss.
Caliber makes a difference too. The 9 can be wonderfully accurate and most times is. The 40, on the other hand, seems to be adequate but not overly exacting, in my opinion. Just my experience. That may have made quite some difference for you in this example of your experience with the two different guns and their respective calibers. All in all I think that's your experience as so many of the other posters have noted in this thread. When I was instructing my department, I saw many of the different kinds of shooters we all see. Fortunately, my department allowed officers to choose between a variety of calibers (9, 40, 45) for autos. The department issue was a Glock but they could choose the caliber. Many times, I had to steer an officer to a particular caliber because they shot it better than others calibers. Same thing with guns. Your experience is typical.
No need to be sorry for better shooting!!!