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

Repeat an update query for multiple fields in a single table

I have a table with 13 fields of information, filed 1 is the item name, and the remaining 12 fields are numerical information about that item. Some of the data in the numerical fields contain undesirable values (eg: "-2", and "-3")....

I'm new to Access, but i can see how to write an update query to update a single field to "0" where "-2" or "-3" is found, but when I tried expanding that query to handle all 12 fields it failed.

I know access can achieve this, I'm just too new to recognize the best way. I'm hoping there is a better way than writing 12 different update querries.

thanks.
Jan 31 '14 #1

✓ answered by ADezii

  1. Here is some compact Code that will check Fields 2 thru 13 of every single Record in a Table named tblData. If a Value of either -2 or -3 is found in any of these Fields, in any Record, it will automatically Update that Value to 0. You need not know any of the Field Names, only that you are processing Fields 2 thru 13.
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database
    2. Dim rst As DAO.Recordset
    3. Dim intFldCtr As Integer
    4.  
    5. Set MyDB = CurrentDb
    6. Set rst = MyDB.OpenRecordset("SELECT * FROM tblData", dbOpenDynaset)
    7.  
    8. With rst
    9.   Do While Not .EOF
    10.     'Skip the 1st Field, only process Fields 2 thru 13
    11.     For intFldCtr = 1 To (.Fields.Count - 1)
    12.       If .Fields(intFldCtr) = -2 Or .Fields(intFldCtr) = -3 Then
    13.         .Edit
    14.           .Fields(intFldCtr) = 0
    15.         .Update
    16.       End If
    17.     Next
    18.       .MoveNext
    19.   Loop
    20. End With
    21.  
    22. rst.Close
    23. Set rst = Nothing
    24.  
  2. tblData before processing:
    Expand|Select|Wrap|Line Numbers
    1. ID     2     3    4    5    6    7    8    9    10    11    12    13
    2. 1     2    -2    4    -2    6    7    -2    9    10    11    12    -3
    3. 2    -3    15   16    17    18    19    20    -3    22    -3    24    -3
    4.  
  3. tblData after Code Execution:
    Expand|Select|Wrap|Line Numbers
    1. ID    2    3    4    5    6    7    8    9    10    11    12    13
    2. 1    2    0    4    0    6    7    0    9    10    11    12    0
    3. 2    0    15    16    17    18    19    20    0    22    0    24    0
    4.  
  4. Kindly forgive the poor formatting. I did not have time to format the Table Values properly.

8 3072
zmbd
5,501 Expert Mod 4TB
1) Check out normalzation > Database Normalization and Table Structures.

2) Please click on the [CODE/] button above and paste your SQL between the [code] [/code] tags so that we can review it.
Jan 31 '14 #2
Expand|Select|Wrap|Line Numbers
  1. UPDATE [MR-FUNCTION_Results]
  2. SET [MR-FUNCTION_Results].[1CDF-E12-5 RAW] = 0
  3.    , [MR-FUNCTION_Results].[1CDFSEQS RG RAW] = 0
  4. WHERE ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-2)
  5.    AND (([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-2)) 
  6.    OR ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-3) 
  7.    AND (([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-3));
Jan 31 '14 #3
zmbd
5,501 Expert Mod 4TB
Here is the lesson on fishing first:
Create a simple select query, use the wizard it's fast and easy.
In this query, make sure that only the records you want to effect are returned. Once that is done, then convert to the update action query.

So for the fish:
The way your query is written:
BOTH fields [1CDF-E12-5 RAW] AND [MR-FUNCTION_Results] must = -2
OR
BOTH fields [1CDF-E12-5 RAW] AND [MR-FUNCTION_Results] must = -3
within the same record in order to be returned within the select query (or effected in the update)

However, it sounds as if you want
field [1CDF-E12-5 RAW] when it = -2 or -3
or when
field [MR-FUNCTION_Results] when it = -2 or -3
to be eventually

Then you need a query like this for the select:
Expand|Select|Wrap|Line Numbers
  1. SELECT [MR-FUNCTION_Results].[1CDF-E12-5 RAW]
  2.    , [MR-FUNCTION_Results].[1CDFSEQS RG RAW]
  3. FROM [MR-FUNCTION_Results]
  4. WHERE ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-2)) 
  5.    OR ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-3))
  6.    OR ((([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-2)) 
  7.    OR ((([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-3));
This then converts to the update query:
Expand|Select|Wrap|Line Numbers
  1. UPDATE [MR-FUNCTION_Results] 
  2. SET [MR-FUNCTION_Results].[1CDF-E12-5 RAW] = 0
  3.    , [MR-FUNCTION_Results].[1CDFSEQS RG RAW] = 0
  4. WHERE ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-2))
  5.    OR ((([MR-FUNCTION_Results].[1CDF-E12-5 RAW])=-3)) 
  6.    OR ((([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-2)) 
  7.    OR ((([MR-FUNCTION_Results].[1CDFSEQS RG RAW])=-3));
(this SQL has been vetted in ACC2010)

Additionally I highly advise you to remove the dash/subtraction sign from your names "-" this is a reserved token and can cause you issues.
Access 2007 reserved words and symbols
AllenBrowne- Problem names and reserved words in Access
Jan 31 '14 #4
ADezii
8,834 Expert 8TB
  1. Here is some compact Code that will check Fields 2 thru 13 of every single Record in a Table named tblData. If a Value of either -2 or -3 is found in any of these Fields, in any Record, it will automatically Update that Value to 0. You need not know any of the Field Names, only that you are processing Fields 2 thru 13.
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database
    2. Dim rst As DAO.Recordset
    3. Dim intFldCtr As Integer
    4.  
    5. Set MyDB = CurrentDb
    6. Set rst = MyDB.OpenRecordset("SELECT * FROM tblData", dbOpenDynaset)
    7.  
    8. With rst
    9.   Do While Not .EOF
    10.     'Skip the 1st Field, only process Fields 2 thru 13
    11.     For intFldCtr = 1 To (.Fields.Count - 1)
    12.       If .Fields(intFldCtr) = -2 Or .Fields(intFldCtr) = -3 Then
    13.         .Edit
    14.           .Fields(intFldCtr) = 0
    15.         .Update
    16.       End If
    17.     Next
    18.       .MoveNext
    19.   Loop
    20. End With
    21.  
    22. rst.Close
    23. Set rst = Nothing
    24.  
  2. tblData before processing:
    Expand|Select|Wrap|Line Numbers
    1. ID     2     3    4    5    6    7    8    9    10    11    12    13
    2. 1     2    -2    4    -2    6    7    -2    9    10    11    12    -3
    3. 2    -3    15   16    17    18    19    20    -3    22    -3    24    -3
    4.  
  3. tblData after Code Execution:
    Expand|Select|Wrap|Line Numbers
    1. ID    2    3    4    5    6    7    8    9    10    11    12    13
    2. 1    2    0    4    0    6    7    0    9    10    11    12    0
    3. 2    0    15    16    17    18    19    20    0    22    0    24    0
    4.  
  4. Kindly forgive the poor formatting. I did not have time to format the Table Values properly.
Feb 1 '14 #5
Thanks for the tip about using the "-" is names - I did not know that this would be a reserved token.

Is your recomendation for table names, or also for field names within a table?

Also, does using the underscore "_" have the same implicaitons?

thanks
Feb 3 '14 #6
@PRANerd
You request that i use code tags in the future - what is this? (did i mention i was new to Access?)
Feb 7 '14 #7
zmbd
5,501 Expert Mod 4TB
Fully explained here:
> Before Posting (VBA or SQL) Code :: Section B paragraph 2

This is a good page to read in its entirety as it covers some basic troubleshooting that can save you time if followed prior to posting questions (^_^)
Feb 7 '14 #8
@zmbd
Awesome, will do. Thanks for the tip!
Feb 7 '14 #9

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

Similar topics

0
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
4
by: 663scott | last post by:
Hi I have the following table in a database: Name: Table 1 Field 1: Account ID Field 2: FILM ID 1 Field 3: FILM ID 2 Field 4: FILM ID 3
0
by: Howie | last post by:
Hi all. Hope you can help. I occasionally need to enter info directly into a table. However, when I do, I often need to enter the same value into a large number of fields. It would be nice if...
3
GrandMaster
by: GrandMaster | last post by:
Hi all I'm using MS Access 97 and in my current database (a flat single table database) I have a field for FirstName and one for FamilyName. I've been given two different lots of data to import...
1
by: sridhar4554 | last post by:
How to give a grant like select,update,insert on a single table i had tried with the command grant eg:GRANT SELECT,INSERT,UPDATE ON DTS_OUTBOX_QUEUE TO sanpapps@dap1 IDENTIFIED BY 'disable';...
3
by: Bigdaddrock | last post by:
I have a form that allows for input of new Customer Billing address fields (B1, B2, B3) as well as fields for the Shipping Address (S1, S2, S3). Sometimes these are the exact same! I have text...
0
by: Alexis Ellis | last post by:
I am using adodb in python to connect to a database and run sql queries. I run several update queries in a table. Then I do a 'Select Into' to copy the table to a new database. All of the updates...
2
by: peterbata | last post by:
Table looks like this: custno custname qty active banner My simple query so far looks like this: /****** Script for SelectTopNRows command from SSMS ******/ SELECT ALL , ,
3
by: M0ji | last post by:
I have created an update query to auto populate the value of one field based on the value of another field. I have two fields "Level" and "Document" if Document is A, Level needs to be 1 if...
1
by: ncsthbell | last post by:
Our business is changing the current employee UserId's to a new value. I am trying to build a query that will read the current user table, find the matching row in the new user table & update the...
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
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
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...
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,...

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.