473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programatically updating second table based on search results of first table

8 New Member
I have an Access 2002 database that has a form that can be used to review individual records. At the bottom of the form are buttons that are linked to functions that allow the user to "Find a record using search criteria", "Delete the current record" and so on. After a user has used the search criteria to find a specific record, I would like to use the "delete" function on the form to not only delete the record, but also update another table. I want to do this programatically rather than developing a parameter query where the user would have to enter the values manually found in the search result. When the delete button is selected it would use the value from one of the fields in the current table's search result as a criteria for updating a different field in another table. The value that is updated in the second table is a calculated value equal to the value found in the field before the update + the value found in a field of the record that is to be deleted.

I have used some macros in the past and have limited understanding of VBA programming.

Thanks in advance for any assistance you can provide.
Oct 15 '07 #1
9 2531
nico5038
3,080 Recognized Expert Specialist
Looks like you're trying to create a dangerous system.
By deleting records that hold a value, no tracing can be done wether or not the value in "the other table" is correct.
Basically that value is deductable from the records, when they wouldn't have been deleted.
The safest way is to "forget" the correction of a field in the other form and to use e.g. a "DateDelete d" on the records found by the user. Just filter the form to show records with an empty "DateDelete d" and for the user it looks like the record has disappeared, but it will allow you to record the user that did the deletion too, incase someone might wonder what happened with some data....
To get the needed summation, just create a GroupBy query to Sum all "Deleted" rows.

Nic;o)
Oct 15 '07 #2
hrreece
8 New Member
Thanks for the quick reply. The delete function is only used when an incorrect record has been entered. The way the program is currently set up is that once a record is recorded a report is printed automatically showing the recorded information. On occassion there is a need to cleanse the master table of an incorrect record. Since one of the fields in the master table is related to another table, I must update a field in the second table to reflect the deletion of the record in the main table. I have been unable to write an update query for the second table that uses the search results from the first table as the criteria and one of the fields from that query result as a variable that needs to be added back to a field in the second table.
Oct 15 '07 #3
nico5038
3,080 Recognized Expert Specialist
This does imply that you're storing calculated data in a tablefield. Somewhere when the incorrect record is inserted there must be a function that adds (subtracts?) the value to the "field in the other table", else you wouldn't need a correction....

Basically your table design is against the normalization rules and the need for an update here reveals why we normalize tables. When you have a normalized tabledesign, the "field in the other table" wouldn't exist, but would be calculated in a query when needed.

As Access tables can be processed "outside" forms, a direct delete of a row in the table would corrupt your figures.....

Getting the idea ?
Can you still change your tabledesign ?

Nic;o)
Oct 15 '07 #4
hrreece
8 New Member
The application was one that was developed several years ago and I am sure does not follow the best normalization rules throughout the entire database. Over the years there have been add-on's etc that also too have probably moved the original design away from the best methods and practices for databases.

With this being said, the users of this application are NOT access literate and use the forms and functions embedded in them to run the application to track customer transactions. The transaction table (Transmas) includes various fields with one of the fields being a "contract number" (ContractNo). Not all transactions have a contract number, but when one does the price for an item is not pulled from a regular price schedule located in a separate table but from a price field located in the contract table for the item as long as the quantity purchased does not exceed the allowed "running" total for the contract. Once a customer purchases an amount equal to what was originally contracted, the price for subsequent purchased items reverts to the regular price. In a separate table (Contracts), details can be found in fields for the contract such as the "Contract Number" (ContractNo), "Price" (ContractPrice) , "Initial Contracted Quantity" and "Contract Quantity left" (ContractRemain ). The only like fields between the two tables is Contract Number (ContractNo) which can be found in both the Transmas and the Contract tables. When a customer purchases an item using the contract price, the quantity of that item is deducted from Contract Quantity Left field located in the Contract table. This field represents a running total for the contract and is the field that the Transmas table "looks-up" when determing what price to use for the transaction.

When an data entry person notices that a mistake has been made after a transaction is recorded, they need to have a way to update the (ContractRemain ) field in the Contract table. Presently they use the delete function to remove the incorrect record from the Transmas table after searching the Transmas table. They then must remember to add back the number of items purchased in the deleted transaction to the (ContractRemain ) field in the Contract table to match the value before the incorrect transaction was added. My desire was to build a function, macro etc that as part of the delete step would call up the appropriate record in the Contract table and then automatically add back to the specific contract a revised ContractRemain value via a calculated field.

Hopefully, this provides additional detail.
Oct 16 '07 #5
nico5038
3,080 Recognized Expert Specialist
Well described, you're creating a fine systemdescripti on this way :-)

For changing the value in the other table we'll need an UPDATE query.
Syntax:
Expand|Select|Wrap|Line Numbers
  1. UPDATE <tablename> SET <fieldname> = <new value>
  2.  
Best to add the needed code "behind" the [Delete] button on the form as you'll have to use the value before deleting the record.
The code would look like:
Expand|Select|Wrap|Line Numbers
  1. ' correct the value
  2. currentdb.execute ("UPDATE Contract SET ContractRemain = ContractRemain 
  3. - " & [number of items purchased] & " WHERE ContractNo=" & Me.ContractNo)
  4. ' delete the record
  5. currentdb.execute ("DELETE * FROM Transmas WHERE ContractNo=" & Me.ContractNo)
  6. ' show it
  7. Me.Requery
  8.  
Best to make first a copy of the database for testing ! Also make sure that [number of items purchased] holds the correct fieldname.

Nic;o)
Oct 16 '07 #6
hrreece
8 New Member
We are getting so close to success I can taste it. When I try to run the following update statement before the delete command I get a message from Microsoft access that there is "a syntax error in the update statement". I have looked at the code several times and can not find the error. Please note that the field names shown in the attached are not the same as in earlier posts, but are indeed the correct field names used in the tables/

CurrentDb.Execu te ("UPDATE Contracts SET Bushels Remaining = Bushels Remaining + " & [Totbush] & " Where ContractNo=" & Me.ContractNo)

Thanks for your assistance
Oct 17 '07 #7
nico5038
3,080 Recognized Expert Specialist
Your fieldname has an embedded space, thus [ and ] is mandatory like:
Expand|Select|Wrap|Line Numbers
  1. CurrentDb.Execute ("UPDATE Contracts SET [Bushels Remaining] = [Bushels Remaining] + " & [Totbush] & " Where ContractNo=" & Me.ContractNo)
  2.  
Guess you know now why I NEVER use spaces in fieldnames, just capitalization works visually the same: BushelsRemainin g

Nic;o)
Oct 17 '07 #8
hrreece
8 New Member
Thank you. The code worked perfectly. I have also learned some valuable tips that will help me in the future. I can now quit pulling my hair out for at least awhile!!!

Roger
Oct 17 '07 #9
nico5038
3,080 Recognized Expert Specialist
Glad I could help before you turned bold :-)

Success with your application !

Nic;o)
Oct 18 '07 #10

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

Similar topics

5
1718
by: J. Yuan | last post by:
Hi, I am working on a checkout/inventory system. How can I make a button that when pressed, would update the previous fields transaction number to a table (for example, -3 printers, so that printer total is now 20 instead of 23) while also logging the transaction in another table (for example, logging the date and -3 printers). I have another question, while a value is selected by a combo box, how can I retrieve it? I mean, I want...
1
1869
by: Chris Jackson | last post by:
I'm a novice Access user and am not sure how to solve the following problem. Any help with the following would be greatly appreciated! I have two tables with identical structures, the first holds the data input for a questionnaire, the second holds the scores from the questionnaire, to move the scores I've set up 2 action queries, the first appends the record to the second table, the second action query 'updates' the results by...
3
4338
by: Katy | last post by:
In my project I have two tables with the same structure and second table's data is a part of the first one. (one is the main and the second is used to keep the results of filters performed on the first one while doing search command. Each table has a lot of fields while one of them is OLE Object and used to keep the picture object (embedded not linked). By updating this OLE Object on the "filtered" table, appropriate record on the main...
2
2408
by: Ray Holtz | last post by:
I have a form that shows a single record based on a query criteria. When I click a button it is set to use an append query to copy that record to a separate table, then deletes the record from the first table. Both tables have a 'DateTime' field that shows when it was last updated using the Now function. What I am trying to do is get the 'DateTime' field updated after the append query copies it to the new table. I have tried updating...
4
2027
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a datagrid isn't going to work). On this page, people can update a variet of records. On submit, I want to then go in and update all of the records. Normally, I'd make each form element include a runat: server and then declare it in my codebhind so I...
1
4345
by: sekisho | last post by:
I'm dynamically adding a column of labels and a column of text boxes to a panel on a webform, based on data returned from an SQL query, which the user builds by selecting options from a few dropdowns and hitting the 'Search' button. The first column returned is a user name and is added to the panel as a label, the second is a number and is added to the label as the text property of a textbox, so the user can update it. When the...
14
2964
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will utilise the disco file to update the Corresponding proxy file and reflect the changes made to the web service. However, the results of doing this with out params is that the results seem
33
3356
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range of 20 steps) On the admin page the steps are listed, in "order" order and the user can create new steps and assign an order and all is well. The problem may come in using a renumber function which should take the steps in their current order...
1
7553
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.