Loading variable from external file not working!

Hi
I’m having trouble with this script. I have an external text file, from which all of the text on the page is loaded. I also have a series of thumbnails, but I need to have different numbers on each page, so I figured I’d put the number of thumbnails into the text file too and then run a loop to create the thumbnails and buttons dynamically.
The loop works fine, until I try to use the value I have loaded from the text file. I think it is something to do with it being in a separate function, but I have tried making it _global.counter, I have tried putting the whole for() loop inside the myData.onLoad function. Basically, I have tried everything I can think of and still the loop does not run when the counter value is specified externally.
Please help!!!
Thanks

myData = new LoadVars();
myData.load("blurb.txt");

//load external text
myData.onLoad = function(){
	sectorName.html = true;
	sectorName.htmlText = this.sector;
	mainText.html = true;
	mainText.htmlText = this.main;
	titleText.html = true;
	titleText.htmlText = this.title;
	link_txt.html = true;
	link_txt.htmlText = this.links;
	counter = this.images;
	trace(counter);
	my_color = new Color(colourfill);
	my_color.setRGB(this.colour);	
	_global.oldimage = 1;
}

for (i=2; i<counter; i++) {
	trace("counter = "+counter);
	//get number of button and assign thumbnumber so that we can put the image presently in the container back into it
	curr_button = _root.createEmptyMovieClip("thumbbutton"+*,i+1000).attachMovie("whitebutton","button"+*, i+1100);
	curr_image = _root.createEmptyMovieClip("thumb"+*,i+500);
	//set position of current button
	curr_button._y = curr_image._y = 90;
	curr_button._x = curr_image._x = 440+((i-2)*110);
	my_mc.loadClip("t"+*+".jpg", curr_image);
	trace("i = "+i);
	curr_button.thumbnumber = i;
	curr_button.newimage = i;
	curr_button.useHandCursor = true;
	curr_button.onPress = function(){
		//assign the number from the file we want to appear to be _global.newimage
		_global.newimage = this.newimage;
		// set initial value to be the oldimage so that links update for next time
		this.newimage = _global.oldimage;
		// load image into container
		my_mc.loadClip(_global.newimage+".jpg", "container");
		//load image into thumbnails
		my_mc.loadClip("t"+_global.oldimage+".jpg", "thumb"+this.thumbnumber);
		//set _global.oldimage - image in container so that next click works
		_global.oldimage = _global.newimage;
	};
};

//default image
	my_mc.loadClip("1.jpg", "container");