 |
|
 |
chintu4u Level: Graduate
 Registered: 22-03-2006 Posts: 11
|
Related To Error Handling
Hi frd's is it possible to get in which sub or function and error has occured while using error handling.I m maintaining a Error Log in which wht's the Err.Number is and Err.Decription but it doesn't give in which particular sub or function of a particular form error has occured.As below code if user enters a specialization which is already exist in list then it will give error Specialization already exist and in Error Log it will note :
"08/25/06 12:28:22 PM -2147217873 Violation of PRIMARY KEY constraint 'PK_SPECIALIZATION_DETAILS'. Cannot insert duplicate key in object 'SPECIALIZATION_DETAILS'."
I also want tht know on which event or sub or function i.e over here on click of cmdsave_Click error has occured....
There is one thing tht in bellow line I shld write like this :
"Print #1, "cmdsave_click",Now, Err.Number, Err.Description"
which is not proper way of generalise coding, in each form in each event I have to write it.
***************************************************
Private Sub cmdsave_Click()
On Error GoTo Err:
If txtspecializationtype.text = "" Then
MsgBox "INSUFFICENT DATA" & vbCrLf & "FIELDS MARK WITH * ARE COMPULSORY", vbExclamation + vbOKOnly
Else
Call Removspc_UppCase(Me)
Call SaveClick_ED(Me)
adospecializatontype.Enabled = True
adospecializatontype.Recordset.Update
adospecializatontype.Refresh
Call FillList
MsgBox "YOUR RECORD HAS BEEN SAVED", vbInformation + vbOKOnly
Frame1.Enabled = False
adospecializatontype.Recordset.Requery
adospecializatontype.Refresh
adospecializatontype.Caption = "Total:" & adospecializatontype.Recordset.RecordCount
End If
Exit Sub
Err:
Open App.Path & "\ErrLog.txt" For Append As #1
Print #1, Now, Err.Number, Err.Description
Close #1
If Err.Number = -2147217873 Then
MsgBox "Specialization Type you are trying to save already exist", vbInformation + vbOKOnly, "Information"
Else
MsgBox "SOME Error HAS OCCURED", vbCritical + vbOKOnly, "Error"
End If
End Sub
***************************************************
|
|
31-08-2006 at 10:11 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Related To Error Handling
Hello. One trick I use is a module-level/global string variable, like following: ' in an external module
Option Explicit
Public CurrentSubName As String |
Then, when I write each routine, I start it by setting this variable, as' in each module or form's module
Private Sub Form_Load()
CurrentSubName = Me.Name & " - Form_Load"
' .... other code....
End Sub
Private Sub btnClickMe_Click()
CurrentSubName = Me.Name & " - btnClickMe_Click"
' .... other code....
End Sub
Private Function MyFunction(arg As Variant) As Variant
CurrentSubName = Me.Name & " - MyFunction(" & arg & ")"
' .... other code....
End Function
Private Sub Form_Unload(Cancel As Integer)
CurrentSubName = Me.Name & " - Form_Unload(" & Cancel & ")"
' .... other code....
End Sub
' .... other Subs and Funx... | ...and so on. I can omit to set it for non-critical routines, asPrivate Sub cboMyCombo_Change()
' send the code to the Click event also when hitting keys in combo's text section
cboMyCombo_Click
End Sub
Private Sub cboMyCombo_Click()
CurrentSubName = Me.Name & " - cboMyCombo_Click/Change"
' .... other code....
End Sub |
Each time the code stops for an error, I can read the variable value, either by a Debug.Print instruction in an error handler section, or by a ?CurrentSubName in the Debug window, or mousing over the variable into the code, or even writing it into a logfile and then reading it.
Var's value is still set on the name of the last routine it has got thru, so it's the name of the routine that raised the error.
When step-by-step debugging, I can even comment the initial routine setting, in order to know what other sub has called the error-generating one, and so on.
Hope it helps.
[Edited by yronium on 02-09-2006 at 05:52 PM GMT]
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
02-09-2006 at 04:42 PM |
|
|
chintu4u Level: Graduate
 Registered: 22-03-2006 Posts: 11
|
Re: Related To Error Handling
Thx yronium for u r reply but is there any other way were there would be a genral coding over here in each sub or function we have to write its name tht I was knowing can u help me out tht we hav to write in genral...and not like this :
CurrentSubName = Me.Name & " - Form_Load"
plz send me soon..
I will be thankfull to u.
|
|
02-09-2006 at 05:35 PM |
|
|
|
|
 |
 |