I've added a command button to a form that allows the end user to delete all entries in that record (and it's related tables) EXCEPT the primary key field.
My intent is to replace the field entries with their default value (when one has been defined) or otherwise with 'null' (as they would be in a new record.)
The record consists of MANY fields of many various data types. The problem 'might' be that many, if not most of the fields do not have an assigned default value. (In some instances, there is a default declared at record entry on the form.)
In an earlier version of the code, I had simply saved the value of the primary key; deleted the record; created a new record and assigned the value of the old primary key to the new record. This seemed to be award and problematic (not certain it wasn't my lack of skill. NTL). So I thought I could deal with it in a more direct way: just wipe all of the entries from the existing record (code below.)
(Don't think it matters, but also know that there is also a constant onslaught of new fields and related tables periodically being added to the record / table.)
It is these null default values that are tossing errors. (I think)
Any thoughts, suggestions, ... will be greatly appreciated ins advance
Code:
Private Sub cmdClearEntries_Click()
Dim strfieldname As String
Dim varDefaultValue As Variant
Dim strTable As String
Dim strType As String
On Error GoTo err_cmdClearEntries_Click
strprompt = "This action will REMOVE ALL detail entries for this fixture type" _
& vbCrLf _
& vbCrLf & "DO YOU WANT TO PROCEED ?"
gsMsgTitle = "CLEAR ENTRY"
gsMsgResponse = MsgBox(strprompt, vbCritical + vbYesNo + vbDefaultButton2, gsMsgTitle)
If gsMsgResponse = vbYes Then
DoCmd.SetWarnings False
strTable = "tbeFixtureTypeDetails"
strType = Me.Type
Set rs = CurrentDb.OpenRecordset(strTable)
With rs
For n = 0 To .Fields.Count - 1
If .Fields(n).Name <> "type" Then
strfieldname = .Fields(n).Name
varDefaultValue = Nz(.Fields(n).DefaultValue, Null)
Debug.Print strfieldname & " : " & varDefaultValue
gsSQL = "update " & strTable & _
" Set " & strfieldname & " = " & varDefaultValue & _
" WHERE " & strTable & ".type = '" & strType & "';"
CurrentDb.Execute gsSQL, dbFailOnError
End If
Next 'n
.Close
End With
Set rs = Nothing
Dim rel As DAO.Relation
DoCmd.SetWarnings False
For Each rel In CurrentDb.Relations
With rel
If .Table = "tbeFixtureTypeDetails" Then
strSQL = "delete * from " & .Table & " where ([type] = '" & vType & "')"
DoCmd.RunSQL strSQL
End If
End With
DoCmd.SetWarnings True
Next
Else
Exit Sub
End If
exit_cmdClearEntries_Click:
DoCmd.SetWarnings True
Exit Sub
err_cmdClearEntries_Click:
DoCmd.SetWarnings True
MsgBox Err.Description
Resume exit_cmdClearEntries_Click
End Sub