try this
push the moonroof button UP (not open or close ) point your finger staight up, hold it in for at least 30 seconds
this will retrain the moonroof, it needs to be retrained every time the battery is unhooked ( pretty much everytime you have it serviced, your dealer should have done it )
here's a like with more detail
http://www.bimmerfest.com/forums/showthread.php?t=83501