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

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
 
F

Fred Boer

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
 
G

Guest

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
 
Joined
Apr 26, 2020
Messages
1
Reaction score
0
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
 

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