The automated exposure modes on the camera typically effect the default behavior of flash.
When shooting any flash image, you're combining two sources of illumination: the ambient, and the flash. And you can balance those two sources against each other any number of ways, ranging from a lit subject with a black background to a silhouetted subject against a light background. Flash and ambient are affected by different sets of factors.
Ambient is controlled by iso, aperture, and shutter speed.
Flash is controlled by iso, aperture, subject-to-flash distance, and flash power.
When you have the camera in aperture or shutter priority modes, the default assumption by the auto-exposure system is that you want to use the flash for fill—most of the light will come from the ambient, with only a small amount of flash to "fill in" the shadows. With A mode, in low light, that can mean slow shutter speeds.
P and Full Auto modes are a little different from A and S. In P mode, the light level is taken into account, and fill is only assumed until lower light levels are measured, at which point, the camera will then use the flash as main illumination, just like a P&S. :)
Sometimes cameras have custom settings that can limit how low the shutter speed can go in A mode (typically to 1/60s for handholding "safety"), or to limit the shutter speed even farther (typically 1/200s) to force the balance of the light to shift from the ambient over to the flash as main illumination. However, I don't think the D40X has these settings.
M mode, naturally, gives you the most control to balance the ambient against the flash however you want to, and would probably be your best bet.