No time at the moment so just a quick info: You don’t need to loop thru all records.
Instead take a look at the search command in the AppleScript dictionary (in Script Editor.app or Script Debugger.app (which got a superior dictionary explorer)).
Here‘s a script that uses this command Searching for items containing the same Alias?.
search doesn’t use the scope: syntax that’s used in toolbar searches. To search an entire database use in root of [database] (as in the linked script).