从多个 Access 表中删除记录

Delete records from multiple Access tables

我将多个格式相同的 table 从 Excel 电子表格导入到 Access。导入带来了我想删除的空记录。我可以使用 DELETE FROM HTS_01 WHERE TESTS Is Null;

从单个 table 中删除

但是,如果我尝试编写它来照顾第二个 table 使用

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

然后我得到错误 "Characters found after end of SQL statement."

如果我从第一行删除分号,我会收到语法错误“查询表达式 TESTS 中的语法错误(缺少运算符)是 Null DELETE FROM HTS_030 WHERE TESTS 为空;

问题是我有 19 个 table。我想我可以编写 19 个查询,然后编写一小段代码来一个一个地执行查询,但我试图避免这种情况。

一位同事想出了以下方法,而且效果很好。感谢您的帮助!

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