Is This Bad Practice in Error Handling?

SyntaxSocialist

Registered User.
Local time
Today, 00:31
Joined
Apr 18, 2013
Messages
109
On a form (default view: Single), I have a bunch of bound controls that display one record at a time from tblMain. I have a bunch of unbound controls as well (buttons).

If the user tabs through all the controls, reaches the last one (btnLast), and presses tab again, the focus is set to the first control (btnFirst) and the bound controls change to display the next record. I just want the focus to be set to the first control without the displayed record being changed.

So I set up an "If" statement in Private Sub btnFirst_GotFocus()

Code:
If Screen.PreviousControl.Name = "btnLast" Then
        [code to change record back to previous record]
End If

But btnFirst gets the focus when the form opens, so Screen.PreviousControl spits out an error.

I've accommodated this like so:

Code:
On Error GoTo ErrorHandler

    If Screen.PreviousControl.Name = "btnLast" Then
        [code to change record back to previous record]
    End If
    
    Exit Sub

ErrorHandler:
    Exit Sub

Is this just bad practice? I'm sure many of you have lots of ideas on how I can accomplish this better. My application is a bit idiosyncratic, though, so I'm not sure if all of them will work.

Only one way to find out!
 
You don't need both Exit Subs . . .
Code:
On Error GoTo ErrorHandler

    If Screen.PreviousControl.Name = "btnLast" Then
        [code to change record back to previous record]
    End If
    
ErrorHandler:
    Exit Sub
. . . but if you put the form in design view, on the Other tab of the property sheet there is a property called Cycle. Set this to "Current Record" and the form will behave as you want, and not advance to the next record after tabbing forward off the last control, without you needing to write any code.

hth
 
You don't need both Exit Subs . . .
Code:
On Error GoTo ErrorHandler

    If Screen.PreviousControl.Name = "btnLast" Then
        [code to change record back to previous record]
    End If
    
ErrorHandler:
    Exit Sub
. . . but if you put the form in design view, on the Other tab of the property sheet there is a property called Cycle. Set this to "Current Record" and the form will behave as you want, and not advance to the next record after tabbing forward off the last control, without you needing to write any code.

hth

Holy scat-monkeys... I totally forgot about that feature!
 

Users who are viewing this thread

Back
Top Bottom