How to gracefully handle errors generated when opening a form with linked data? (1 Viewer)

cricketbird

Registered User.
Local time
Today, 13:43
Joined
Jun 17, 2013
Messages
108
When I open an Access DB, it it set to open a Welcome form which runs some code in the 'Form_Open' event. However, if the backend is offline, the 'Form_Open' code will generate an error. In my error handling to the 'Form_Open' function, I have it set to open the "Settings" form where the user can re-link the tables. The Settings window does indeed open as intended, but the Welcome form immediately opens again, hiding the "Settings" window and confusing my users. I think this is because the Welcome form's subsequent 'Form_Load' function has (otherwise necessary) steps that draw the focus back to Form A.

Is there a way to have Form_Open return an error that the Form_Load can use. e.g. if iserror(Form_Open) then do X, else do Y?

Or, what would the best practice be for code that needs to run on the Welcome form that requires access to the backend but also gracefully handles the situation of allowing the user to relink the backend when needed and then resuming running?
 

bastanu

AWF VIP
Local time
Today, 10:43
Joined
Apr 13, 2010
Messages
1,402
Try to open the Settings form as a modal instead so it will wait until the user relinks the BE before passing control back to the calling sub.

Cheers,
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:43
Joined
May 21, 2018
Messages
8,529
See if this relinker helps
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:43
Joined
Feb 19, 2002
Messages
43,275
To avoid this type of issue, I have special purpose code in the opening form of an application which is always unbound. The first form starts by checking to see if two of the tables of the application are available. Pick any one or more or even all tables to check. If there are no errors, the login form opens and the login proceeds. Otherwise, the relink form is opened. Simpler all around than starting with a bound form and trying to recover from an error.

Here is the simplest version of the code I could find.
Code:
Private Sub Form_Activate()

    On Error GoTo ErrProc
    If DCount("*", "tblCustomers") >= 0 Then
        DoCmd.OpenForm "frmLogin", , , , , , Me.Name
    Else
        DoCmd.OpenForm "frmRelinkJetORACEtables", , , , , , Me.Name
    End If
    Me.Visible = False
    
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case 3044   ''' table not found
            DoCmd.OpenForm "frmRelinkJetORACEtables", , , , , , Me.Name
        Case Else
            MsgBox Err.Number & " -- " & Err.Description
    End Select
End Sub
 
Last edited:

cricketbird

Registered User.
Local time
Today, 13:43
Joined
Jun 17, 2013
Messages
108
Try to open the Settings form as a modal instead so it will wait until the user relinks the BE before passing control back to the calling sub.

Cheers,
I tried this first as the simplest solution, and it worked beautifully! Thank you!
 

cricketbird

Registered User.
Local time
Today, 13:43
Joined
Jun 17, 2013
Messages
108
Thanks everyone - it wasn't re-linking that was the problem. I ended up moving everything to Form_Open and changing the "settings" form to modal, and that did the trick.
 

Users who are viewing this thread

Top Bottom