Just to let you know that I have sorted it out at last. I had a go at amending the sql code as you suggested but that caused even more problems, elsewhere in the system. In the end I just used a variable and carried it from page to page. It works which is the main thing.
So thanks again for all your assistance.