473,466 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Updating mutliple tables through a form

20 New Member
Need a little assistance with a VBA code for updating tables through a form. I have a form with two text boxes and a command button. One text box is named txtNewPartNumber and the other text box is named txtOldPartNumber. The idea is for the user to type in the old part number in the txtOldPartNumber box and type what the new part number is changing to into the txtNewPartNumber box then click on the command button to change the part numbers in 3 different tables. When I set up the VBA code to change the part number in one table, it works just fine, but when I try to and another table, I get the error message shown below. What am I missing in the SQL statement?



Here is the code I am using to update the tables

Private Sub cmdUpdate_Click()

Dim mySQL As String

mySQL = "UPDATE tblPreventiveToolDamageinfo, tblPMTrial"
mySQL = mySQL + " SET tblPMTrial.PartNumber = '" & Me.txtNewPartNumber & "' tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtNewPartNumber & "'"
mySQL = mySQL + " WHERE tblPMTrial.PartNumber = '" & Me.txtOldPartNumber & "' AND tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtOldPartNumber & "'"

DoCmd.RunSQL mySQL
cmbPartNumber.Requery
End Sub

Thanks for your help
Jun 6 '08 #1
9 1649
Annalyzer
122 New Member
I'm not a vastly experienced Access programmer, so when I read this I was really curious. Can you really update 3 tables at one time? I would have just run 3 separate update queries:
mySQL = "UPDATE tblPreventiveToolDamageinfo SET tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtNewPartNumber & " WHERE tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtOldPartNumber & "'"
DoCmd.RunSQL mySQL

mySQL = "UPDATE tblPMTrial SET tblPMTrial.PartNumber = '" & Me.txtNewPartNumber & " WHERE tblPMTrial.PartNumber = '" & Me.txtOldPartNumber & "'"
DoCmd.RunSQL mySQL

mySQL = "UPDATE tblTable3 SET tblTable3.PartNumber = '" & Me.txtNewPartNumber & " WHERE tblTable3.PartNumber = '" & Me.txtOldPartNumber & "'"
DoCmd.RunSQL mySQL
Not a very elegant solution, granted, but I'm not yet a very elegant programmer. =)
Jun 7 '08 #2
Steve67
20 New Member
I am pretty sure that you can update more then one table at once, but it takes knowing what the right code would be, which is where I am stuck at..lol. If I can update 3 tables at once using an update query, I would think I could do the same in VBE with the right code using an Click event on a commond button.
Jun 9 '08 #3
Annalyzer
122 New Member
This got me curious and I just couldn't leave it alone. I read everything I could about updating multiple tables and I think I see a problem. Don't you need to separate the SET commands? Try this:

mySQL = "UPDATE tblPreventiveToolDamageinfo, tblPMTrial"
mySQL = mySQL + " SET tblPMTrial.PartNumber = '" & Me.txtNewPartNumber & "', tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtNewPartNumber & "'"
mySQL = mySQL + " WHERE tblPMTrial.PartNumber = '" & Me.txtOldPartNumber & "' AND tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtOldPartNumber & "'"

Hope this works!
Jun 10 '08 #4
Steve67
20 New Member
Thought that might have been the problem also not seperating them, but I tried seperating them, but still didn't solve the problem. Then again codes like this are so finiky I could have placed the comma wrong or had and extra space.. Will give it another try
Jun 10 '08 #5
Steve67
20 New Member
I think I figured it out and was able to make a very slight change to the code and get all 3 tables updated with the use of one statement. Here is what I did. I went back and seperated the "SET" statements with commas. I tried this before and it didn't work, but for giggles I decide to try it again. Well the seperating the "SET" statements with commas didn't fix the problem. Next I decided to change the "AND" in the "WHERE statement from being all capitalized to just the first letter being capitalized "And." I ran the code and it worked. All three tables updated with the new part number. Here is the code that I used:
Expand|Select|Wrap|Line Numbers
  1. Dim mySQL As String
  2.  
  3. mySQL = "UPDATE tblPreventiveToolDamageinfo, tblPMTrial, tblPreventiveToolDamageLog"
  4. mySQL = mySQL + " SET tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtNewPartNumber & "', tblPMTrial.PartNumber = '" & Me.txtNewPartNumber & "', tblPreventiveToolDamageLog.PartNumber = '" & Me.txtNewPartNumber & "'"
  5. mySQL = mySQL + " WHERE tblPreventiveToolDamageinfo.PartNumber = '" & Me.txtOldPartNumber & "' And tblPMTrial.PartNumber = '" & Me.txtOldPartNumber & "' And tblPreventiveToolDamageLog.PartNumber = '" & Me.txtOldPartNumber & "'"
  6.  
  7. DoCmd.RunSQL mySQL
  8. cmbPartNumber.Requery
Jun 10 '08 #6
Annalyzer
122 New Member
Bizarre. I've never heard of Access SQL being case sensitive. I'm glad you got it working. Now, I can't wait to modify a couple of my databases to make use of this.
Jun 11 '08 #7
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. Access is most definitely not case sensitive when it comes to the use of keywords - I wonder if there is a simpler explanation, such as an accidental additional space typed into one of the textboxes preventing the WHERE clause from matching? It has nothing at all to do with the case of the keywords - you can check this easily in the query builder using SQL View and try any form of case substitution you like - it won't make any difference.

There is no need to go through any existing queries changing the case of ANDs and so on...
-Stewart
Jun 11 '08 #8
Steve67
20 New Member
I went back and changed the code back to using AND and it still worked, so I guess you are right on the case sensitive thing, but not sure what happened that made it work now and not work before...lol. It it working, so I am not going to lose anymore sleep over it..lol.
Jun 11 '08 #9
NeoPa
32,556 Recognized Expert Moderator MVP
@Annalyser UPDATE queries can include tables individually, or they can refer to any "updatable" recordset. Check the Help system for all the details, but generally a recordset is updatable if the tables are linked exclusively with INNER JOINs.

PS. Case is not sensitive in SQL as Stewart explains, but it is nevertheless good policy to capitalise all SQL reserved words. This is not for the benefit of the SQL processor, but rather for your own and anyone having to read it. It is likewise good policy to start each SQL clause on a separate line. You will definitely benefit from this over time ;)
Jun 12 '08 #10

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

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
3
by: LouD | last post by:
I have 5 tables in my db and when i bring up the linked form I get my product lists Tbls are tbStock, which contain unitcost, sellprice n qty tblProduct, contains ID, medicalname n genericname...
1
by: gaosul | last post by:
I am non-programming scientist and I am using a Program called Easyarticles from Synaptosoft Inc., which is based the database program Access. Unfortunately, the owner of this company has...
3
by: Doug | last post by:
Scenario: I select data from a SQL Server View which links 3 tables, into a single dataset table. I update some of those fields on a web form. When I want to update the db, clearly I can't update...
1
by: Robert | last post by:
I sent this last week, but am unable to go that far back in the newsgroup to read the answer, so I'm posting again: I'm using the Jet.Oledb to connect to an Access 2002 database. I have a form...
10
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1)...
4
by: Geoff | last post by:
Hi I'm hoping somebody can help me with the following problem that has occurred to me. Suppose I have two tables in an SQL Server database. Let's call these tables A and B. Assume that A has...
1
by: rdemyan via AccessMonster.com | last post by:
My App has 10 or so tables that we provide that contains proprietary data. This data will need to be updated once or twice a year. I would like some comments, suggestions on my proposed strategy...
2
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.