ObiBenKenobi
01-30-2012, 07:10 AM
Hi there, I have an Access 2007 front-end with a SQL 2008 backend. I am successfully using a form with combo boxes to filter my passthrough query by using a command button to rewrite the SQL string based on the data on the form.
My problem is that I am having difficulty writing clean script in my On_Click VBA code. Currently, I have everything posted in one VB line and it works fine. If I throw in some “& _ “ to break it up, I start getting errors. The line break works fine if I’m only using SELECT commands, but it doesn’t like the breaks when I add WHERE criteria.
Here is the working On_Click code:
Private Sub Command9_Click()
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT C_CASE_EXTID AS 'CASE NUMBER',OU_T_ORG_UNIT_NM AS 'TEAM', C_STAT_AID_STAT_CD AS 'FEDERAL AID STATUS', C_CRNT_STAT_CD AS 'CASE STATUS', C_CASE_PROC_CD AS 'INTAKE STATUS', C_NON_COOP_STAT_CD AS 'COOPERATION', OU_O_ORG_UNIT_DESC_TXT AS' MANAGING OFFICE'from CASE_CAS WHERE C_MNG_CNTY_FIPS_CD = '" & [Forms]![frm_Select_Office].[Combo0] & "' and OU_T_ORG_UNIT_NM = '" & [Forms]![frm_Main_Form].[Team] & "' and C_CRNT_STAT_CD = '" & [Forms]![frm_Main_Form].[CaseStatus] & "' and C_STAT_AID_STAT_CD= '" & [Forms]![frm_Main_Form].[FedAidStatus] & "' ;"
db.QueryDefs("PassThroughTest").SQL = strSQL
DoCmd.OpenQuery "PassthroughTest"
End Sub
Here is what I would like the strSQL section to look like:
strSQL = "SELECT C_CASE_EXTID AS 'CASE NUMBER'” & _
“,OU_T_ORG_UNIT_NM AS 'TEAM'” & _
“, C_STAT_AID_STAT_CD AS ‘FEDERAL AID STATUS’” & _
“, C_CRNT_STAT_CD AS ‘CASE STATUS’” & _
“, C_CASE_PROC_CD AS ‘INTAKE STATUS’” & _
“, C_NON_COOP_STAT_CD AS ‘COOPERATION’” & _
“, OU_O_ORG_UNIT_DESC_TXT AS’ MANAGING OFFICE’” & _
“from CASE_CAS” & _
“WHERE” & _
“”C_MNG_CNTY_FIPS_CD = '" & [Forms]![frm_Select_Office].[Combo0]” & _
& "' and OU_T_ORG_UNIT_NM = '" & [Forms]![frm_Main_Form].[Team] & _
& "' and C_CRNT_STAT_CD = '" & [Forms]![frm_Main_Form].[CaseStatus] & _
& "' and C_STAT_AID_STAT_CD= '" & [Forms]![frm_Main_Form].[FedAidStatus] & "' ;"
I’m hoping this is a simple case of me leaving out parentheses or spaces somewhere.
My problem is that I am having difficulty writing clean script in my On_Click VBA code. Currently, I have everything posted in one VB line and it works fine. If I throw in some “& _ “ to break it up, I start getting errors. The line break works fine if I’m only using SELECT commands, but it doesn’t like the breaks when I add WHERE criteria.
Here is the working On_Click code:
Private Sub Command9_Click()
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT C_CASE_EXTID AS 'CASE NUMBER',OU_T_ORG_UNIT_NM AS 'TEAM', C_STAT_AID_STAT_CD AS 'FEDERAL AID STATUS', C_CRNT_STAT_CD AS 'CASE STATUS', C_CASE_PROC_CD AS 'INTAKE STATUS', C_NON_COOP_STAT_CD AS 'COOPERATION', OU_O_ORG_UNIT_DESC_TXT AS' MANAGING OFFICE'from CASE_CAS WHERE C_MNG_CNTY_FIPS_CD = '" & [Forms]![frm_Select_Office].[Combo0] & "' and OU_T_ORG_UNIT_NM = '" & [Forms]![frm_Main_Form].[Team] & "' and C_CRNT_STAT_CD = '" & [Forms]![frm_Main_Form].[CaseStatus] & "' and C_STAT_AID_STAT_CD= '" & [Forms]![frm_Main_Form].[FedAidStatus] & "' ;"
db.QueryDefs("PassThroughTest").SQL = strSQL
DoCmd.OpenQuery "PassthroughTest"
End Sub
Here is what I would like the strSQL section to look like:
strSQL = "SELECT C_CASE_EXTID AS 'CASE NUMBER'” & _
“,OU_T_ORG_UNIT_NM AS 'TEAM'” & _
“, C_STAT_AID_STAT_CD AS ‘FEDERAL AID STATUS’” & _
“, C_CRNT_STAT_CD AS ‘CASE STATUS’” & _
“, C_CASE_PROC_CD AS ‘INTAKE STATUS’” & _
“, C_NON_COOP_STAT_CD AS ‘COOPERATION’” & _
“, OU_O_ORG_UNIT_DESC_TXT AS’ MANAGING OFFICE’” & _
“from CASE_CAS” & _
“WHERE” & _
“”C_MNG_CNTY_FIPS_CD = '" & [Forms]![frm_Select_Office].[Combo0]” & _
& "' and OU_T_ORG_UNIT_NM = '" & [Forms]![frm_Main_Form].[Team] & _
& "' and C_CRNT_STAT_CD = '" & [Forms]![frm_Main_Form].[CaseStatus] & _
& "' and C_STAT_AID_STAT_CD= '" & [Forms]![frm_Main_Form].[FedAidStatus] & "' ;"
I’m hoping this is a simple case of me leaving out parentheses or spaces somewhere.