473,396 Members | 1,805 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.

Programatically updating second table based on search results of first table

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 2497
nico5038
3,080 Expert 2GB
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 "DateDeleted" on the records found by the user. Just filter the form to show records with an empty "DateDeleted" 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
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 Expert 2GB
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
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 Expert 2GB
Well described, you're creating a fine systemdescription 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
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.Execute ("UPDATE Contracts SET Bushels Remaining = Bushels Remaining + " & [Totbush] & " Where ContractNo=" & Me.ContractNo)

Thanks for your assistance
Oct 17 '07 #7
nico5038
3,080 Expert 2GB
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: BushelsRemaining

Nic;o)
Oct 17 '07 #8
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 Expert 2GB
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
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...
1
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...
3
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...
2
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...
4
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...
1
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...
14
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...
33
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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.