Yes/No - Update 1 record to YES and change all other records to NO (1 Viewer)

StlthBM

New member
Local time
Today, 11:48
Joined
Nov 2, 2021
Messages
5
What is the easiest way to update all records in a table so that when one is selected to YES, all other records are updated to NO?

The table is designed to keep track of statements, with only one statement being the "Endorsed" statement but the other iterations being kept track in the table. At the moment I've built the table and I'm in the process of building the form.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:48
Joined
Oct 29, 2018
Messages
21,453
You should be able to use an UPDATE query to do that. For example,

UPDATE TableName SET FieldName = False WHERE ID <> 1

1 being the ID of the approved record.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:48
Joined
Feb 28, 2001
Messages
27,140
First, decide what is the identifying criterion for the record to become YES. For sake of argument, let's say you know the prime key for the record. If the table is MyTable, the PK field is MyID, and the value in question is in your form's ChosenID control, and the Yes/No indicator is MyYN:

Code:
strSQL = "UPDATE MyTable SET MyYN = ( MyID = " & Cstr( Me.ChosenID ) & " );"
CurrentDB.Execute strSQL, dbFailOnError

This will set every record's MyYN to the truth-value of "MyID = " & CStr( Me.ChosenID ); only one record will match and be YES, others will be NO
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:48
Joined
May 7, 2009
Messages
19,231
you need 2 update query (in case there is previous Yes in a record).

update yourTable set YesNoField = 0
update yourTable set YesNoField =-1 Where [ID] = 99
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 22:48
Joined
Feb 28, 2001
Messages
27,140
Arnel, my method is one-pass, not two-pass.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 04:48
Joined
Feb 19, 2013
Messages
16,607
you can do it in one query

update yourTable set YesNoField =[ID}=1

where 1 is the ID of the record selected to be yes

edit: Doc got there before me
 

StlthBM

New member
Local time
Today, 11:48
Joined
Nov 2, 2021
Messages
5
Are you guys talking in SQL?
I can barely spell SQL correctly. My VBA and macro knowledge is about the same
 

plog

Banishment Pending
Local time
Yesterday, 22:48
Joined
May 11, 2011
Messages
11,638
The table is designed to keep track of statements, with only one statement being the "Endorsed" statement but the other iterations being kept track in the table.

What does "the other iterationss being kept track in the table" mean? What are you tracking?

If only 1 statement is to be endorsed, you simply just store it and not the others. I fear you haven't structured your table correctly.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 23:48
Joined
May 21, 2018
Messages
8,525
So likely you want to do this in the after update event in a form.
In the after update of the Endorsed field

What is the name of the Endorsed field? (is it "Endorsed")
What is the name of the Primary key and is it numeric or text?

it is probably some code that uses that above sql. Something like this in the afterupdate
Code:
dim PK as long
dim strSql as string
If Me.Endorsed then
    PK = me.NameOfPKField
    strSql = "Update tablName SET Endorsed = False"
    currentDB.execute strSql
    strSql = "Update tablename SET Endorsed = true WHERE NameOfPKfield = " & PK
   currentdb.execute strSql
end if
 

Users who are viewing this thread

Top Bottom