Solved

Access VBA:  Update a filtered subform based on the filter

Posted on 2013-01-16
3
2,331 Views
Last Modified: 2013-01-16
Hi All

Big hairy question:  What's the VBA code to update ONLY the filtered rows in a subform?

Main form:  
A bunch of list boxes that allow users to select 'search' values.
GO command button that filters a subform based on VBA that pieces together a filter.
   Example:  [state] = 'CA' AND [products] IN ('bananas', 'grapes')
Select all command button that checks the 'is_selected' checkbox for all rows in the subform.

Subform:  
Starts with all records in an updateable SQL view (say 1,000), which includes a bit (Yes/No) column with a checkbox called is_selected that users can check or uncheck.
The main form's GO command button filters the form via VBA, displays the filtered records, and does not update them.
The main form's Select All command button checks the is_selected checkbox in all rows in the subform.

My task is to write VBA so that the Select All button ONLY checks the is_selected checkbox for the rows that have been filtered.  Using the above example, if CA bananas and grapes amount to 200 of the total 1,000 rows, then I want those 200 is_selected checkboxes checked, and the others unaffected.

Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
3 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 150 total points
ID: 38782627
is the  "is_selected checkbox" bound or unbound ?

if bound

sql="update [subformtable] set is_selected=-1 where " & form_subformName.filter
0
 
LVL 61

Accepted Solution

by:
mbizup earned 350 total points
ID: 38782675
Jim,

If your subform filter contains constraints for fields in different tables (not just the table you are updating), it might be easiest to use recordset code:

dim rs as DAO.recordset
set rs = Me.SubformName.form.recordsetclone

if rs.recordcount = 0 then exit sub

Do until rs.EOF
      rs.edit
      rs![is_selected] = true
      rs.update
      rs.movenext
Loop
set rs = nothing
Me.SubformName.form.requery

Open in new window

0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 38784432
Both solutions work, but the loop enables me to make SQL SP calls individually, so that was accepted as best.  Thanks.  -Jim
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question