Solved

Access VBA:  Update a filtered subform based on the filter

Posted on 2013-01-16
3
2,247 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

762 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

7 Experts available now in Live!

Get 1:1 Help Now