dbTalk Databases Forums  

Updating a flag in a table for a specific number of records

comp.databases.ms-access comp.databases.ms-access


Discuss Updating a flag in a table for a specific number of records in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Laura
 
Posts: n/a

Default Updating a flag in a table for a specific number of records - 03-02-2011 , 09:59 AM






I have an access table that contains X number of records (X changes on
a weekly basis). I have a recordset that counts the number of records
in the table (X) and stores this number as a variable (FSPRecords).

I have a second vairable that works out what 20% of the total number
of records is (FSPLowest20).

I have field in my table tbl_FSP_2010 called Flag and another called
Total. Using VBA code, I need to order my table by the Total column
and assign the Flag field to "true" for the first 20% of the records
(the number of records stored in the variable FSPLowest20).

Does anyone have any ideas on how this can be done please?

Many thanks

Laura

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Updating a flag in a table for a specific number of records - 03-02-2011 , 11:30 AM






Laura wrote:
Quote:
I have an access table that contains X number of records (X changes on
a weekly basis). I have a recordset that counts the number of records
in the table (X) and stores this number as a variable (FSPRecords).

I have a second vairable that works out what 20% of the total number
of records is (FSPLowest20).

I have field in my table tbl_FSP_2010 called Flag and another called
Total. Using VBA code, I need to order my table by the Total column
and assign the Flag field to "true" for the first 20% of the records
(the number of records stored in the variable FSPLowest20).

Does anyone have any ideas on how this can be done please?

Stop messing with recordsets and variables. Create and save a query that
returns the first 20% of your records

SELECT TOP 20 PERCENT <key fields>,[Flag]
FROM tbl_FSP_2010
ORDER BY Total ASC

Save it as First20PercentFSP

Then in VBA (test this in a backup of your database):
-------------------------------------------
set db=currentdb

'first, clear the flag column in all records:
db.execute "update tbl_FSP_2010 set Flag=0"

'Then update the flag column using the saved query as the source:
db.execute "update First20PercentFSP set Flag = -1"
-------------------------------------------

PS. I'm a little concerned with the name of your table, since it implies
that you will be creating a new table for each year. This is not
recommended. What you should have is a single table called tbl_FSP with an
additional column called FSP_Year to identify the year of the records. The
maxim is: Data should be stored as data, not metadata.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.