Delete records from multiple Access tables

Brent

I have multiple identically formatted tables imported from Excel spreadsheets into Access. The imports bring over empty records that I want to delete. I can delete from a single table using DELETE FROM HTS_01 WHERE TESTS Is Null;

However, if I try to write it to take care of a second table using,

  DELETE FROM HTS_01 WHERE TESTS Is Null;
  DELETE FROM HTS_0203 WHERE TESTS Is Null;

then I get the error "Characters found after end of SQL statement."

If I remove the semicolon from the first line, I get a syntax error "Syntax error (missing operator) in query expression TESTS Is Null DELETE FROM HTS_030 WHERE TESTS Is Null;"

The problem is that I have 19 tables. I suppose I could write 19 queries and then a short piece of code to execute the queries one by one, but I was trying to avoid that.

Brent

A co-worker came up with the following, and it worked very well. Thanks for your help!

Sub delete_empty_rows()
' **************************************************************************
' J.K. DeHart
' 3/8/16
' This script will loop through all active tables in the current database and
' remove rows there the defined colmn has 'NULL' data cells
' **************************************************************************

DoCmd.SetWarnings False ' Turn warnings 'Off' for DELETE function

Dim db As Database
Dim tbl As TableDef
Dim fieldName
Dim sqlString As String
Set db = CurrentDb

fieldName = "TESTS" ' Update this value for the driving field

For Each tbl In db.TableDefs
    If tbl.Attributes = 0 Then   'This tells it to ignore hidden tables
        sqlString = "DELETE * FROM " & tbl.Name & " WHERE '" & fieldName & "' Is Null"
        DoCmd.RunSQL (sqlString)
    End If
Next

' Clean up the script
Set tbl = Nothing
Set db = Nothing

DoCmd.SetWarnings True ' Turn warnings back 'On'
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete all records from multiple tables with VBA loop? Access 2010

From Dev

Query to delete from multiple tables in Access

From Dev

Query to delete from multiple tables in Access

From Dev

Delete multiple records from different tables with SQL stored procedure

From Dev

Delete records from multiple tables with foreign key constraint

From Dev

Delete multiple records from gridview

From Dev

How to delete records from linked tables in vba

From Dev

have to delete records from production master tables

From Dev

Delete from multiple tables with MyISAM

From Dev

Delete from multiple tables, if has

From Dev

Matching records from several tables in access

From Dev

Delete a row in multiple access tables using vb

From Dev

Select newest records from multiple tables

From Dev

Fetch records from multiple tables using join

From Dev

PHP MySQL newest records from multiple tables

From Dev

Counting grouped records from multiple tables

From Dev

Retrieve records from Multiple tables using Join

From Dev

Retrieving Records from Multiple Tables issue

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

What are alternatives to update/delete records with a key that references multiple tables?

From Dev

How to Delete multiple records from listview in android?

From Dev

Algolia: Delete multiple records from dashboard

From Dev

How to delete multiple records from SQL table

From Dev

How to Delete multiple records from listview in android?

From Dev

MySQL - Delete from multiple tables using a UNION?

From Dev

Delete the same set of values from multiple tables

From Dev

Delete rows from multiple tables as one query

From Dev

LINQ Join to delete contents from multiple tables

From Dev

Delete rows from multiple tables in a database

Related Related

  1. 1

    How to delete all records from multiple tables with VBA loop? Access 2010

  2. 2

    Query to delete from multiple tables in Access

  3. 3

    Query to delete from multiple tables in Access

  4. 4

    Delete multiple records from different tables with SQL stored procedure

  5. 5

    Delete records from multiple tables with foreign key constraint

  6. 6

    Delete multiple records from gridview

  7. 7

    How to delete records from linked tables in vba

  8. 8

    have to delete records from production master tables

  9. 9

    Delete from multiple tables with MyISAM

  10. 10

    Delete from multiple tables, if has

  11. 11

    Matching records from several tables in access

  12. 12

    Delete a row in multiple access tables using vb

  13. 13

    Select newest records from multiple tables

  14. 14

    Fetch records from multiple tables using join

  15. 15

    PHP MySQL newest records from multiple tables

  16. 16

    Counting grouped records from multiple tables

  17. 17

    Retrieve records from Multiple tables using Join

  18. 18

    Retrieving Records from Multiple Tables issue

  19. 19

    Postgresql delete multiple rows from multiple tables

  20. 20

    What are alternatives to update/delete records with a key that references multiple tables?

  21. 21

    How to Delete multiple records from listview in android?

  22. 22

    Algolia: Delete multiple records from dashboard

  23. 23

    How to delete multiple records from SQL table

  24. 24

    How to Delete multiple records from listview in android?

  25. 25

    MySQL - Delete from multiple tables using a UNION?

  26. 26

    Delete the same set of values from multiple tables

  27. 27

    Delete rows from multiple tables as one query

  28. 28

    LINQ Join to delete contents from multiple tables

  29. 29

    Delete rows from multiple tables in a database

HotTag

Archive