Question about User defined variables

Question about User defined variables

I've declared a variable in the 'On Load' section . . . viz:

nextIndexField = '01-Jan-2000 17:04:16';

if (count(Workshop_Job)  >  0)
{
    //get First record 
    myCollection  =  Workshop_Job  [Added_Time > nextIndexField] sort by  Added_Time  range from 1 to 1;
    input.Customer = myCollection.Customer;
    nextIndexField = myCollection.Added_Time;
}
else
{
    //this is last record ... now what ?
}

But then if I try to use it in my Button Click code . . . viz:

//get 1 record ... whose Added_Time is greater than the one in the current Stateless Form
myCollection  =  Workshop_Job  [Added_Time > nextIndexField] sort by  Added_Time  range from 1 to 1;
input.Customer = myCollection.Customer;
nextIndexField = myCollection.Added_Time;

It doesn't know about it any longer.
Can anyone help with this?  It's like I need to declare a Public variable . . . you can see I'm caught between languages eh