Last Record Error

A

Arturo

I am using the following code in the Timer Event.

Private Sub Form_Timer()
DoCmd.GoToRecord , , acNext
End Sub

When I get to the last record, how do I get it to return to the first record
instead of gettng the error message "You can't go to the specific record?"

Thank you.
 
K

Ken Sheridan

Trap and handle the error like so:

Const NOMORERECORDS = 2105

On Error Resume Next
DoCmd.GoToRecord , , acNext
Select Case Err.Number
Case 0
' no error
Case NOMORERECORDS
' go to first record
DoCmd.GoToRecord , , acFirst
Case Else
' unknown error
MsgBox Err.Description, vbExclamation, "Error"
End Select

Ken Sheridan
Stafford, England
 
D

Dirk Goldgar

(re-posting, as my original reply hasn't appeared)

Arturo said:
I am using the following code in the Timer Event.

Private Sub Form_Timer()
DoCmd.GoToRecord , , acNext
End Sub

When I get to the last record, how do I get it to return to the first
record
instead of gettng the error message "You can't go to the specific record?"

Thank you.


I don't know whether your form allows additions or not, so the code below
allows for the possibility that it doesn't. It does assume that you don't
want to show the blank "new record", if the form allows additions/

'----- start of code -----
Private Sub Form_Timer()

On Error GoTo Err_Handler

DoCmd.GoToRecord , , acNext

If Me.NewRecord Then
DoCmd.GoToRecord , , acFirst
End If

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2105 Then
DoCmd.GoToRecord , , acFirst
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
'----- end of code -----
 
A

Arturo

Both options worked great. Thanks.

Dirk Goldgar said:
I don't know whether your form allows additions or not, so the code below
allows for the possibility that it doesn't. It does assume that you don't
want to show the blank "new record", if the form allows additions/

'----- start of code -----
Private Sub Form_Timer()

On Error GoTo Err_Handler

DoCmd.GoToRecord , , acNext

If Me.NewRecord Then
DoCmd.GoToRecord , , acFirst
End If

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2105 Then
DoCmd.GoToRecord , , acFirst
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
'----- end of code -----


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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