I have an Access 2010 database that has links to several ODBC tables. My username and password were cached when I first linked the tables, so I don't have to re-enter my Login ID and Password everytime I run a query - except on one. I have 20+ queries that are working just fine without having to relogin.
My SQL is below. Is there something intrinsically off in the code, or is it another issue?
SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide INNER JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) INNER JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((Relationship.Relationship) Like "*spouse*"));
[Contacts In Query (username)] is a flat table in Access, the others (Relationship and ContactContact) are linked from an external database.
I've got a workaround - oddly, as soon as I changed the joins from INNER JOIN
to LEFT JOIN
and RIGHT JOIN
, it worked without requiring a login. I then had to use a couple of criteria to approximate an inner join by filtering out results where there wasn't an exact match.
SELECT DISTINCT IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])) AS LeftSideContactID, IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])) AS RightSideContactID, [RightSide]![Pref] & " & " & [LeftSide]![Pref] AS [Combined Prefix], [RightSide]![FName] & " & " & [LeftSide]![FName] AS [Combined FName], IIf([RightSide]![LName]=[LeftSide]![LName],[RightSide]![LName],[RightSide]![LName] & " & " & [LeftSide]![LName]) AS [Combined LName], [RightSide]![Pref] & " " & [RightSide]![FName] & " " & [RightSide]![LName] & " & " & [LeftSide]![Pref] & " " & [LeftSide]![FName] & " " & [LeftSide]![LName] AS [Combined Mailing Name]
FROM (([Contacts In Query (username)] AS LeftSide RIGHT JOIN ContactContact ON LeftSide.ContactID = ContactContact.Contact1ID) LEFT JOIN [Contacts In Query (username)] AS RightSide ON ContactContact.Contact2ID = RightSide.ContactID) INNER JOIN Relationship ON ContactContact.RelationshipID = Relationship.RelationshipID
WHERE (((IIf(IsNull([LeftSide]![ContactID]),"",CStr([LeftSide]![ContactID])))<>"") AND ((IIf(IsNull([rightSide]![ContactID]),"",CStr([rightSide]![ContactID])))<>"") AND ((Relationship.Relationship) Like "*spouse*"));
Hopefully this will help anyone who's come up against the same issue, but if anyone could shed some light on why this is happening, and why this fix works, I'd greatly appreciate it.
It sounds like there may be some database corruption in one of the hidden system tables. Have you tried importing the objects into a new Access database, and then run the original query? Corrupted objects won't import successfully.