Aw, y'all already beat me to it.

Yea, my dad pulled mine for some reason (I can't remember, he just takes it sometimes and does stuff to it, *sigh*). Then, he was like, "Oops, I forgot about the code". It was a pain in the ass, but you just have to prove to BMW that you're the owner and they will give it to you if you don't have it. (As has already been said) Changing the battery made me have to re-enter the code but it didn't mess with my alarm. I'm sure that's another reason. What's wrong with it? Maybe you just need to replace your fob's battery?