I have asked a question and have received a good answer. The answer suggests that I change the code but even when changing it to what is suggested, it does not work. Nevertheless I feel that this is a step forward from before. Can I update the question with this new code or should I ask a new updated question? If I update the question it might confuse people as there will be a discrepancy with the previous reply.
Well, technically, you can always edit a question.
However, if you have answers that are directed to the original version of the question and the edit will invalidate the answers, you should ensure you edit in a way that doesn't do that.
One way is to add updates at the bottom of the original question.
However, think carefully if this is still the same question - if not you should ask a new question and possibly link to the question the inspired it.
With the specific scenario you describe, a comment on the answer is appropriate (something along the lines of "thanks, tried it but it didn't work. I got the following errors/symptoms").