Library tutorials & articles
Handling Errors in VB/VBA/VBS/ASP
By Mike J, published on 16 Oct 2001
Real-life Samples
…Or simply put the reusable code appendix.
Get a full file as string to your application
Retrieve Error data pertaining to a given error number.
Get all errors printed to a text file, if a proper description exists
The following function will print out all available errors to a text file called AllErrors.txt in the same location as your application file is stored.
Get a full file as string to your application
Public Function GetAFile(ByRef TheFile As String) As String
On Error GoTo ErrHandler
Dim F As Integer
F = FreeFile ' get a free file number (always in the range 1 - 255)
Open TheFile For Binary As #F 'open the file in binary mode
GetAFile = Input(LOF(F), #F)
Close #F ' close the file when your done
Exit Function ' if there was no error, don’t step further than here
ErrHandler: ' this is a named range called ErrHandler
MsgBox "An error happened here!" & vbCrLf & _
"Error number: " & Err.Number & vbCrLf & _
"Error Description" & Err.Description & vbCrLf & _
"Error Help Context ID" & Err.HelpContext & vbCrLf & _
"Error Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
Err.Clear ' destroy the error and…
Resume Next
End Function
Retrieve Error data pertaining to a given error number.
'// Form Code
'Create a form and ad a command button named Command1
Private Sub Command1_Click()
Call GetErrData(TheNumberHere)
End Sub
'// Module Code
Public Function GetErrData(ByRef ErrorNumber As Long)
On Error GoTo ErrHandler
Err.Raise (ErrorNumber) ' this brief call to the event handler Raises the error
' Note that the Err.Source would be of little help here since
' this would be the calling project that you are in.
Exit Function
ErrHandler:
MsgBox "This is the error info..." & vbCrLf & _
"Number: " & Err.Number & vbCrLf & _
"Description " & Err.Description & vbCrLf & _
"Help Context ID" & Err.HelpContext & vbCrLf & _
"Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
Err.Clear ' destroy the error and…
Resume Next
End Function
Get all errors printed to a text file, if a proper description exists
The following function will print out all available errors to a text file called AllErrors.txt in the same location as your application file is stored.
'// Form Code
'Create a form and ad a command button named Command1
Private Sub Command1_Click()
Call GetAllErrors()
MsgBox "The Error document has been created", vbExclamation,
"Finished!" End Sub
'// Module Code
Public Function GetAllErrors()
'clean out residual error tags
Err.Clear
On Error GoTo ErrHandler
Dim I As Long, F As Integer, ErrFile As String
Dim MessageTxt As String
ErrFile = App.Path & "\AllErrors.txt"
F = FreeFile
Open ErrFile For Output As #F
Print #F, "Err.Number|Err.Description|Err.HelpContext|Err.HelpFile"
For I = 1 To 65535 'all possible valid Long err numbers 'Loop
the numbers and call them one by one Err.Raise (I)
GoHere:
' On resuming after getting the error message, go here
'only print the messages clearly defined
If MessageTxt <> "" And _
InStr(1, Trim(MessageTxt), "Application-defined or", vbTextCompare)
= 0 Then
Print #F, MessageTxt
End If
Next I
Close #F
Exit Function
ErrHandler:
MessageTxt = Err.Number & "|" & _
Err.Description & "|" & _
Err.HelpContext & "|" & _
Err.HelpFile
Err.Clear
Resume GoHere ' Resume execution at the GoHere line label
End Function
Related articles
Related discussion
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
How to open .bat application from excel VBA or VB6
by NaseemAhmed (0 replies)
-
Outlook VBA query
by James Crowley (1 replies)
-
How to control IE from VBA
by NaseemAhmed (0 replies)
-
Run-time error '91'
by converter2009 (1 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Is there any way to handle errors in INFINITE LOOP.
for example,
While Not blnIdeaFound
blnIdeaFound = ProcessDetector(EXE_NAME, False)
Wend
IF THE CONTROL IS INSIDE THE LOOP ( INFINITE LOOP ), IS IT POSSIBLE TO HANDLE THIS CASE WITH A ERROR HANDLER ?
This thread is for discussions of Handling Errors in VB/VBA/VBS/ASP.