You want to set the value of Z in the add() function and pass that value to the querystring in the URL. I assume you want a client side implementation here.
It wont work that way because the variable Z is not recognized outside the <script> block, so the <A href> does not know of such a variable.
What will work however, is if you create the whole URL within the function and submit it from within the function.
Your code will look like this:
In the add() function, the URL is constructed and browser address is instantly changed to that location.
<script>
function add()         { 
var Z="3025"; 
document.location="http://XXXXXXXX.co.uk/XXXX.qmd?pack_id="+Z;
} 
</script> 
<form> 
    <input type ="submit" onClick="javascript:add()" value="my button"> 
    </form>
If you actually want to just set the variables into the <A href> so that these can be clicked by the user at a later time, you'll probably need to use some not recommended techniques such as document.write of the full  url.
Read the above link for why not to go down that route.