Dear all,
Is there a way to dalete all records of several tables using one DELETE query?
Is there a way to dalete all records of several tables using one DELETE query?
Private Const conClearSQL As String = "DELETE * FROM [%T]%W;" 'ClearTable clears the named table. A WHERE string is used if passed. Public Sub ClearTable(strTable As String, Optional strWhere As String = "") Dim strSQL As String On Error GoTo CTError strTable = CurrentDb.TableDefs(strTable).Name On Error GoTo 0 strSQL = Replace(Replace(conClearSQL, "%T", strTable), _ "%W", IIf(strWhere = "", "", " WHERE " & strWhere)) Call DoCmd.RunSQL(strSQL) Exit Sub CTError: Call MsgBog(Prompt:="Invalid table {" & strTable & "}", _ Title:="ClearTable") End Sub
'Will DELETE ALL Records in 3 Tables CurrentDb.Execute "Delete * From Customers", dbFailOnError
Select Distinct Name as Table_Name From MSysObjects Where Type in (1,4,6) and left(name,4) = "tbl_" <--- use this if you want specific tables
Function RunDelValuesInTables() 'you can put in error handling later' Dim dbs as doa.Database Dim qdf as doa.QueryDef Dim rst as Recordset Dim strRunQry as String: strRunQry = "" Set dbs = CurrentDb Set qdf = dbs.QueryDefs("xQuery_List_of_Existing_Tables") Set rst = qdf.OpenRecordSet rst.MoveFirst Do While Not rst.EOF strRunQry = rst.Fields(0) strRunQry = "Delete from " & strRunQry dbs.Execute strRunQry rst.MoveNext Loop response = MsgBox ("Data in all tables has been removed ." End Function
Comment