2016-03-08 178 views
0

我有從Excel電子表格導入Access的多個相同格式的表。導入會導致我想要刪除的空記錄。我可以使用DELETE FROM HTS_01 WHERE TESTS Is Null;從多個Access表中刪除記錄

不過,如果我試着寫一個使用採取了第二個表的護理單表中刪除,

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

然後我得到的錯誤「人物SQL語句結束後發現「。

如果我從第一行中刪除分號,則會在查詢表達式中出現語法錯誤「語法錯誤(缺少操作符)TESTS爲空DELETE FROM HTS_030 WHERE TESTS Is Null;」

問題是我有19個表。我想我可以編寫19個查詢,然後編寫一小段代碼逐個執行查詢,但我試圖避免這種情況。

+0

聽起來不錯。我不知道如何編寫該語法。它會像這樣簡單:DELETE FROM [「&HTS_01&」] WHERE TESTS是NULL; DELETE FROM [「&HTS_0203&」] WHERE TESTS is Null; .....等等。這就是刪除整個記錄所需的全部內容? – Brent

+0

不太清楚你在那裏的意思,布倫特。我提交了一個答案,希望能讓我的意圖更清楚。 – HansUp

回答

0

一位同事想出了以下內容,並且工作得很好。謝謝你的幫助!

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