473,387 Members | 1,789 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,387 software developers and data experts.

Help with update query

I am trying to update a new field in a table [tblTripDetails].[StateID] with the value from a lookup table [tlkpStates.StateID] where [tlkpStates].[State] = [tblTripDetails].[State]. I have been unable to get this to work.
Here are what I've tried:
using Dlookup;
Expand|Select|Wrap|Line Numbers
  1. UPDATE tblTripDetails INNER JOIN tlkpStates ON tblTripDetails.StateID = tlkpStates.StateID SET tblTripDetails.StateID = DLookUp("StateID","tlkpStates","[tlkpStates].[State] = '" & [tblTripDetails].[State] & "'");
and using a select query;
Expand|Select|Wrap|Line Numbers
  1. UPDATE tblTripDetails INNER JOIN tlkpStates ON tblTripDetails.StateID = tlkpStates.StateID SET tblTripDetails.StateID = (SELECT [tlkpStates].[StateID] FROM [tlkpStates] WHERE [tlkpStates].[State] = [tblTripDetails].[State]);
I am not trying to duplicate data storage. After I get the StateID field populated I'll delete the State field and then update a LocationID field the same way.

Thanks for you're help!
Knowlton
Aug 19 '11 #1
8 1194
patjones
931 Expert 512MB
Hi Knowlton,

If you do the INNER JOIN correctly, then the DLookup isn't necessary because the JOIN will have already matched the records up for you. But it looks like you need to do the JOIN based on [State] rather than [StateID], because [StateID] doesn't exist in tblTripDetails yet...so the JOIN will not work. Try this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblTripDetails INNER JOIN tlkpStates ON tblTripDetails.State = tlkpStates.State SET tblTripDetails.StateID = tlkpStates.StateID

Another way of writing this would be:

Expand|Select|Wrap|Line Numbers
  1. UPDATE tblTripDetails 
  2. SET tblTripDetails.StateID = tlkpStates.StateID 
  3. WHERE tblTripDetails.State = tlkpStates.State;

Pat
Aug 19 '11 #2
I copied your SQL into a new query window and viewed the results. It returned the correct number of records but all were blank. This is the same result as I was getting originally. Any ideas what may be wrong?
Thanks again!
Aug 20 '11 #3
NeoPa
32,556 Expert Mod 16PB
You appreciate you're looking at an UPDATE query? What data would you expect to see there?
Aug 21 '11 #4
If you "View" the query from the query window shouldn't it show the records affected and the changes to be made so that you can be sure its what you expected before actually running the query?
Aug 22 '11 #5
NeoPa
32,556 Expert Mod 16PB
Not exactly no. Viewing an UPDATE query shows you only the fields (field in your case) which are due to be changed. The values shown are the existing values for that field. If I'm not mistaken (as you describe it as a new field) I would expect the data to show as blanks when you look at it.
Aug 22 '11 #6
Thanks for your help and explanations! After you questioned what I expected to see I ran the query and it updated the table corectly.
Knowlton
Aug 22 '11 #7
NeoPa
32,556 Expert Mod 16PB
I knew that because I've fallen over it myself in the past. It's a bit counter-intuitive, but it sort of makes sense (if only to MS).
Aug 23 '11 #8
patjones
931 Expert 512MB
I'm glad that everything worked out okay. When I first started working with SQL I had some problems with the concept of UPDATEing joins. Let us know if there are any other issues.
Aug 23 '11 #9

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

Similar topics

0
by: bwalke | last post by:
I am trying to update data in a MS Access table with data from a SQL 2000 table connected by linked servers. Here are the update statements: This one Works\\ Update OpenQuery(MII, 'Select *...
4
by: rdraider | last post by:
I am looking for some assistance with an update query that needs to link 3 tables: This query ran and reported over 230,000 records affected but did not change the field I wanted changed, not...
1
by: Kunal | last post by:
Hi, I need some help on writing an update query to update "UnitsSold" field in Products Table whenever I save a transaction. The transaction may contain several "Subtransactions", one for each...
3
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field...
3
by: Clive Moss | last post by:
Anyone able to help? I want to update a specified number of records that match a criteria. If I use an update query it alters all the records that match - I need to alter only a variable number....
10
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This...
7
by: Dave Hopper | last post by:
Hi I posted a question recently regarding problems I am having getting a value from a list box to use in a query. I got a lot of help, for which I thank you and it's nearly working! But I need...
7
by: Mark Carlyle via AccessMonster.com | last post by:
I have this update query that I am trying to run. I know the syntax is messed up but do not know how to correct it. Select 'UPDATE', Transactions,'Set = where = ' From "Get Daily Balances" ...
2
by: bobabooey2k | last post by:
I have an update query with one field having in its "Update to" cell a DLookup statement. This query takes 2-3 minutes on 3000 records. Can I avoid dlookup here using multiple queries? An...
2
by: Reedsp | last post by:
OS: MS XP Access version: 2003 SP2 I am trying to use an update query to replace quote marks with nothing. In essence, I'm removing quote marks. I get a error message when a field is empty or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.