It was from the Overboard comic strip (the one about pirates anachronistically operating in today’s world). The theme of the strip was that one of the pirates had their hand stuck in a cookie jar; Captain Crow advises him to let go of some of the cookies so he can get his hand out; hours later night has fallen and the pirate still has his hand stuck in the jar. Does anyone anywhere have this strip archived?
You’re sure that you’re not thinking of the Simpsons gag with the vending machines?
Well, GoComics has the whole thing archived here: http://www.gocomics.com/overboard/1990/08/20.
That’s a quarter-century plus, but happy digging!