Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to eliminate the ugly error message when I skip too far...

Status
Not open for further replies.

AccipiterQ

Programmer
May 25, 2005
27
US
I'm going to try to be as blunt as possible...I'm having a devil of a time figuring this out..
This is using Access 2000, and the Access Macro editor:

Alright, I've got a simple macro, called Ahead10, it uses the GoToRecord command, and for the offset I entered 10. I have a form i'm using this macro in. I made a button and attached the macro to it under the 'onclick event'. Now say there's 37 records in the form, I hit the button 3 times, and that takes me to record 31. But if I hit the button once more, I get an ugly 'action failed' dialogue box,becuase there's only 37 records, the action failed box looks unproffesional. I want to edit the macro so that if I hit the button and it would take me past the last record of the form it wouldn't display that ugly error message, it just tells me 'not enough records to skip this far' or something of that ilk. But I don't know what commands to use under Access's macro editor to do this Any help would be appreciated.

Thanks!

matt
 
Matt,

You've reached a great point at which to switch from macros to VBA code. One of the limitations of macros is that there is no error handling.

There's a menu option to convert your macro to VBA (I don't remember if you get to this from the database window or from the design view of the macro, but a little poking around will find it). Do this. Then hook your button up to use the code instead of the macro. The error message you get at this point should have an error number. You can use this error number in your error handler to branch your code so you can show a friendly message (or none) to your user.

Here's the basic shell of a function, with a very basic error handler (Please note this is untested aircode):

Code:
function CodeForMatt
on error goto ErrorHandler

{some code here}

ExitPoint:

Exit Function

ErrorHandler:
select case err.number
  case 234 'Made up error number, let's pretend it's for no disk in the drive
    Call MsgBox("Please insert a disk.")
  case else
    Call MsgBox(err.number & ": " & err.description)
end select

Resume ExitPoint:

end function

In your case, you would watch the error come up to learn what the error number is, change the first case (234) to whatever that error number is, and change the message to something you would want your users to see at this point, or just get rid of that line entirely, so nothing else happens.

Jeremy

---
Jeremy Wallace
METRIX Lead Developer
Fund for the City of New York
http:// metrix . fcny . org
 
How are ya AccipiterQ . . . . .

I couldn't agree with [blue]JeremyNYC[/blue] more. Upgrade yourself to [blue]the power of VBA![/blue] As an example, the following code makes jumps of 10 records, but if a jump is over the last record, it only goes as far as the last! Copy & replace you button code with the following ([blue]you![/blue] substitute proper names/values in [purple]purple[/purple]):
Code:
[blue]   Dim rst As DAO.Recordset
   
   Set rst = Me.RecordsetClone
   rst.FindFirst "[[purple][b]PrimaryKeyName[/b][/purple]] = " & Me![purple][b]PrimaryKeyName[/b][/purple]
   
   If rst.AbsolutePosition + 10 > rst.RecordCount - 1 Then
      DoCmd.RunCommand acCmdRecordsGoToLast
   Else
      rst.AbsolutePosition = rst.AbsolutePosition + 10
      Me.Bookmark = rst.Bookmark
   End If
   
   Set rst = Nothing[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top