When the user clicks a button on a form, I want this SQL to execute:
Given that name such as "DecActFTE" and "tblOpPlanForecast12" vary from month to month, I want the user to select a value from a drop-down menu, and the code will execute based on the selection. Rather than having to write the query 12 times and call one of 12 available queries, it seems more efficient to do simply specify the variables "DecActFTE" and "tblOpPlanForecast12" based on the selection. Thus I tried to write my query like this and I keep getting syntax errors:
I know this is the right way to go, but I can't figure out what I'm doing wrong. I'm trusting the experts here know the answer!
Code:
DoCmd.RunSQL "SELECT tblOpPlanForecast.ProjNum, tblOpPlanForecast.DevCenter, " & _
"Sum(tblOpPlanForecast.DecRes) AS DecActFTE, Sum(tblOpPlanForecast.SumOfDecODC) AS DecActODC, " & _
"Sum(Nz(tblOpPlanForecast12.DecRes)) AS DecFCFTE, Sum(Nz(tblOpPlanForecast12.SumOfDecODC)) AS DecFCODC INTO tblTT4 " & _
"FROM tblOpPlanForecast LEFT JOIN tblOpPlanForecast12 ON (tblOpPlanForecast.DevCenter = tblOpPlanForecast12.DevCenter) " & _
"AND (tblOpPlanForecast.ProjNum = tblOpPlanForecast12.ProjNum) " & _
"GROUP BY tblOpPlanForecast.ProjNum, tblOpPlanForecast.DevCenter;"
Given that name such as "DecActFTE" and "tblOpPlanForecast12" vary from month to month, I want the user to select a value from a drop-down menu, and the code will execute based on the selection. Rather than having to write the query 12 times and call one of 12 available queries, it seems more efficient to do simply specify the variables "DecActFTE" and "tblOpPlanForecast12" based on the selection. Thus I tried to write my query like this and I keep getting syntax errors:
Code:
Dim strNumber As String
Dim strName As String
strNumber = cboMonthNumber.Value
strName = txtMonth.Value
DoCmd.RunSQL "SELECT tblOpPlanForecast.ProjNum, tblOpPlanForecast.DevCenter, " & _
"Sum(tblOpPlanForecast."&strName&"Res) AS "&strName&"ActFTE, Sum(tblOpPlanForecast.SumOf"&strName&"ODC) AS "&strName&"ActODC, " & _
"Sum(Nz(tblOpPlanForecast"&strNumber&"."&strName&"Res)) AS "&strName&"FCFTE, Sum(Nz(tblOpPlanForecast"&strNumber&".SumOf"&strName&"ODC)) AS "&strName&"FCODC INTO tblTT4 " & _
"FROM tblOpPlanForecast LEFT JOIN tblOpPlanForecast"&strNumber&" ON (tblOpPlanForecast.DevCenter = tblOpPlanForecast"&strNumber&".DevCenter) " & _
"AND (tblOpPlanForecast.ProjNum = tblOpPlanForecast"&strNumber&".ProjNum) " & _
"GROUP BY tblOpPlanForecast.ProjNum, tblOpPlanForecast.DevCenter;"
I know this is the right way to go, but I can't figure out what I'm doing wrong. I'm trusting the experts here know the answer!