I'm receiving this syntax error:
Syntax error (missing operator) in query expression
'(((Branch.BranchName)=Atlata NE ) AND
((ChartOfAccounts.ScorecardLine)>0))'
When executing the code below. I believe the syntax error is near the Having Clause where I have inserted my variable from the recordset. If someone could take a quick pass at catching where the syntax error is I would be greatly appreciative. Thank you.
----------------------------------------------------------------
Function ScorecardReports()
Dim strOutputFormat As String
strOutputFormat = "PDF Format (*.pdf)"
Dim strObjectName As String
strObjectName = "BranchScorecardRprt"
'Reference to Current Database
Dim db As Database
Dim qdf As QueryDef
Set db = CurrentDb()
Set qdf = db.QueryDefs("qdf")
'Identify table with names
Dim tbl As TableDef
Set tbl = db.TableDefs("Branch")
'Declare a string variable to hold the branch names
Dim strBranchName As String
strBranchName = ""
'Declare a counter for a loop
Dim i As Integer
i = 0
'Open a recordset against the table with names
Dim rst As Recordset
Set rst = tbl.OpenRecordset
With rst
If .RecordCount > 0 Then 'Verify records even exist first!
.MoveFirst 'Moves to the first position
Do While i <= .RecordCount And Not .EOF
strBranchName = rst!BranchName
Dim StrSQL As String
StrSQL = ""
StrSQL = StrSQL & "SELECT Branch.Division, Branch.DivisionName, Branch.Region, Branch.RegionName, Branch.Branch, Branch.BranchName, TY.Period, TY.Year, "
StrSQL = StrSQL & "TY.Account, TY.Ctr, TY.Type, TY.Description, Sum(TY.[Current Period $]) AS [SumOfCurrent Period $], Sum(TY.[To Date $]) AS YTD, "
StrSQL = StrSQL & "Sum(TY.[Current Period Budget $]) AS [SumOfCurrent Period Budget $], Sum(TY.[To Date Budget $]) AS [SumOfTo Date Budget $], "
StrSQL = StrSQL & "Sum(TY.[Current Period Prior Year $]) AS [SumOfCurrent Period Prior Year $], Sum(TY.[To Date Prior Year $]) AS [SumOfTo Date Prior Year $], "
StrSQL = StrSQL & "ChartOfAccounts.ScorecardLine, ChartOfAccounts.Category, ChartOfAccounts.CategoryName, ReportGroup3.ReportGroup3 INTO BranchMasterTbl "
StrSQL = StrSQL & "FROM ((Branch INNER JOIN TY ON Branch.Branch = TY.Div) INNER JOIN ChartOfAccounts ON TY.Account = ChartOfAccounts.Account) "
StrSQL = StrSQL & "INNER JOIN ReportGroup3 ON (ChartOfAccounts.ScorecardLine = ReportGroup3.ScorecardLine) AND (TY.Ctr = ReportGroup3.Ctr) "
StrSQL = StrSQL & "GROUP BY Branch.Division, Branch.DivisionName, Branch.Region, Branch.RegionName, Branch.Branch, Branch.BranchName, TY.Period, TY.Year, "
StrSQL = StrSQL & "TY.Account, TY.Ctr, TY.Type, TY.Description, ChartOfAccounts.ScorecardLine, ChartOfAccounts.Category, "
StrSQL = StrSQL & "ChartOfAccounts.CategoryName, ReportGroup3.ReportGroup3 "
StrSQL = StrSQL & "HAVING (((Branch.BranchName)= " & strBranchName & " ) AND ((ChartOfAccounts.ScorecardLine)>0)) "
StrSQL = StrSQL & "ORDER BY Branch.Branch, TY.Ctr, ChartOfAccounts.ScorecardLine; "
Debug.Print StrSQL
qdf.SQL = StrSQL
DoCmd.SetWarnings False
DoCmd.OpenQuery "qdf"
DoCmd.SetWarnings True
'Output the reports to the H: Drive
DoCmd.OutputTo acReport, strObjectName, strOutputFormat, "H:\Reports\BranchScorecardRprt" & "-" & strBranchName, False, ""
i = i + 1
.MoveNext
Loop
Else
'Do Nothing
End If
End With
Debug.Print strBranchName
End Function
Syntax error (missing operator) in query expression
'(((Branch.BranchName)=Atlata NE ) AND
((ChartOfAccounts.ScorecardLine)>0))'
When executing the code below. I believe the syntax error is near the Having Clause where I have inserted my variable from the recordset. If someone could take a quick pass at catching where the syntax error is I would be greatly appreciative. Thank you.
----------------------------------------------------------------
Function ScorecardReports()
Dim strOutputFormat As String
strOutputFormat = "PDF Format (*.pdf)"
Dim strObjectName As String
strObjectName = "BranchScorecardRprt"
'Reference to Current Database
Dim db As Database
Dim qdf As QueryDef
Set db = CurrentDb()
Set qdf = db.QueryDefs("qdf")
'Identify table with names
Dim tbl As TableDef
Set tbl = db.TableDefs("Branch")
'Declare a string variable to hold the branch names
Dim strBranchName As String
strBranchName = ""
'Declare a counter for a loop
Dim i As Integer
i = 0
'Open a recordset against the table with names
Dim rst As Recordset
Set rst = tbl.OpenRecordset
With rst
If .RecordCount > 0 Then 'Verify records even exist first!
.MoveFirst 'Moves to the first position
Do While i <= .RecordCount And Not .EOF
strBranchName = rst!BranchName
Dim StrSQL As String
StrSQL = ""
StrSQL = StrSQL & "SELECT Branch.Division, Branch.DivisionName, Branch.Region, Branch.RegionName, Branch.Branch, Branch.BranchName, TY.Period, TY.Year, "
StrSQL = StrSQL & "TY.Account, TY.Ctr, TY.Type, TY.Description, Sum(TY.[Current Period $]) AS [SumOfCurrent Period $], Sum(TY.[To Date $]) AS YTD, "
StrSQL = StrSQL & "Sum(TY.[Current Period Budget $]) AS [SumOfCurrent Period Budget $], Sum(TY.[To Date Budget $]) AS [SumOfTo Date Budget $], "
StrSQL = StrSQL & "Sum(TY.[Current Period Prior Year $]) AS [SumOfCurrent Period Prior Year $], Sum(TY.[To Date Prior Year $]) AS [SumOfTo Date Prior Year $], "
StrSQL = StrSQL & "ChartOfAccounts.ScorecardLine, ChartOfAccounts.Category, ChartOfAccounts.CategoryName, ReportGroup3.ReportGroup3 INTO BranchMasterTbl "
StrSQL = StrSQL & "FROM ((Branch INNER JOIN TY ON Branch.Branch = TY.Div) INNER JOIN ChartOfAccounts ON TY.Account = ChartOfAccounts.Account) "
StrSQL = StrSQL & "INNER JOIN ReportGroup3 ON (ChartOfAccounts.ScorecardLine = ReportGroup3.ScorecardLine) AND (TY.Ctr = ReportGroup3.Ctr) "
StrSQL = StrSQL & "GROUP BY Branch.Division, Branch.DivisionName, Branch.Region, Branch.RegionName, Branch.Branch, Branch.BranchName, TY.Period, TY.Year, "
StrSQL = StrSQL & "TY.Account, TY.Ctr, TY.Type, TY.Description, ChartOfAccounts.ScorecardLine, ChartOfAccounts.Category, "
StrSQL = StrSQL & "ChartOfAccounts.CategoryName, ReportGroup3.ReportGroup3 "
StrSQL = StrSQL & "HAVING (((Branch.BranchName)= " & strBranchName & " ) AND ((ChartOfAccounts.ScorecardLine)>0)) "
StrSQL = StrSQL & "ORDER BY Branch.Branch, TY.Ctr, ChartOfAccounts.ScorecardLine; "
Debug.Print StrSQL
qdf.SQL = StrSQL
DoCmd.SetWarnings False
DoCmd.OpenQuery "qdf"
DoCmd.SetWarnings True
'Output the reports to the H: Drive
DoCmd.OutputTo acReport, strObjectName, strOutputFormat, "H:\Reports\BranchScorecardRprt" & "-" & strBranchName, False, ""
i = i + 1
.MoveNext
Loop
Else
'Do Nothing
End If
End With
Debug.Print strBranchName
End Function