NEVER DISCONNECT THE BATTERY WITH THE ENGINE RUNNING!!!! YOU CAN FRY THE ELECTRONIC SYSTEMS!!!
The "visor trick" is to turn on the passenger visor light when you observe the symptoms. If all gets well, it's likely a worn out ignition switch.
Otherwise, the problem could be in the general module, as doru says.
Might check the seat connector, as DONROD suggests.
Finally, the battery voltage s/b 12.5+ after setting overnight, and 14+ when the engine is running.
Ed in San Jose '97 540i 6 speed aspensilber over aubergine leather. Build date 3/97. Golden Gate Chapter BMW CCA Nr 62319.