Hopefully I will explain what I've been trying to do properly! I have a query that creates a ranking for stores energy usage in different areas. Someone has asked me to include the previous ranking so they can check for improvements etc.
Now I have done the ranking part and created a table to store the previous ranking but I am struggling creating a query that updates the table. Ideally I would like it to check whether a store is there, if so update the previous ranking column and if not add the store. At the moment I am trying to be clever and do this from a button but it doesn't seem to be working! Can someone kindly point me in the right direction?
Expand|Select|Wrap|Line Numbers
- Private Sub UpdateAppend_Click()
- On Error GoTo AddNewRecord
- 'Update the column Previous Ranking in the table
- strSQL = "UPDATE tbl_[Temp - League Table Previous Rank] SET " & _
- "PreviousRanking = & "[Ranking], "
- WHERE
- 'This part searches for whether the record is already there based on Town and Address
- "([Reporting - League Table (Elec) 2].[Town])=" & [Town] & ") AND (" & _
- "([Reporting - League Table (Elec) 2].[Address])=" & [Address] & ") ;"
- DoCmd.SetWarnings (False)
- DoCmd.RunSQL strSQL
- DoCmd.SetWarnings (True)
- Exit Sub
- 'If the record isn't there, the query will add it
- AddNewRecord:
- INSERT INTO tbl.[Temp - League Table Previous Rank],
- Values ([Ranking],[Town],[Supplies],[Address],[Date Changed]) ,
- SELECT [Reporting - League Table (Elec) 2].[Ranking] AS [PreviousRanking], [Reporting - League Table (Elec) 2].[Town] AS Town, [Reporting - League Table (Elec) 2].[Supplies] AS Supplies,[Address] AS Address;
- DoCmd.SetWarnings (False)
- DoCmd.SetWarnings (True)
- End Sub