ThriftyFinanceGirl said:
Thought I would post my code that isn't working...
Dim db As Database
Dim Rst As Recordset
Dim NumRecords As Long
Dim SQL As String
Set db = CurrentDb()
SQL = "SELECT Count(TaxRatesBases.TaxID) AS NumRecord" & _
"FROM (Location LEFT JOIN TaxRatesBases ON Location.BusUnit =
TaxRatesBases.BusUnit) LEFT JOIN LocationObjects ON TaxRatesBases.TaxID =
LocationObjects.TaxID" & _
"GROUP BY TaxRatesBases.BusUnit, LocationObjects.ObjectAcctNo"
& _
"HAVING
(((TaxRatesBases.BusUnit)=[Forms]![frmlocation]![BusUnit]) AND
((LocationObjects.ObjectAcctNo) Is Null));"
The way you're building the SQL string, there are no spaces between various
words where they need to be. Also, when you use DAO, references to form
controls -- such as your [Forms]![frmlocation]![BusUnit] -- aren't resolved
automatically. You have to fill them in as you build the SQL string.
Set Rst = db.OpenRecordset("SQL")
Also, you're passing the literal string "SQL" to OpenRecordset, rather than
the variable you named SQL.
Try this:
SQL = _
"SELECT Count(TaxRatesBases.TaxID) AS NumRecord " & _
"FROM (Location LEFT JOIN TaxRatesBases " & _
"ON Location.BusUnit = TaxRatesBases.BusUnit) " & _
"LEFT JOIN LocationObjects " & _
"ON TaxRatesBases.TaxID = LocationObjects.TaxID " & _
"GROUP BY TaxRatesBases.BusUnit, LocationObjects.ObjectAcctNo " & _
"HAVING (((TaxRatesBases.BusUnit)='" & _
[Forms]![frmlocation]![BusUnit] & _
"') AND ((LocationObjects.ObjectAcctNo) Is Null));"
Set Rst = db.OpenRecordset(SQL)
I'm not vouching for the validity of the SQL statement, but the above should
correct the syntax errors. It assumes that BusUnit is a text field. If
not, change the last three lines to:
"HAVING (((TaxRatesBases.BusUnit)=" & _
[Forms]![frmlocation]![BusUnit] & _
") AND ((LocationObjects.ObjectAcctNo) Is Null));"