Chiusura Recordset in caso di errore

di il
2 risposte

Chiusura Recordset in caso di errore

Buongiorno a tutti, chiedo Vostro consiglio su quale e' il modo corretto di procedere: ho diverse situazioni in cui ho l'apertura di recordset condizionati come nell'esempio seguente.

Private Sub Form_Current()
On Error GoTo Err_handler

Dim rst As DAO.Recordset
Dim rst1 As DAO.Recordset
    If Me.sfrmRCcboNUMtabRCIDtabOEid.Value <> 0 Then
        Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM tblOEoffertecostamp WHERE IDtabOEid=" & Me.sfrmRCcboNUMtabRCIDtabOEid, dbOpenSnapshot, dbReadOnly)
        Me.sfrmRCchkflagoffertaevasa.Value = rst.Fields(8)
        sfrmRCchkflagoffertaevasa.Requery
        rst.Close
        Set rst = Nothing
    Else
        Me.sfrmRCchkflagoffertaevasa.Value = False
    End If
    If Me.sfrmRCcboNUMtabRCIDtabONid.Value <> 0 Then
        Set rst1 = DBEngine(0)(0).OpenRecordset("SELECT * FROM tblONordiniclienti WHERE IDtabONid=" & Me.sfrmRCcboNUMtabRCIDtabONid, dbOpenSnapshot, dbReadOnly)
        Me.sfrmRCchkflagordineevaso.Value = rst1.Fields(6)
        sfrmRCchkflagordineevaso.Requery
        rst1.Close
        Set rst1 = Nothing
    Else
        Me.sfrmRCchkflagordineevaso.Value = False
    End If
    
    sfrmRClstNUMtabRCIDtabEDid.Visible = Me.NewRecord 'listbox visibile con NewRecord
    sfrmRCctrtxtattrezzista.Visible = Not Me.NewRecord 'textbox non visibile con NewRecord
    sfrmRCcboNUMtabRCIDtabANid.Requery
    
Exit_Err_handler:
    Exit Sub
Err_handler:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_Err_handler

End Sub

Mi sono accorto che se la routine dovesse andare in errore dopo l'apertura di un recordset, questo teoricamente rimarrebbe aperto perche' si attiva la procedura dell'Err_handler e si chiude la Sub con il rst aperto.

Ho provato a spostare la sua chiusura dopo Exit_Err_handler, ma va in errore perche' se il recordset non e' stato aperto ovviamente non lo puo' chiudere.

Quindi quale e' il corretto modo di operare? Si puo' verificare se un recordset e' stato aperto?

Grazie in anticipo

2 Risposte

  • Re: Chiusura Recordset in caso di errore

    Ovviamente è così... secondo me commetti un errore, aprire ed istanziare 2 Recordset, non servono, e di impostazione.

    Premesso che fossi in te userei in questo caso DLOOKUP essendo una chiamata singola... ma proverei nel caso a gestirla così:

    Private Sub Form_Current()
    On Error GoTo Err_handler
    
    Dim rst As DAO.Recordset
        If Me.sfrmRCcboNUMtabRCIDtabOEid.Value <> 0 Then
            Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM tblOEoffertecostamp WHERE IDtabOEid=" & Me.sfrmRCcboNUMtabRCIDtabOEid, dbOpenSnapshot, dbReadOnly)
            Me.sfrmRCchkflagoffertaevasa.Value = rst.Fields(8)
            sfrmRCchkflagoffertaevasa.Requery
        Else
            Me.sfrmRCchkflagoffertaevasa.Value = False
        End If
        If Me.sfrmRCcboNUMtabRCIDtabONid.Value <> 0 Then
            Set rst1 = DBEngine(0)(0).OpenRecordset("SELECT * FROM tblONordiniclienti WHERE IDtabONid=" & Me.sfrmRCcboNUMtabRCIDtabONid, dbOpenSnapshot, dbReadOnly)
            Me.sfrmRCchkflagordineevaso.Value = rst1.Fields(6)
            sfrmRCchkflagordineevaso.Requery
        Else
            Me.sfrmRCchkflagordineevaso.Value = False
        End If
        
        sfrmRClstNUMtabRCIDtabEDid.Visible = Me.NewRecord 'listbox visibile con NewRecord
        sfrmRCctrtxtattrezzista.Visible = Not Me.NewRecord 'textbox non visibile con NewRecord
        sfrmRCcboNUMtabRCIDtabANid.Requery
        
    Exit_Err_handler:
        If Not rst is Nothing then
        	rst.Close
        	Set rst = Nothing
        End If
        Exit Sub
        
    Err_handler:
        MsgBox Err.Number & " " & Err.Description
        Resume Exit_Err_handler
    
    End Sub
  • Re: Chiusura Recordset in caso di errore

    25/11/2025 - @Alex ha scritto:

    Ovviamente è così... secondo me commetti un errore, aprire ed istanziare 2 Recordset, non servono, e di impostazione.

    Fare copia e incolla di pezzi di codice purtroppo mi fa commettere errori banali. Ho corretto come da Tuo suggerimento.

    Interessante il "If Not rst is Nothing then" era proprio cio che cercavo: prezioso come sempre il tuo suggerimento

    Grazie

Devi accedere o registrarti per scrivere nel forum
2 risposte