Qui entriamo nel dettaglio, devi fare tu il debug del tuo codice, manca un end if dove ci dovrebbe essere.........