473,385 Members | 2,210 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,385 software developers and data experts.

How to update a filed with multiple conditions?

I have a table, what I need to do is

if Field 1= a, field 2=b, field 3=c, then field 4 update to xxxx;
iif Field 1= a, field 2=b, field 3=d, then field 4 update to xxxxxx,
if Field 1= z, field 2=e, field 3=g, then field 4 update to xxxxxxxxx,
if Field 1= y, field 2=h, field 3=s, then field 4 update to xxxxxxxxxxx,
so on

I wold like to know how to do it in Access?

If I use Update query in Access, I need to write a lot of queries like:
Update Table Set ... Where .. ; Can Update query add condition?

Can I use VBA or ADO?

Thank you very much for your help !
Sep 11 '07 #1
1 7267
MMcCarthy
14,534 Expert Mod 8TB
You can do this in a query as follows:

Expand|Select|Wrap|Line Numbers
  1. UPDATE TableName SET [Field4]="xxxx"
  2. WHERE [Field1]= a 
  3. AND [Field2]=b, 
  4. [Field3]=c;
However, for better Control you will probably need to use a recordset in VBA with nested IF statements. I am using DAO as I personally prefer it. Something like the following:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim rs As DAO.Recordset
  3.  
  4.    Set db = CurrentDb
  5.    Set rs = db.OpenRecordset("TableName")
  6.  
  7.    rs.MoveFirst
  8.    Do until rs.EOF
  9.       If rs!Field1 = "a" Then
  10.          If rs!Field2 = "b"  And rs!Field3 = "c" Then
  11.             rs.Edit
  12.             rs!Field4 = "xxxxx"
  13.             rs.Update
  14.          Else
  15.             rs.Edit
  16.             rs!Field4 = "xxx"
  17.             rs.Update
  18.          End If
  19.       ElseIf rs!Field1 = "z" Then
  20.          ' and so on
  21.       ElseIf rs!Field1 = "y" Then
  22.          ' and so on
  23.       Else
  24.          ' if none are true
  25.       End If
  26.       rs.MoveNext
  27.    Loop
  28.  
  29.    rs.Close
  30.    Set rs = Nothing
  31.    Set db = Nothing
  32.  
You just need to figure out the logic.
Sep 13 '07 #2

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

Similar topics

1
by: Roy Adams | last post by:
Hello people I've recently been woring with multiple insert from text fields and got that woking fine thanks to the help from people from this forum now i'm trying to deal with multiple update,...
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 *...
6
by: rob | last post by:
Hey, I need to update a row in one table with a row (most but not all columns) from another table. This sounds like there should be a really easy way to do this. I've seen there's FROM in the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
2
by: anony | last post by:
Maybe this feature is already out there. I guess you could write triggers to do some of this. Often when designing a database I add a start_date and end_date column to the table. The start_date...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
1
by: cancer2006 | last post by:
I have to correct the ID field in 19 tables and there are 265 records affected in just one table . The id field is populated on the values from EInfo.ID, and EInfo.ID is based on the values from...
3
by: Yas | last post by:
Hi everyone I am trying to create a DELETE Trigger. I have 2 tables. Table1 and Table2. Table 2 has all the same fields and records as Table1 + 1 extra column "date_removed" I would like that...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.