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
187 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

863 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

28 Experts available now in Live!

Get 1:1 Help Now