Private Sub cmdDelSensitive_Click()
Dim rtn As Long
Me.txtOut = Null
rtn = MsgBox("This will delete all sensitive data from your DB. Make sure you have a backup before attempting. Do you want to continue?", vbCritical + vbYesNo, "Delete Data")
If rtn = vbYes Then
ClearSensitive
End If
End Sub
Public Sub ClearSensitive()
Dim tdf As TableDef
Dim rs As Recordset
Dim fld As Field
Dim strSql As String
Dim strOut As String
For Each tdf In CurrentDb.TableDefs
If Left(tdf.Name, 1) <> "~" And Left(tdf.Name, 4) <> "MSYS" And Left(tdf.Name, 4) <> "Copy" Then
strOut = strOut & "TABLE: " & tdf.Name & vbCrLf
Set rs = CurrentDb.OpenRecordset(tdf.Name, dbOpenDynaset)
For Each fld In rs.Fields
If DoesFieldPropertyExist(fld, "Description") Then
If Left(fld.Properties("Description"), Len("Sensitive")) = "Sensitive" Then
If fld.Required = True Then
MsgBox "Cannot clear " & fld.Name & " in " & tdf.Name & " It is required.", vbCritical
strOut = strOut & "Cannot clear " & fld.Name & " in " & tdf.Name & " It is required." & vbCrLf
Else
strOut = strOut & "-- Clearing Sensitive Field: " & fld.Name & vbCrLf
strSql = "Update " & tdf.Name & " Set " & fld.Name & " = Null"
'strOut = strOut & "----" & strSql & vbCrLf
CurrentDb.Execute strSql
End If
End If
End If
Next fld
strOut = strOut & vbCrLf
End If
Next tdf
Me.txtOut = strOut
End Sub
Function DoesFieldPropertyExist(fld As DAO.Field, propName As String) As Boolean
On Error GoTo ErrorHandler
Dim p As DAO.Property
' Attempt to access a standard property (e.g., DataUpdatable)
' This will generate an error if the property doesn't exist for this field type
Dim temp As Variant
temp = fld.Properties(propName).Value
DoesFieldPropertyExist = True
Exit Function
ErrorHandler:
' If an error occurred, it means the property was not found directly
' Now, check for user-defined properties
For Each p In fld.Properties
If p.Name = propName Then
DoesFieldPropertyExist = True
Exit Function
End If
Next p
DoesFieldPropertyExist = False ' Property not found
End Function