Navigation Buttons

D

Duane

Hello,

I have created my own set of navigation buttons but I am receiving a 2105
runtime error. I have an error handler that looks for the 2105 error and
should exit the sub, but I still get the error message. What is really odd
is that I copied the code from another database of mine (that works fine),
but not in this database. Any help would be appreciated. Thanks in advance.

I must be tired.

Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

DoCmd.GoToRecord , , acNext

Exit_cmdNext_Click:
Exit Sub

Err_cmdNext_Click:
If Err.Number = 2105 Then
DoCmd.GoToRecord , , acLast
Resume Exit_cmdNext_Click
Else
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdNext_Click
End If
End Sub
 
B

boblarson

If you are capturing the error 2105, you can't try to use the same code that
generated that error. Get rid of the DoCmd.GoToRecord , ,acLast from the
Error handler.

--
Bob Larson

Free Tutorials and Samples at http://www.btabdevelopment.com

__________________________________
 
D

Duane

Thanks Bob,

I have the code in the OnClick event of the cmdNext button. When I am at
the end of the recordset and click the command button I am still getting the
2105 error. I am being asked to End or Debug. Of course if I hit Debug,
the Visual Basic Editor opens and DoCmd.GoToRecord , , acNext is
highlighted. I realize I am into EOF and that is why I am getting the
error, but why isn't the Error Handler exiting me from the sub?

I removed the DoCmd.GoToRecord , ,acLast, but it doesn't make a difference.
Do you have any ideas to prevent this from happening?

Thanks.
 
B

Beetle

Modify your code like this;

Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

If Me.CurrentRecord = Me.RecordsetClone.RecordCount Then
Exit Sub
Else
DoCmd.GoToRecord , , acNext
End If

Exit_cmdNext_Click:
Exit Sub

Err_cmdNext_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdNext_Click
End Sub

Then you don't even need to trap for the error.
 
B

boblarson

Try this:

Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

DoCmd.GoToRecord , , acNext

Exit_cmdNext_Click:
Exit Sub

Err_cmdNext_Click:
If Err.Number <> 2105 Then
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdNext_Click
End If
End Sub

--
Bob Larson

Free Tutorials and Samples at http://www.btabdevelopment.com

__________________________________
 
K

Klatuu

Here is the code I use:

'---------------------------------------------------------------------------------------
' Procedure : cmdFirstRec_Click
' DateTime : 2/6/2006 09:04
' Author : Dave Hargis
' Purpose : Navigate to first record in recordset
'---------------------------------------------------------------------------------------
'
Private Sub cmdFirstRec_Click()
On Error GoTo cmdFirstRec_Click_Error

On Error GoTo Err_cmdFirstRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acFirst
End If

Exit_cmdFirstRec_Click:
Exit Sub

Err_cmdFirstRec_Click:
MsgBox Err.Description
Resume Exit_cmdFirstRec_Click

cmdFirstRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdFirstRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdFirstRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdFirstRec_Click_Exit

End Sub
'---------------------------------------------------------------------------------------
' Procedure : cmdPreviousRec_Click
' DateTime : 2/6/2006 09:04
' Author : Dave Hargis
' Purpose : Navigate to Previous Record
'---------------------------------------------------------------------------------------
'
Private Sub cmdPreviousRec_Click()
On Error GoTo cmdPreviousRec_Click_Error

On Error GoTo Err_cmdPreviousRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acPrevious
End If
Exit_cmdPreviousRec_Click:
Exit Sub

Err_cmdPreviousRec_Click:
MsgBox Err.Description
Resume Exit_cmdPreviousRec_Click

cmdPreviousRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdPreviousRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdPreviousRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdPreviousRec_Click_Exit

End Sub
'---------------------------------------------------------------------------------------
' Procedure : cmdNextRec_Click
' DateTime : 2/6/2006 09:04
' Author : Dave Hargis
' Purpose : Navigate to Next Record in Recordset
'---------------------------------------------------------------------------------------
'
Private Sub cmdNextRec_Click()

On Error GoTo cmdNextRec_Click_Error

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acNext
End If

cmdNextRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdNextRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdNextRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdNextRec_Click_Exit

End Sub
'---------------------------------------------------------------------------------------
' Procedure : cmdLastRec_Click
' DateTime : 2/6/2006 09:05
' Author : Dave Hargis
' Purpose : Navigate to Last Record in Recordset
'---------------------------------------------------------------------------------------
'
Private Sub cmdLastRec_Click()
On Error GoTo cmdLastRec_Click_Error

On Error GoTo Err_cmdLastRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acLast
End If

Exit_cmdLastRec_Click:
Exit Sub

Err_cmdLastRec_Click:
MsgBox Err.Description
Resume Exit_cmdLastRec_Click

cmdLastRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdLastRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdLastRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdLastRec_Click_Exit

End Sub

*************
Now, I also put this in the Form Current Event:

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

Which does this:

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

On Error GoTo SetNavButtons_Error

With frmSomeForm
If .CurrentRecord = 1 Then
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
.cmdNextRec.SetFocus
.cmdFirstRec.Enabled = False
.cmdPreviousRec.Enabled = False
ElseIf .CurrentRecord = .Recordset.RecordCount Then
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdPreviousRec.SetFocus
.cmdNextRec.Enabled = False
.cmdLastRec.Enabled = False
Else
.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

The SetNavButtons sub is in a standard module so it can be used from any
form.
 

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

Similar Threads


Top