I recently made some unrelated changes to my db and some code I had to update counts in a table worked perfectly but now doesn't work. Err.description tells me it's a syntax error and it occurs right after the SQL for counting distinct employee IDs. I have tried everything I know to figure out what it doesn't like and have failed. The code loops through a table with queries listed and first gets a DCount for each query and writes to the table because I don't need distinct-this still works. It then loops through the same queries and gets a distinct count- this section now fails. This code used to work perfectly until I added some additional tables which the list of queries pull data from. If someone has some insight, it would be much appreciated. Code is below:
Dim rstQueryList As DAO.Recordset
Dim rstCountData As DAO.Recordset
Dim strSQLQueryList As String
Dim strQueryName As String
Dim strSQLQueryData As String
Dim RecordCount As Integer
Dim strSQL As String
Dim qdf As QueryDef
strSQLQueryList = "SELECT * FROM tblQueries;"
Set db = CurrentDb()
Set rstQueryList = db.OpenRecordset(strSQLQueryList)
rstQueryList.MoveFirst
Do While Not rstQueryList.EOF 'loops through the list of queries
strQueryName = rstQueryList!Query
rstQueryList.Edit
rstQueryList!RecCnt = DCount("*", strQueryName) 'write the record count count to the table
rstQueryList.Update
rstQueryList.MoveNext
Loop
'count distinct affected employees
rstQueryList.MoveFirst
Do While Not rstQueryList.EOF 'loops through the list of queries
strQueryName = rstQueryList!Query
strSQL = "SELECT Count([Employee ID]) AS AffEmps FROM (SELECT DISTINCT [Employee ID] FROM " & strQueryName & ");"
Set rstCountData = db.OpenRecordset(strSQL)
MsgBox Err.Description 'The error seems to happen here
'MsgBox strQueryName & " = " & rstCountData!AffEmps
rstQueryList.Edit
rstQueryList!AffEmpCnt = rstCountData!AffEmps 'write the number of affected employees to the table
rstQueryList.Update
rstCountData.Close
rstQueryList.MoveNext
Loop
rstQueryList.Close
rstCountData.Close
Me.Requery
db.Close
DoCmd.SetWarnings True
DoCmd.Hourglass False
MsgBox "Download successfully imported.", vbExclamation, "Success!"
Dim rstQueryList As DAO.Recordset
Dim rstCountData As DAO.Recordset
Dim strSQLQueryList As String
Dim strQueryName As String
Dim strSQLQueryData As String
Dim RecordCount As Integer
Dim strSQL As String
Dim qdf As QueryDef
strSQLQueryList = "SELECT * FROM tblQueries;"
Set db = CurrentDb()
Set rstQueryList = db.OpenRecordset(strSQLQueryList)
rstQueryList.MoveFirst
Do While Not rstQueryList.EOF 'loops through the list of queries
strQueryName = rstQueryList!Query
rstQueryList.Edit
rstQueryList!RecCnt = DCount("*", strQueryName) 'write the record count count to the table
rstQueryList.Update
rstQueryList.MoveNext
Loop
'count distinct affected employees
rstQueryList.MoveFirst
Do While Not rstQueryList.EOF 'loops through the list of queries
strQueryName = rstQueryList!Query
strSQL = "SELECT Count([Employee ID]) AS AffEmps FROM (SELECT DISTINCT [Employee ID] FROM " & strQueryName & ");"
Set rstCountData = db.OpenRecordset(strSQL)
MsgBox Err.Description 'The error seems to happen here
'MsgBox strQueryName & " = " & rstCountData!AffEmps
rstQueryList.Edit
rstQueryList!AffEmpCnt = rstCountData!AffEmps 'write the number of affected employees to the table
rstQueryList.Update
rstCountData.Close
rstQueryList.MoveNext
Loop
rstQueryList.Close
rstCountData.Close
Me.Requery
db.Close
DoCmd.SetWarnings True
DoCmd.Hourglass False
MsgBox "Download successfully imported.", vbExclamation, "Success!"