Its only 4 digits how hard can it be, right?

I am trying to re-create a password screen that starts with four zeros. the one on the end flashes. A user would hit and up or down button to scroll through the numbers 0-9 and the left or right button to switch between digits. when the code is completed the enter button is pressed and the code is accepted.

I can create the dynamic text and even change the digits 0-9 with the buttons but when i change frames the numbers go away. my main issue is that i would like to put all my code in one action frame but tend to end up copying functions and renaming them to use the same button with different functions per frame.

here is my confusing code… any help would be appreciated.

up1_btn.addEventListener(MouseEvent.CLICK, up1);
function up1(e:MouseEvent):void
{
var nr = int(first_txt.text);
nr++;
first_txt.text = nr.toString();
if(nr == 10)
{
first_txt.text=“0”
}
}
down1_btn.addEventListener(MouseEvent.CLICK, down1);
function down1(e:MouseEvent):void
{
var nr = int(first_txt.text);
nr–;
first_txt.text = nr.toString();
if(nr == -1)
{
first_txt.text=“9”// jump to …
}
}

this works but i have to put it on every frame and rename the function and the buttons. the main issue is that when i leave the frame the information is lost. so if i set the text box to 5 it reverts back to default when i leave the frame.

This is the code i have on my action layer

stop();

next_btn.addEventListener(MouseEvent.CLICK, next1);
function next1(e:MouseEvent):void{
if (first_txt.text==“5”){
gotoAndStop(5);
}else if (first_txt.text=="-"){
gotoAndStop(2);
}else if (first_txt.text==“5” && second_txt.text==“3”){
gotoAndStop(6);
}else if (second_txt.text=="-"){
gotoAndStop(3);
}else if (third_txt.text==“0”){
gotoAndStop(7);
}else if (third_txt.text=="-"){
gotoAndStop(4);
}else if (fourth_txt.text==“9”){
gotoAndStop(8);
}else if (fourth_txt.text=="-"){
gotoAndStop(1);
}
}

I can make the numbers flash by going back and forth between frames with a delay but i am losing my data when i change frames.

so i would like it to function something like this…

0000
(push up button 9 times)
0009
(push left button twice, then up 3 times)
0309
(push left button once, the up button 5 times)
5309

then enter button checks my code and im done.

I can check the code but everytime I try this it becomes very cumbersome. I am sure there is a beter way. Any ideas, or places to look? I searched all the keywords I could think of.