Permanently losing magic doesn’t seem to be possible.
We know of wizards who were bad at magic (like Neville and Ron in their first years), and wizards whose magical abilities were weakened due to emotional issues (like Merope Gaunt, Ariana Dumbledore, and to a lesser extent Tonks - she seemed to lose her Metamorphmagus ability but retained the ability to cast spells). However, there’s no known examples of anyone who permanently lost their magic power with no way of getting it back. If someone shows early signs of magic, and then is accepted to Hogwarts, that means they are a wizard and will always be one.
It’s not possible for someone to gain magic if they don’t have it.
Albus Dumbledore, who would have no problem with the idea of a Muggle becoming magical if it was possible, refers to the king in “Babbitty Rabbity and Her Cackling Stump” as foolish for thinking he can simply become a wizard.
“The King in Beedle’s story is a foolish Muggle who both covets and fears magic. He believes that he can become a wizard simply by learning incantations and waving a wand.17”
He further explains in his notes that wizards are born with their abilities, they can’t be created, and Muggles will not be able to do magic. It is possible to get magical powers without any known ancestors being wizards, though it’s likely that there would be one somewhere in the line even if they’re many generations removed.
“17 As intensive studies in the Department of Mysteries demonstrated as far back as 1672, wizards and witches are born, not created. While the “rogue” ability to perform magic sometimes appears in those of apparent non-magical descent (though several later studies have suggested that there will have been a witch or wizard somewhere on the family tree), Muggles cannot perform magic. The best — or worst — they could hope for are random and uncontrollable effects generated by a genuine magical wand, which, as an instrument through which magic is supposed to be channeled, sometimes holds residual power, which it may discharge at odd moments — sec also the notes on wandlore for “The Tale of the Three Brothers.”
- The Tales of Beedle the Bard
Whether a child has magical powers seems to always be detectable by eleven. However, it’s possible that the child themselves might not know that they are a wizard, or that they might be kept out of wizarding society. (Credence Barebone in the Fantastic Beasts movie is a likely example of this - he’s untrained in magic despite his age, and it’s not clear whether anyone tried to get him into Ilvermorny or any other magic school.)