Error trapping

D

Dean

I have created my form with my own set of navigation buttons, "First Record"
"Next", "Last", etc.

Each of the buttons is controlled by a Macro called from the On Click event.
I want to be able to trap errors so that if the user is at record 1 and
clicks the "previous" button they get a message saying this.
Do I have to have a ErrorHandler routine for each button or can/should I
have 1 routine that does them all?

Also how do I remove the standard system "Action Failed" macro name etc, etc
dialog box.

Any help would be appreciated.

Thanks
Dean
 
K

Klatuu

Macros are not strong on error handling. To get robust handling of your
controls, you really need VBA.
Here is something from my bag of tricks you can add to you code that will
prevent moving beyond either end of the record set. I have it in a standard
module named modFormOperations that I use for all common form handling.
This function is called from the form's Current event and will disable or
enable buttons as necessary so you can't move beyond the recordset limits.
Note the names of the controls in this code. I use the same names for the
controls all the time, so I can use it for any and all of my forms. You may
need to change the control names to suit your naming conventions. That is
unless you are not giving good names and using the meaningless names Access
provides, then change your control names to something that makes sense.

Code************************************

'---------------------------------------------------------------------------------------
' Procedure : SetNavButtons
' DateTime : 2/6/2006 09:36
' Author : Dave Hargis
' Purpose : Enables and Disables Nav buttons based on current record
positio
'---------------------------------------------------------------------------------------
'
Sub SetNavButtons(ByRef frmSomeForm As Form)

On Error GoTo SetNavButtons_Error

With frmSomeForm
If .CurrentRecord = 1 Then 'At The First Record
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
.cmdNextRec.SetFocus
.cmdFirstRec.Enabled = False
.cmdPreviousRec.Enabled = False
ElseIf .CurrentRecord = .Recordset.RecordCount Then 'At the
Last Record
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdPreviousRec.SetFocus
.cmdNextRec.Enabled = False
.cmdLastRec.Enabled = False
Else 'All the Other Records
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
End If
End With

SetNavButtons_Exit:

On Error Resume Next
Exit Sub

SetNavButtons_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure SetNavButtons of Module modFormOperations"
GoTo SetNavButtons_Exit

End Sub
*****End Code*******************

You call it like this:

Private Sub Form_Current()
Call SetNavButtons(Me)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top