"You can't go to the specified record"

DataMiner

Registered User.
Local time
Today, 07:59
Joined
Jul 26, 2001
Messages
336
How do I trap the error "You can't go to the specified record"?

This occurs when I use

DoCmd.GoToRecord , , acNext (or acprevious)

on a form, but am at the end of the recordset.

I thought I could use the form's OnError event to trap for this, but this event does not seem to fire when this happens.

I would just like to give my users a more informative error message!

Using Access 2002
 
Here's a link back to an old thread which shows a DAO method (typical of A97).

I don't know ADO (typical of A2000 and A2002) but maybe someone can rustle up a ADO version of the code.

Alternatively, you can copy the code into your project and set your Database to DAO by adjusting the DAO reference's priority above the ADO reference.
 
You have to use the On Error event of the sub wherein lies you acNext or acPrevous code, not the form's On error event. The following is code I put behind command buttons to move to the Next Record and Previous Record.

Private Sub Go2Next_Click()
On Error GoTo Err_Go2Next_Click
DoCmd.GoToRecord , , acNext
[ClientName].SetFocus

Exit_Go2Next_Click:
Exit Sub

Err_Go2Next_Click:
DoCmd.GoToRecord , , acFirst
[ClientName].SetFocus

End Sub

Private Sub Go2Prev_Click()
On Error GoTo Err_Go2Prev_Click
DoCmd.GoToRecord , , acPrevious
[ClientName].SetFocus
Exit_Go2Prev_Click:
Exit Sub

Err_Go2Prev_Click:
DoCmd.GoToRecord , , acLast
[ClientName].SetFocus

End Sub

In order for the code to work with the acNext sub, the form's Allow Additions property must be set to No (otherwise the acNext command will take you to a blank record). I use a button to "Add a Record" and use the code "Me.AllowAdditions = True" behind the button to allow for additions. I then have a button called "Save New Record" which has the code "Me.AllowAdditions = False" behind it to turn off the Allow Additions.

If you choose to ignore the previous paragraph, when you reach the end of the recordset, a blank new record will appear, but if you hit "Next" a second time it will take you to the first record in the recordset.

Hope this helps.

The Missinglinq
 

Users who are viewing this thread

Back
Top Bottom