I have a big messy query, it looks like this: (about 1200 characters with 10 AND/OR statements)
After I have saved the query and opened it again in design view and saved it again, access turns it into a monster
It looks like this: (4700 characters 50+ AND/OR statements)
The query takes about 5 sec to run, but after the mutation it takes 7. The query is the rowsource of a listbox. I have VBA code that handles query sorting, but it breaks with all these criteria. How can I keep access from messing with my SQL?
Code:
[FONT=Times New Roman][FONT=Arial][FONT=Verdana]SELECT Patients.EMPI, [Patients].[LAST_NM] & ", " & [Patients].[FIRST_NM] & " " & [Patients].[MIDDLE_NM] AS PAT_NM, DateDiff('yyyy',[BIRTH_DT],Date()) AS AGE, Patients.SEX_CD, Providers.LAST_NM & ", " & Providers.FIRST_NM & " " & Providers.MIDDLE_NM AS PRVDR_NM, Patients.PCP_ID, Patients.SCP_ID[/FONT][/FONT][/FONT]
[FONT=Times New Roman][FONT=Times New Roman][FONT=Verdana]FROM Providers RIGHT JOIN (Patients INNER JOIN Screen ON Patients.EMPI = Screen.EMPI) ON Providers.PRVDR_ID = Patients.PCP_ID[/FONT]
[FONT=Verdana]WHERE (Patients.EMPI Like [Forms]![ScreenTool].[FILTEREMPI]& "*") AND [/FONT]
[FONT=Verdana](DateDiff('yyyy',[BIRTH_DT],Date()) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND [/FONT]
[FONT=Verdana](Patients.SEX_CD Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND [/FONT]
[FONT=Verdana](Patients.FIRST_NM Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND [/FONT]
[FONT=Verdana](Patients.LAST_NM Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND [/FONT]
[FONT=Verdana](Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED])<=[Forms]![ScreenTool].[FILTERSTATUS]) AND [/FONT]
[FONT=Verdana]( (Providers.REGION_NM Like [Forms]![ScreenTool].[FILTERREGION]) OR [/FONT]
[FONT=Verdana]([Forms]![ScreenTool].[FILTERREGION] is null) ) AND [/FONT]
[FONT=Verdana]( (Patients.PCP_ID Like [Forms]![ScreenTool].[FILTERPCP]) OR[/FONT]
[FONT=Verdana](Patients.SCP_ID Like [Forms]![ScreenTool].[FILTERPCP]) OR[/FONT]
[FONT=Verdana]([Forms]![ScreenTool].[FILTERPCP] is null));[/FONT]
[/FONT]
[/FONT]
After I have saved the query and opened it again in design view and saved it again, access turns it into a monster
It looks like this: (4700 characters 50+ AND/OR statements)
Code:
[FONT=Verdana]SELECT Patients.EMPI, [Patients].[LAST_NM] & ", " & [Patients].[FIRST_NM] & " " & [Patients].[MIDDLE_NM] AS PAT_NM, DateDiff('yyyy',[BIRTH_DT],Date()) AS AGE, Patients.SEX_CD, Providers.LAST_NM & ", " & Providers.FIRST_NM & " " & Providers.MIDDLE_NM AS PRVDR_NM, Patients.PCP_ID, Patients.SCP_ID[/FONT]
[FONT=Verdana]FROM Providers RIGHT JOIN (Patients INNER JOIN Screen ON Patients.EMPI = Screen.EMPI) ON Providers.PRVDR_ID = Patients.PCP_ID[/FONT]
[FONT=Verdana]WHERE (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Patients.PCP_ID) Like [Forms]![ScreenTool].[FILTERPCP]) AND ((Providers.REGION_NM) Like [Forms]![ScreenTool].[FILTERREGION]) AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS])) OR (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Patients.SCP_ID) Like [Forms]![ScreenTool].[FILTERPCP]) AND ((Providers.REGION_NM) Like [Forms]![ScreenTool].[FILTERREGION]) AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS])) OR (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Patients.PCP_ID) Like [Forms]![ScreenTool].[FILTERPCP]) AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS]) AND (([Forms]![ScreenTool].[FILTERREGION]) Is Null)) OR (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Patients.SCP_ID) Like [Forms]![ScreenTool].[FILTERPCP]) AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS]) AND (([Forms]![ScreenTool].[FILTERREGION]) Is Null)) OR (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Providers.REGION_NM) Like [Forms]![ScreenTool].[FILTERREGION]) AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS]) AND (([Forms]![ScreenTool].[FILTERPCP]) Is Null)) OR (((Patients.EMPI) Like [Forms]![ScreenTool].[FILTEREMPI] & "*") AND ((DateDiff('yyyy',[BIRTH_DT],Date())) Between [Forms]![ScreenTool].[FILTERAGEMIN] And [Forms]![ScreenTool].[FILTERAGEMAX]) AND ((Patients.SEX_CD) Like [Forms]![ScreenTool].[FILTERSEX] & "*") AND ((Patients.FIRST_NM) Like "*" & [Forms]![ScreenTool].[FILTERFIRST] & "*") AND ((Patients.LAST_NM) Like "*" & [Forms]![ScreenTool].[FILTERLAST] & "*") AND ((Abs([Screen].[COMPLETED]+[Screen].[PASSED]+Not IsNull([Screen].[LETTER_DT])+Not IsNull([Screen].[REPLY_DT])+[Screen].[ENROLLED]))<=[Forms]![ScreenTool].[FILTERSTATUS]) AND (([Forms]![ScreenTool].[FILTERREGION]) Is Null) AND (([Forms]![ScreenTool].[FILTERPCP]) Is Null));[/FONT]
The query takes about 5 sec to run, but after the mutation it takes 7. The query is the rowsource of a listbox. I have VBA code that handles query sorting, but it breaks with all these criteria. How can I keep access from messing with my SQL?