473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating an Update Append Query from a button

Hi guys,

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
  1. Private Sub UpdateAppend_Click()
  2. On Error GoTo AddNewRecord
  3.  
  4.  
  5. 'Update the column Previous Ranking in the table
  6. strSQL = "UPDATE tbl_[Temp - League Table Previous Rank] SET " & _
  7.                     "PreviousRanking =  & "[Ranking], "
  8.                     WHERE
  9. 'This part searches for whether the record is already there based on Town and Address
  10.                     "([Reporting - League Table (Elec) 2].[Town])=" & [Town] & ") AND (" & _
  11.                     "([Reporting - League Table (Elec) 2].[Address])=" & [Address] & ") ;"
  12.  
  13. DoCmd.SetWarnings (False)
  14. DoCmd.RunSQL strSQL
  15. DoCmd.SetWarnings (True)
  16. Exit Sub
  17.  
  18. 'If the record isn't there, the query will add it
  19. AddNewRecord:
  20. INSERT INTO tbl.[Temp - League Table Previous Rank],
  21. Values ([Ranking],[Town],[Supplies],[Address],[Date Changed]) ,
  22. 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;
  23.  
  24. DoCmd.SetWarnings (False)
  25. DoCmd.SetWarnings (True)
  26.  
  27.  
  28. End Sub
Apr 30 '14 #1
1 1460
zmbd
5,501 Expert Mod 4TB
Now I have done the ranking part and created a table to store the previous ranking
Does your data have a date/time field associated with it?
If so, then set one query to base the ranking on time period one and the second on time period two; thus, you do not need the VBA nor storage table. This is my number one suggestion!



Looking at your code:
"[Reporting - League Table (Elec) 2]"
Is a poor field name due to the dash and the parenthesis
-It is best practice when naming fields, tables, and files to avoid the use of anything other than alphanumeric characters and the underscore (spaces although allowed are problematic from a programing point of view and best avoided) and it is VERY importaint to avoid all reserved words and tokens:
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access

Your first SQL string has quite a few typos - this fixes those:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbl_[Temp - League Table Previous Rank] SET " & _
  2.                     "PreviousRanking =  " & [Ranking] & ", " & _
  3.                     "WHERE " & _
  4.                     "([Reporting - League Table (Elec) 2].[Town])=" & [Town] & ") AND (" & _
  5.                     "([Reporting - League Table (Elec) 2].[Address])=" & [Address] & ") ;"
  6.  
I suggest you take another look at your second block of SQL for the same types of errors.


Next
but it doesn't seem to be working! Can someone kindly point me in the right direction?
You've told us what you want to happen, which is much better than most posts ( d(^_^)b ); however, you've not told us about any errors or how it's not working; however, I'm sure that the typos in your SQL is certainly one major issue.
You should take a look at the basic troubleshooting found here:[*]> Before Posting (VBA or SQL) Code


--
If you insist on the table then I would look the execute method:
For an example of how to use this method; read thru the post: (How do I show just ONE warning while running multiple delete queries : Post#6 so that you have an idea of what you are looking at then open the vba editor.

If you would like to go this route (and I highly recommend this method if you insist on storing your past results), then post back any questions.
Apr 30 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi - I am trying to perform a simple append query, with no luck. I have a table (MktPrices) that has the following fields: BondID, PriceDate, Price. The objective is to allow the user to input a...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
6
by: dd_bdlm | last post by:
Hi all I have a database that stores customer records and their associated insurance details. I need to be able to archive any changes made to that record. Currently the user selects 'record...
13
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
2
by: Pete | last post by:
After realizing the integrity of my data is questionable I went back to my update queries and opened up a few. The joins I had originally created between key fields were no longer there. It was...
0
by: jon | last post by:
Hi there, I'm brand new to Access and may be trying to do too much too soon, but I wanted to get some expert advice on how the best way to go about what I am trying to accomplish would be. I...
11
by: kabradley | last post by:
Hello Everyone, So, thanks to nico's help I was finally able to 'finish' our companies access database. For the past week or so though,I have been designing forms that contain a subform and an...
0
by: halwiindims | last post by:
Hi all, I want to append a new table distributors with an old table distributors2 in my SQL DB. I need your help.
1
by: rebexter | last post by:
I have a list of about 58,000 records which I created by merging numerous smaller lists, each of which contain a certain keyword. What I want to do now is update the master list of 58,000...
4
by: Adam Tippelt | last post by:
Situation: When a user 'confirms' that they want to save the information they've inputted, I need to append the database records from a temporary table to a different table. Problem: The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.