Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Data in a field in all records 2

Status
Not open for further replies.

jbento

Technical User
Jul 20, 2001
573
US
All,
I have a Yes/No field called answerYes, which is in a table called tbl_Data.

I have a switchboard with a command button that I would like to reference the tbl_Data and if the value is True for answerYes in all records, I would like to make the value False in every record.

Is there an easy way to do this?

Jerome Benton
JERPAT Web Designs
GOD Is Good All The Time!!!
 
There is probably an easier, or at least more elegant way to do this, but my first thought is:

Dim nFalse as Integer
Dim strSql as String
Dim myDB As DAO.Database
Dim myRST As DAO.Recordset

Set myDB = whatever
strSql = "SELECT yes/no field FROM table"
Set myRST = myDB.OpenRecordset(strSql, dbOpenDynaset)
nFalse = 0
myRST.MoveFirst
While Not myRST.EOF
If myRST!yes/no field = FALSE Then
nFalse = nFalse + 1
End If
Wend

If nFalse <> 0 Then
myRST.MoveFirst
While Not myRST.EOF
myRST!yes/no field = FALSE
Wend
End If

MyRST.Close
MyDB.Close
 
dverdi,
I think you forgot the MyRST.MoveNext statement for both loops.
Also, in the second loop, you need to issue MyRST.Edit{/B] and then MyRST.Update before any updates can occur to the recordset.
In addition, your second loop conditional expression should be If nFalse = 0 Then.
Apart from that, the code should work :)

Alternatively, you could add a conditional expression in the query string: strSql = "SELECT yesNo FROM Table1 WHERE yesNo = FALSE" and then check if any records are returned (myRST.EOF will be true). If so then execute an update query that sets the field to FALSE for every record.
Cheers.

 
Here's a modified code of dverdi's:
Code:
Dim strSql As String
Dim myDB As DAO.Database, myRST As DAO.Recordset
Set myDB = CurrentDb

strSql = "SELECT YesNo FROM Table1 WHERE YesNo = FALSE"
Set myRST = myDB.OpenRecordset(strSql, dbOpenDynaset)

If myRST.EOF Then
    strSql = "UPDATE Table1 SET Table1.[YesNo] = False;"
    myDB.Execute strSql
End If

myRST.Close
myDB.Close
 
This worked like a charm:

Dim strSql As String
Dim myDB As DAO.Database, myRST As DAO.Recordset
Set myDB = CurrentDb

strSql = "SELECT YesNo FROM Table1 WHERE YesNo = FALSE"
Set myRST = myDB.OpenRecordset(strSql, dbOpenDynaset)

If myRST.EOF Then
strSql = "UPDATE Table1 SET Table1.[YesNo] = False;"
myDB.Execute strSql
End If

myRST.Close
myDB.Close

Thanks so much to BOTH of you and have a great evening.

Jerome Benton
JERPAT Web Designs
GOD Is Good All The Time!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top