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
188 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

785 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