Capture the Text of a VBA Error Mesage

ruve1k

Board Regular
Joined
Aug 31, 2008
Messages
171
How can I capture the entire text of a VBA error message and save it into a string variable?

Note that I would like to capture the error message not only when it is a native VBA error message but also when it is a SQL error message that gets kicked through to VBA. (E.g. when the VBA connects to a SQL Server database and executes a number of SQL commands against the database.)
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
One way...

Code:
    On Error Resume Next
    'do whatever
    If Err.Number <> 0 Then MsgBox Err.Description
    On Error GoTo 0

I've used the above with ADO and DAO.

Some (all?) Windows APIs trap any error themselves and then return an appropriate error code. See the documentation for the specific API.
 
Upvote 0
Thanks! It worked well.
I did the following:

  1. I used
    Code:
    On Error Goto myErrorHandler
    to trap the error.
  2. Then I saved the error description into a string variable using the err.description property as you suggested and later wrote the error description to a SQL log table
  3. Then I experimneted with two options for throwing the error message:

  • Natively throwing the VBA error using [FONT=arial, helvetica]Err.Raise(number[, source, description, helpfile, helpcontext])[/FONT]
  • Throwing a pseudo-error using a VBA message box
Thanks again for your help.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top