eliminating the "You can't go to the specified record" error message

  • Thread starter Thread starter Paul James
  • Start date Start date
P

Paul James

I used the command button wizard to create Next Record and Previous Record
navigation buttons, but if you click on one of these buttons when you're in
the First or Last record, a useless error message appears saying "You can't
go to the specified record."

I'd like to trap this error so this doesn't happen. Can anyone tell me how
to do this?

Thanks in advance.

Paul
 
Dear Paul:

You could use error handling and trap the error, either ignoring it or
popping up a message, but it would probably be better not to let the user
generate the error. What might be preferable is to disable the appropriate
navigation buttons when you are at the beginning or end of the recordset. I
have the following code in the Current event of my forms:

Me.RecordsetClone.MoveLast
Me.cmdGoNext.Enabled = Not (Me.CurrentRecord =
Me.RecordsetClone.RecordCount)
Me.cmdGoPrevious.Enabled = Not (Me.CurrentRecord = 1)

The first line ensures that Access has all of the records loaded, the next
two will enable/disable the navigation buttons to avoid the error message.
When you are on the first record, the Previous Record button will be grayed
out and disabled, and when you are on the last record the Next Record button
will be disabled and greyed out.

I have seen lots of variations in code to accomplish this - someone might
suggest something better...

Alternatively, you might consider letting an Access Expert do the heavy
lifting: check out the following by Stephen Lebans:

http://www.lebans.com/recnavbuttons.htm

It's a complete solution that you can just drop into your forms...

HTH
Fred Boer
 
Fred,
This code works great and thank you for posting it. What if the user wants
to add another record? With the code that is posted, I can only move to the
next record, I can not add any more. Any suggestions? Thank you so very much,
Ryan
 
Fred,
This code works great and thank you for posting it. What if the user wants
to add another record? With the code that is posted, I can only move to the
next record, I can not add any more. Any suggestions? Thank you so very much,
Ryan
I know it has been 15 years but if needed you can add 1 to the recordcount

Code:
Private Sub Form_Current()
Me.RecordsetClone.MoveLast
Me.cmdGoNext.Enabled = Not (Me.CurrentRecord = Me.RecordsetClone.RecordCount + 1)
Me.cmdGoPrevious.Enabled = Not (Me.CurrentRecord = 1)
End Sub
 
Back
Top