A
Arvi Laanemets
This posting follows my post "Overriding constraint restriction message"
from 21.04.2010 12:00.
Overriding constraint restriction message when changing a record, or when
adding a new record, now works - thanks link
(http://support.microsoft.com/default.aspx?scid=kb;en-us;206175) provided by
Stefan. But solution provided there didn't help when records were deleted
(My forms are continous, so there may be several records selected for
deleting, and some of them may be allowed to delete, and some not).
I tried to write for this case a function similar to the one provided by
link above, and called from OnDelete event of the form. The function seems
to work, but apparently I have problem with OnDelete event - I can't clear
deleted records from form.
The function and event are here:
------------------------------------------------------------------------
Public Function DeleRecODBC(SRO_form As Form) As Boolean
'***************************************************************
'Function: DeleRecODBC
'
'Purpose: Deletes marked records from a form based on a linked ODBC table
' when deletion isn't restricted, and traps any ODBC errors
otherwise.
'
'Arguments: SRO_Form, which refers to the form.
'
'
'Returns: always True, because exists possibility,
' that some records are deleted, and some not.
'***************************************************************
Dim errStored As Error
Dim rc As DAO.Recordset
Dim iSelTop As Integer
Dim iSelHeight As Integer
Dim i As Integer
Dim rcError As Boolean
iSelTop = SRO_form.SelTop
iSelHeight = SRO_form.SelHeight
Set rc = SRO_form.Recordset.Clone
For i = iSelTop + iSelHeight - 2 To iSelTop - 1 Step -1
' To avoid problems with absolute position when deleting records,
' the deleting is started fom marked record with highesd absolute
position
rcError = False
rc.AbsolutePosition = i
On Error GoTo DeleRecODBCErr
rc.Delete
Continue_DeleRecODBCErr:
Next i
DeleRecODBC = True
' As the result may be mixed (some records were deleted, and some not),
' the return value is irrelevant.
Exit Function
DeleRecODBCErr:
' The function failed because of an ODBC error.
For Each errStored In DBEngine.Errors
Select Case errStored.Number
Case 547
' Foreign key constraint error.
MsgBox "Deleting was restricted by foreign key!"
rcError = True
Exit For
Case Else
End Select
Next errStored
Resume Continue_DeleRecODBCErr
End Function
------------------------------------------------------------------------
The function is called when form's OnDelete event occurs:
------------------------------------------------------------------------
Private Sub Form_Delete(Cancel As Integer)
If DeleRecODBC(Me) Then
Cancel = -1
' Records wich were allowed to be deleted are gone from source
database now.
' But on the form they exists - how remove deleted records from form
now?
'Me.Requery
' Requery returns an error "Operation is not supported in
transactions".
'Me.Refresh
' Refresh does something and freezes the app for a couple of
minutes,
' and deleted records remain on the form.
End If
End Sub
from 21.04.2010 12:00.
Overriding constraint restriction message when changing a record, or when
adding a new record, now works - thanks link
(http://support.microsoft.com/default.aspx?scid=kb;en-us;206175) provided by
Stefan. But solution provided there didn't help when records were deleted
(My forms are continous, so there may be several records selected for
deleting, and some of them may be allowed to delete, and some not).
I tried to write for this case a function similar to the one provided by
link above, and called from OnDelete event of the form. The function seems
to work, but apparently I have problem with OnDelete event - I can't clear
deleted records from form.
The function and event are here:
------------------------------------------------------------------------
Public Function DeleRecODBC(SRO_form As Form) As Boolean
'***************************************************************
'Function: DeleRecODBC
'
'Purpose: Deletes marked records from a form based on a linked ODBC table
' when deletion isn't restricted, and traps any ODBC errors
otherwise.
'
'Arguments: SRO_Form, which refers to the form.
'
'
'Returns: always True, because exists possibility,
' that some records are deleted, and some not.
'***************************************************************
Dim errStored As Error
Dim rc As DAO.Recordset
Dim iSelTop As Integer
Dim iSelHeight As Integer
Dim i As Integer
Dim rcError As Boolean
iSelTop = SRO_form.SelTop
iSelHeight = SRO_form.SelHeight
Set rc = SRO_form.Recordset.Clone
For i = iSelTop + iSelHeight - 2 To iSelTop - 1 Step -1
' To avoid problems with absolute position when deleting records,
' the deleting is started fom marked record with highesd absolute
position
rcError = False
rc.AbsolutePosition = i
On Error GoTo DeleRecODBCErr
rc.Delete
Continue_DeleRecODBCErr:
Next i
DeleRecODBC = True
' As the result may be mixed (some records were deleted, and some not),
' the return value is irrelevant.
Exit Function
DeleRecODBCErr:
' The function failed because of an ODBC error.
For Each errStored In DBEngine.Errors
Select Case errStored.Number
Case 547
' Foreign key constraint error.
MsgBox "Deleting was restricted by foreign key!"
rcError = True
Exit For
Case Else
End Select
Next errStored
Resume Continue_DeleRecODBCErr
End Function
------------------------------------------------------------------------
The function is called when form's OnDelete event occurs:
------------------------------------------------------------------------
Private Sub Form_Delete(Cancel As Integer)
If DeleRecODBC(Me) Then
Cancel = -1
' Records wich were allowed to be deleted are gone from source
database now.
' But on the form they exists - how remove deleted records from form
now?
'Me.Requery
' Requery returns an error "Operation is not supported in
transactions".
'Me.Refresh
' Refresh does something and freezes the app for a couple of
minutes,
' and deleted records remain on the form.
End If
End Sub