Testing user selection of a button - Cancel or OK

This test works when I am asking to only click on one of the buttons “Cancel or OK”. Now that I am asking to the user to enter a number and select a button the test does not work anymore.

Would someone know why?

THANKS in advance!
Daniel

set newBatchNumber to 1
set startingNumber to (the text returned of (display dialog "Enter STARTING number" buttons {"Cancel", "OK"} default button 2 default answer newBatchNumber)) as integer

if button returned of result is "Cancel" then
	--	write (return & (current date) & " L'opération a été annulé par l'opérateur") as string to fileRef starting at eof
	--	close access fileRef
	display dialog "Cancel"
	return
end if

Hi,

the result of pressing the Cancel button is error number -128 (user cancelled),
you need a repeat loop to catch the error if the user enters a non-number value.
To check which button has been pressed is actually not necessary.
Cancel and OK are the default buttons so you don’t have to specify them


set newBatchNumber to 1
repeat
	try
		set startingNumber to (the text returned of (display dialog "Enter STARTING number" default button 2 default answer newBatchNumber)) as integer
		exit repeat
	on error e number n
		if n = -128 then -- user cancelled
			return
		else if n = -1700 then -- integer coerce error
			display dialog "please enter a number" buttons {"Try Again!"} default button 1 giving up after 2
		end if
	end try
end repeat

--	write (return & (current date) & " L'opération a été annulé par l'opérateur") as string to fileRef starting at eof
--	close access fileRef