Solved

Is it possible to delete records from a table that are not part of the filter on a form for that table?

Posted on 2014-10-10
3
193 Views
Last Modified: 2014-10-10
I have a form with a subform (child) that is a datasheet view. The user can filter records of the form. With that, I want to know if it is possible to have some code that will delete all of the records in the table that are not part of the filtered data.

I do not know how to write a delete query string for that.

If this type of action is possible, could someone please show me an example?

Thanks
0
Comment
Question by:PBLack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 40372651
VBA for that would look similar to this:

Dim strSQL as string
Dim lngID as long 
lng = {this is where you would define the criteria for the records to delete}
strSQL = "DELETE * FROM SomeTable WHERE [IDField] = " & lngID
CurrentDB.Execute strSQL, dbfailonerror

Open in new window


The exact code and placement of the code would depend on the specific table/field names in your database,  what you are trying to delete, and how you want the deletion to be triggered.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 40372669
<<  delete all of the records in the table that are not part of the filtered data. >>

Ah - if you are trying to delete ALL of the records that are not in the filter, try this (MAKE A BACKUP BEFORE TESTING THIS, PLEASE!!)

Dim strSQL as string
'  ** Use the inverse of the form's filter for your deletion criteria.
strSQL = "DELETE * FROM SomeTable WHERE  NOT(" & Me.Filter & ")"
CurrentDB.Execute strSQL, dbfailonerror

Open in new window

0
 

Author Closing Comment

by:PBLack
ID: 40372699
That is what I wanted. Thanks. Backup not necessary as this table is built from pulling data from other tables on the fly.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

688 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