What is the rule for determining whether to use a or an?

The rule is: Use an before a word beginning with a vowel sound, however the word is spelled. Hence you say an MBA, an hour, but a BA, a horologist. You say either an historical event or a historical event, according to whether you pronounce the h. The rule is that if the h- is sounded, then a is the proper form. The indefinite article a is used before words beginning with a consonant sound, y and w sounds. The sound, not the actual letter, determines which you use. It is not unusual to find a before a word starting with a vowel or an before a word starting with a consonant.