Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Error handler

  Asked By: Kuhaylah    Date: Jan 29    Category: MS Office    Views: 636
  

code:
----------

Private Sub cmdAdd_Click()

On Error GoTo errmsg

// main code: calculation work perform here.

On Error GoTo 0

errmsg:
MsgBox "Adding can't execute due to the textbox is empty.", vbOKOnly
End Sub

The purpose of the code above is to handle error when there is a
error the messange box will pop out.

However, the above code always pop up the message box even there is
not error occurs.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Daniel Evans     Answered On: Jan 29

You are always going to run the error  code unless you get out.

Try...

Private Sub cmdAdd_Click()

On Error GoTo errmsg

// main  code: calculation  work perform  here.

On Error GoTo 0

Exit sub ' Get out of here!!!!

errmsg:
MsgBox "Adding can't execute  due to the textbox  is empty.", vbOKOnly )

End Sub

In this particular case though... Why not just test to see if there is any
text in the text box?



Error handling comes up quite frequently on these lists and a lot of it is
down to personal preference. *My* preference FWIW is to deal with each error
seperately and "in line".

Option Explicit
Public igErrNum as integer
----------------

Sub subSomeSub

On Error Resume Next
// Single Line of code  that could error
igErrNum = Err.Number
On error goto 0
Select case igErrNum
Case 0
Case ....
Case ....
Case else
End select

Repeat for each line that could error.

End Sub

If anyone wants to jump in here with their own preferences please do feel
free to do so!

 
Didn't find what you were looking for? Find more on Error handler Or get search suggestion and latest updates.




Tagged: