Ciao Sandro proprio per verificare quello che mi segnala l'amico Mimmo, cioè che non risulterebbero tabelle correlate, ho importato nel mio DB dal tuo Db questo:
1)
Option Compare Database
Option Base 1
Option Explicit
Public Function MostraTabelleCorrelate(OrigineDati As String, chiavePrimaria As String)
Dim nuovaOrigineDati As Variant
nuovaOrigineDati = Null
Dim dbs As DAO.Database
Dim tbl As DAO.TableDef
Dim idx As DAO.index
Dim parc As Integer
Dim fld As DAO.Field
Dim rel As DAO.Relation
Dim i As Integer, j As Integer, nTables As Integer, nFields As Integer, MaxNumField As Integer, nCiclo As Integer
Dim MyTablesName() As String, pk As String, TabPrimEst() As String
Dim myTable As Variant
Dim rst As DAO.Recordset
Set dbs = CurrentDb
If identificaOggetto(OrigineDati) = False Then
Dim objQ As AccessObject, objT As AccessObject, dbst As Object
Dim esciFor As Boolean
Set dbst = Application.CurrentData
' Search for open AccessObject objects in AllQueries collection.
For Each objQ In dbst.AllQueries
If objQ.Name = OrigineDati Then
For Each objT In dbst.AllTables
If objQ.IsDependentUpon(acTable, objT.Name) Then
Set tbl = dbs.TableDefs(objT.Name)
For Each tbl In dbs.TableDefs
If tbl.Name = objT.Name Then
For Each idx In tbl.Indexes
If idx.Primary = True Then
nuovaOrigineDati = objT.Name
esciFor = True
End If
Next
End If
If esciFor Then
Exit For
End If
Next
End If
If esciFor Then
Exit For
End If
Next
End If
If esciFor Then
Exit For
End If
Next
' se l'origine dati è una query composta da più tabelle determino la tabella della pk della form sottostante
' gli esciFor sono li utilizzo per uscire prima dal ciclo for each ... next quando non serve
Set objQ = Nothing
Set objT = Nothing
Set dbst = Nothing
End If
If Not IsNull(nuovaOrigineDati) Then
OrigineDati = nuovaOrigineDati
End If
nTables = dbs.TableDefs.Count ' numero delle tabelle in dbs
Set tbl = dbs.TableDefs(OrigineDati)
i = 1
Set tbl = dbs.TableDefs(0)
MaxNumField = 0
For Each tbl In dbs.TableDefs
If tbl.Fields.Count > MaxNumField Then
MaxNumField = tbl.Fields.Count
End If
Next ' trovo il numero massimo di campi delle tabelle di dbs
ReDim TabPrimEst(1, MaxNumField)
For Each rel In dbs.Relations
If rel.Table = OrigineDati Then
TabPrimEst(1, i) = rel.ForeignTable
i = i + 1
TabPrimEst(1, i) = rel.Fields(0).ForeignName
i = i + 1
End If
Next ' matrice con nome tabella esterna/e,pk esterna/e
nCiclo = i
ReDim MyTablesName(nTables, MaxNumField) ' ridimensiono la matrice per contenere i nomi delle tabelle e il numero massimo di campi
i = 1
j = 3
For Each tbl In dbs.TableDefs
If Left(tbl.Name, 4) <> "Msys" Then
MyTablesName(i, 1) = tbl.Name
j = 2
For Each idx In tbl.Indexes
If idx.Primary Then
Set fld = tbl.Fields(0)
If InStr(idx.Fields, ";") > 0 Then
For parc = 1 To nCiclo
If TabPrimEst(1, parc) = MyTablesName(i, 1) Then
MyTablesName(i, 2) = TabPrimEst(1, parc + 1)
Exit For
End If
Next
Else
MyTablesName(i, 2) = Mid(idx.Fields, 2, Len(idx.Fields))
End If
End If
If idx.Foreign = True Then
j = j + 1
MyTablesName(i, j) = Mid(idx.Fields, 2, Len(idx.Fields))
End If
Next
i = i + 1
End If
Next ' memorizzo nella matrice il nome delle tabelle nella colonna1, la pk nella colonna2 e le chiavi esterne nella colonna3
myTable = Null
For i = 1 To nCiclo - 1
For j = 1 To nTables
If TabPrimEst(1, i) = MyTablesName(j, 1) Then
Set rst = dbs.OpenRecordset(MyTablesName(j, 1), dbOpenTable)
While Not rst.EOF
If rst(TabPrimEst(1, i + 1)) = chiavePrimaria Then
myTable = (myTable + ", " + vbCrLf) & MyTablesName(j, 1) & " con questo riferimento " & rst(MyTablesName(j, 2))
'Debug.Print myTable
End If
rst.MoveNext
Wend
End If
Next
Next
myTable = myTable & "."
If myTable <> "." Then
'MsgBox ("Non puoi cancellare questo record in quanto contiene record correlati in queste tabelle:" + vbCrLf + myTable), vbCritical
' è possbile utilizzare anche un msgbox, se il numero delle correlazione non è elevato.
' in caso contrario non sono completamente visibili
DoCmd.OpenForm "ErroreTabelleCorrelate", , , , , , myTable
End If
Set tbl = Nothing
Set idx = Nothing
Set dbs = Nothing
Set fld = Nothing
Set rel = Nothing
Set rst = Nothing
End Function
2) Private Sub DelRecord_Click()
On Error GoTo Err_DelRecord_Click
Call MostraTabelleCorrelate(Me.RecordSource, Me.IDCliente)
Exit_DelRecord_Click:
Exit Sub
Err_DelRecord_Click:
Resume Exit_DelRecord_Click
End Sub
3) Form_ErroreTabelleCorrelate
4) attivata la libreria Microsoft DAO 3.6 Object Library
e facendo click sul pulsante al punto 2 non accade nulla, non compare la Form che mi avvisa delle tabelle collegate.
Cosa non va?
Ciao Nicola.