Fil
Fil

Reputation: 471

Error handling on form load using OnError Resume Next

I am trying to catch error on form load so I can call a function. On Error Resume Next is not catching error. I am trying to catch an error using Set rs = CurrentDb.OpenRecordset("SELECT DonorName FROM tblDonations"). Is there a way around this? I am thinking that the Err.Number <> 0 is returning 0 hence the next line is skipped. This code works well in access 2019 and 365 but not in access 2016. Please see below:

Private Sub Form_Load()
On Error Resume Next
Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT DName FROM tblDonations")
If Err.Number <> 0 Then
    MsgBox "Error Number: " & Err.Number & " " & Err.Description & " Please relink to Backend file!", , "Backend missing"
    Call RelinkTables
End If
rs.Close
Set rs = Nothing

DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "frmMainform"
End Sub

Upvotes: 0

Views: 297

Answers (1)

niton
niton

Reputation: 9189

You should be able to work around this behaviour.

Private Sub Form_Load()

Dim rs As Object

On Error Resume Next
Set rs = CurrentDb.OpenRecordset("SELECT DName FROM tblDonations")
On Error GoTo 0

If rs Is Nothing Then
    MsgBox "rs Is Nothing", , "Backend missing"
    Call RelinkTables
End If

'...

End Sub

Upvotes: 1

Related Questions