?
Solved

conditional update query

Posted on 2011-03-22
6
Medium Priority
?
318 Views
Last Modified: 2012-06-27
I have 2 ms access 2003 tables with the following sample data

table a
id      Floater
1      
2    
3
4


table b
id      Floater
1      N
3      Y

Note: there is only one entry for a given id on both tables
          there may be id entries on table a that are not on table b

I am trying to write an update query that will  update the value of floater on table a

 if there is an matching id entry on table b I would like the value of floater on table a to be set to that on table b


If there is no matching id entry on table b I would like the floater value on table a to be set to N

using the example data I would like table a to have the following values after the update query is run


table a
id      Floater
1      N
2      N
3      Y
4      N
0
Comment
Question by:johnnyg123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 35192402
update tableA set floater = nz(Dlookup("Floater", "TableB","Id = " & tableA.Id) , "N")
0
 

Author Comment

by:johnnyg123
ID: 35192573
I have the access query prompts on

when I run the query I get a message saying it is going to update 4 records but then when I click ok
I get an error message that says

Microsoft Office Access can't update all the records in the update query

Microsoft office access didn't update 4 field(s) due to a type conversion failure

0
 

Author Comment

by:johnnyg123
ID: 35192605
I have attached a sample database
test.mdb
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 17

Expert Comment

by:JezWalters
ID: 35192703
You'll need the following, because the id fields are text:

    UPDATE tablea
    SET floater = Nz(DLookup("floater", "tableb", "id = """ & id & """"), "N")

0
 
LVL 26

Expert Comment

by:Nick67
ID: 35193350
@JezWalters is right
String variables need to be surrounded by quotes
But, gazillions of quotes drive me mad and are VERY hard to debug
Chr(34) is a quote mark
Works in VBA and the query editor

This works and is easier to digest
UPDATE tableA
SET tableA.floater = nz(DLookUp("Floater","TableB","Id = " & Chr(34) & tableA.Id & Chr(34)),"N");

Chr(34) & tableA.Id & Chr(34) is your text field id surrounded by quotes
0
 
LVL 17

Accepted Solution

by:
JezWalters earned 2000 total points
ID: 35196884
If you don't like the fact that you have to double each quotation mark when it's inside quotation marks, you could always use apostrophes instead.

Whichever delimiter you use, but bear in mind that you'll also have to double any ocuurences of it in your id values too!
UPDATE tablea
SET floater = Nz(DLookup("floater","tableb","id = '" & id & "'"),"N")

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question