I have 100 linked tables in ms-Access with the Name "TBL*" and they have the same columns. I tried to create a module using vba that deletes some rows according to an sql query as follows:
Option Compare Database
Option Explicit
Sub DeleteRecords()
Dim strSQL As String 'sql statement
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Set db = CurrentDb
For Each tdf In db.TableDefs
DoCmd.SetWarnings False
If Not (tdf.Name Like "MSys*") And tdf.Name Like "TBL*" Then
db.Execute "DELETE FROM " & tdf.Name & " WHERE " & tdf.Fields(2) & " NOT LIKE '%MUST NOT DEL%'", dbFailOnError
End If
'DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Next
End Sub
Which means I need only the rows that their second column is like '%MUST NOT DEL%' and delete the others. This code gives me an error of invalid operation. I tried a lot of changes but nothing. I think that maybe I have a syntax error on my query. Any ideas what's wrong?
DELETE FROM...SQL in a string variable first so you can see the completed statement (and check it for errors/issues), and then pass it todb.Execute.NOT LIKE.