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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error checking 1

Status
Not open for further replies.

newVBuser

Programmer
Jan 5, 2009
3
Hello,

I am quite new to this so bare with me...

I have some code which works but I would like to add an error checking facility...

I wont go into detail on the code as I dont think it is necessary and I think all I require is something simple.

I have an user input box where the user enters a name, the code then checks that this name exists in a table, and contines if it does and throws an end/debug error if it isnt. Is there any way I can catch this error and replace it with a simple user-friendly message?

I currently have:

MyTable.MyField = InputBox("Enter heading name: ")

I then want something thing like...

If an exception is thrown
msgbox "error: please try again"
Exit sub

Else

contine...


Just something simple like that if it is possible.

Cheers guys.

 
A standard error handling block might look like this:

Code:
Sub MySub()
On Error GoTo HandleErr

    Here = SomeCode

ExitHere:
    Exit Sub

HandleErr:
    Select Case Err.Number
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "mdlModule.MySub"
    End Select
End Sub

You can step though and find the error number and add it as a case, however, it is always best to avoid errors and this can usually be done with a little rewriting of code, which is why it s often best to post your code.

 

What about instead of input box, you give the user a drop down combo, set it to drop down list, so they can just choose the name instead of typing it?

No need to check if they typed the correct name, they have to choose from the list so you do know the name is in the database.

But don't give up on error handling - very important stuff to know :)

Have fun.

---- Andy
 
I agree with Andy, the drop-down combo allows to avoid typo errors and reminds field names.
Please note that the behaviour of error trapping depends on settings (in VBE: tools>options, 'general' tab) and there is no programmatic access to it. After 'Break on All Errors' set the code will not skip to the error handler.

combo
 
I have to agree with Andy and combo. It is always best if you can avoid user-entered strings. A string is a string is a string, and VBA has ZERO contextual knowledge.

"Charles A. Bronson"
"zippety-do-dah"
"adghakdha ahdkhdakdhkhwrwy%^ladh"

are all the same to VBA (except for the length of the string of course). Strings do not mean anything to VBA, but they DO mean something to you, the programmer.

So, giving the user a selection of choices by a dropdown - ALL of which you have written - is a good way.

Of course, sometimes that is not possible, and you really do need a user-entered string. Although I have to say that good programming actually can reduce these far more than most people realize.

In which case, I agree again with Andy. Do not give up on error handling, it IS very important stuff to know.

Bottom line, though, is what Remou stated.

"however, it is always best to avoid errors and this can usually be done with a little rewriting of code, "

That, and his basic error handling code, is about it.

PS. hey dhookom, I had not seen that MZ-Tools. Thanks! Handy.

Gerry
 
Just have to say thanks again to Duane (dhookom) for that reference. That M-Z-Tools is VERY handy. Once set up, it is speeding things up dramatically. I use keyboard shortcuts a lot, and being able to quickly add things as I type is simply fantastic. Love it. It finally makes the VBE a modern and friendly environment to work in.

Gerry
 
Gerry, why not giving a star to Duane ?????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top