Hi all,
I feel I'm on the brink of fixing this one myself but seem to be meeting myself coming back and going backwards in coming forwards... Trying to remind myself that 7 months ago I'd never even HEARD of SQL or VBA, let along used them...
I have a report based on a query which is essentially a filtered temporary table.
I'm trying to add further fields into the temporary table, BASED on the data in the table. I'm getting all sorts of errors as I'm amending the SQL and VBA to get it to what I need.
In a bit more detail, after the temporary table is populated with performance measures, I want to use the SQL method of ranking each individual performance measure, then add the results to the temporary table as a new field.
Here's what I've got so far (apologies for the messy code - I will tidy it up once I get it working!). First is the run query and create table bit, which calls the first performance measure function:
Then the function to rank and add the results to the newly created table:
the SQL I need to correct reads as:
Running that as a seperate query gives me 'You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause.'
Then my forehead hits the table...
I'm thinking I've got to specify that the EmployeeNo in the new SELECT query joins with the EmployeeNo in the Temp Table... but getting all sorts of syntax errors trying to do that... any help would be greatly appreciated!
I feel I'm on the brink of fixing this one myself but seem to be meeting myself coming back and going backwards in coming forwards... Trying to remind myself that 7 months ago I'd never even HEARD of SQL or VBA, let along used them...
I have a report based on a query which is essentially a filtered temporary table.
I'm trying to add further fields into the temporary table, BASED on the data in the table. I'm getting all sorts of errors as I'm amending the SQL and VBA to get it to what I need.
In a bit more detail, after the temporary table is populated with performance measures, I want to use the SQL method of ranking each individual performance measure, then add the results to the temporary table as a new field.
Here's what I've got so far (apologies for the messy code - I will tidy it up once I get it working!). First is the run query and create table bit, which calls the first performance measure function:
Code:
Private Sub btnKPIReport1_Click()
Dim strSQL As String
Dim db As Database
Dim rs As DAO.Recordset
Dim strTbl As String
Set db = CurrentDb
strTbl = "tblTempKPI1"
strSQL = "SELECT * INTO " & strTbl & " FROM qry_AllFilterKPI1;"
On Error GoTo ErrHandler
DoCmd.DeleteObject acTable, strTbl
DoCmd.OpenQuery "qry_AllFilterKPI1"
DoCmd.RunSQL strSQL
Call KPIRank("AHT")
DoCmd.Close acQuery, "qry_AllFilterKPI1", acSaveNo
ErrHandler:
Debug.Print Err.Number
If Err.Number = 7874 Then Resume Next
If Err.Number <> 7874 Then Exit Sub
End Sub
Then the function to rank and add the results to the newly created table:
Code:
Private Sub KPIRank(ByVal Field As String)
Dim strSQLRank As String
Dim strSQLField As String
Dim strSQLAddRank As String
Dim db As Database
Dim strTbl As String
Set db = CurrentDb
strTbl = "tblTempKPI1"
strSQLField = "ALTER TABLE " & strTbl & " ADD COLUMN " & Field & "Rank NUMBER;"
DoCmd.RunSQL strSQLField
strSQLRank = "UPDATE " & strTbl & " SET " & Field & "Rank = (SELECT [Rnk1].[EmployeeNo], [Rnk1].[" & Field & "], Count([Rnk2].[EmployeeNo])+1 AS " & Field & "Rank " _
& "FROM qry_AllFilterKPI1 AS Rnk1 LEFT JOIN qry_AllFilterKPI1 AS Rnk2 " _
& "ON [Rnk2].[" & Field & "]>[Rnk1].[" & Field & "] " _
& "GROUP BY [Rnk1].[EmployeeNo], [Rnk1].[" & Field & "]);"
Debug.Print strSQLRank
DoCmd.RunSQL strSQLRank
End Sub
the SQL I need to correct reads as:
Code:
UPDATE tblTempKPI1 SET AHTRank = (SELECT [Rnk1].[EmployeeNo], [Rnk1].[AHT], Count([Rnk2].[EmployeeNo])+1 AS AHTRank FROM qry_AllFilterKPI1 AS Rnk1 LEFT JOIN qry_AllFilterKPI1 AS Rnk2 ON [Rnk2].[AHT]>[Rnk1].[AHT] GROUP BY [Rnk1].[EmployeeNo], [Rnk1].[AHT]);
Running that as a seperate query gives me 'You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause.'
Then my forehead hits the table...
I'm thinking I've got to specify that the EmployeeNo in the new SELECT query joins with the EmployeeNo in the Temp Table... but getting all sorts of syntax errors trying to do that... any help would be greatly appreciated!