![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
In this respect, let's say I have a maketable query. |
#12
| |||
| |||
|
|
My function will stop with any error at all and pop up a MsgBox informing the user of the error number and description. It's right there in the code. If that's not what you want, you'd have to describe it. What I meant was that in some occasions, if your function gives an error, I want the calling sub to stop as well. I think the only way to do that, is to give a False or True back to the calling sub. |
#13
| |||
| |||
|
|
Well, you could alter my function so that instead of returning the recordsaffected, it would return an error object. Since there's already an optional ByRef parameter for the recordsaffected, you would not lose any functionality. I often call it like this, though: Debug.Print SQLRun(strSQL) & " records added." ...and that would no longer work. So, you'd change the error handler to replace my MsgBox for the CASE ELSE and have it populate the error object you're returning. I'd be interested in hearing discussion of whether or not folks think this would be a better approach. |
#14
| |||
| |||
|
|
But for the production use, (and I remind that I struggle to think of a good example of query erroring out in runtime that isn't indicative of a bad design) I'm more likely to just settle on getting a return value of failure (e.g. -1 in the recordsaffected) than getting a custom Err object so I don't need to write error handling every time I call the function and can wrap it in a simple If/Then condition. |
#15
| ||||
| ||||
|
|
Well, given that the purpose of the function is to replace DoCmd.RunSQL, I'm loathe to make it harder to use. DoCmd.RunSQL doesn't return anything, and if you pass it bad SQL it will give you an error. |
|
Right now, it's a piece of code that's in a gray area for me -- I originally wrote it for other people to use because I got tired of explaining that if you used CurrentDB.Execute, you needed to use the dbFailOnError switch, and that meant you needed an error handler. |
|
But once I'd written it, I realized I was writing error handlers around .Execute statements, so I started using it myself. I wrote it to work the way I wanted it to work, and I'm happy with it returning the MsgBox when it errors out, since, as you say, at runtime it's going to be an indication of a bug that has to be fixed. If it doesn't inform the user of the error, then I have to handle it where I call it, and then I'm back to where I was before I started using it, i.e., writing an error handler for each instance. |
|
But I'm having a hard time imagining how what you've suggested would make it easier to use in a production app. It would mean writing more code around each call to it, and the whole point of writing it is to streamline code. Perhaps an optional parameter could control what is returned, e.g., and that, in turn, control whether the MsgBox kicks in or the code runs in "silent" mode and passes back an error number. But too many optional parameters then makes it harder to use, and the point is ease of use as a replacement for the inadequate DoCmd.RunSQL. |
#16
| |||
| |||
|
|
Modifying the function to return -1 in event of error for records affected is probably the least invasive change though we will lose any information about the actual error (unless it wrote to some kind of persistent placeholder). |
![]() |
| Thread Tools | |
| Display Modes | |
| |