Solved

Access VBA:  Update a filtered subform based on the filter

Posted on 2013-01-16
3
2,287 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 119

Assisted Solution

by:Rey Obrero
Rey Obrero 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
I have a large data set and a SSIS package. How can I load this file in multi threading?
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now