J
Jesse Aviles
Recently, I started developing a new project and was asked for some
capabilities that I think are better served using a class to represent the
table records. I have done a lot of what I want with the class. As I went
along, I tested different things to see how the class responded. I noticed
that some errors were from the class preocedures (which was being called
from a form) but were kind of hard to track. Reading the Access Help,
Access Developers Handbook and VBA Developers Handbook and added an
ErrorHandler as follows:
Err.Raise Err.Number "Sharetech.Propuesta"
Within the form events and functions I have a general error handler as
follows
msgbox "Error: " & Err.Number & vbNewLine & Err.Description & vbNewLine
& "In [FormEnvent or Function],,"Error"
I use this to catch unexpected errors and not see the standard Error Window.
My classes have several properties and functions (public and private) that
were called from the form. Code in the form called the functions in the
class and if there was an error, I received the msgbox error procedure
above. When the class generated an error (null value in a string variable),
the procedure jumped from the class to the form code and I ended up with the
default Error box (the one that has the end and debug buttons) with a very
cryptic message (which provided no information about the actual error).
Stepping through the procedures, I found out that the problem was Err.Raise
line (above). I thought that when the function in the class found an error
in execution, it would pass along the error number along with the class
name to the calling procedure so the error handler from the calling
procedure would present me the actual error that occured but thats not what
happened.
After all this, what I'm asking for is an overview of error handling within
a class. It seems that my perceptions on the way it was supposed to work
was not right. Below is the code that calls a function from the class and
the function itself.
<<<<<-----FORM FUNCTIONS----->>>>>
Private Sub cboPropuesta_AfterUpdate()
' Comments : Set the proposal record to one in the search box
(cboPropuesta)
' Parameters:
' Created : 10 Nov 2005 06:29 JA
' Modified :
' --------------------------------------------------
On Error GoTo ErrorHandler
Dim objPropuesta As Propuesta
Set objPropuesta = New Propuesta
objPropuesta.Propuesta = Me("cboPropuesta")
If objPropuesta.Load Then
Call LlenarForma(objPropuesta)
bolNewPropuesta = False
Else
MsgBox "Error al Buscar Propuesta"
GoTo ExitHandler
End If
Me.Caption = "Propuesta " & Me.txtPropID & " - " & Me.txtPropNombre
ExitHandler:
Set objPropuesta = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Num & vbNewLine & Err.Description & vbNewLine &
"In cboPropuesta_AfterUpdate."
Resume ExitHandler
End Sub
Private Function LlenarForma(objPropuesta As Propuesta)
' Comments : Llena la forma principal con la data de un objeto
Propuesta
' Parameters: objPropuesta debidamente inicializado
' Returns : -
' Created : 11 Nov 2005 06:53 JA
' Modified :
' --------------------------------------------------
On Error GoTo ErrorHandler
With Me
.txtPropID = objPropuesta.Propuesta
.txtPropNombre = objPropuesta.Nombre
.txtluCompania = objPropuesta.Compania
.txtluPropTipo = objPropuesta.Tipo
.dtPropFecha = objPropuesta.Fecha
.lutxtPueblo = objPropuesta.Pueblo
.sngDiasEst = objPropuesta.DiasEstimados
.curPrecioEst = objPropuesta.Precio
.lutxtEmpleado = objPropuesta.Empleado
.bolAceptada = objPropuesta.Aceptada
.meDescripcion = objPropuesta.Descripcion
End With
ExitHandler:
Exit Function
ErrorHandler:
MsgBox "Error " & Err.Num & vbNewLine & Err.Description & vbNewLine &
"In LlenarForma."
Resume ExitHandler
End Function
<<<<<-----CLASS FUNCTIONS----->>>>>
Public Function Load() As Boolean
Dim rst As ADODB.Recordset
On Error GoTo ErrorHandler
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblPropuesta WHERE txtPropID = '" & Me.Propuesta
& "'", _
Application.CurrentProject.Connection
With rst
If .EOF = False Then
'Cargar la información de los campos
mstrNombre = !txtPropNombre
mstrCompania = !txtluCompania
mstrTipo = !txtluPropTipo
mdtFecha = !dtPropFecha
mvarPueblo = !lutxtPueblo
msngDias = !sngDiasEst
mcurPrecio = !curPrecioEst
mstrEmpleado = !lutxtEmpleado
mbolAceptada = !bolAceptada
mvarDescripcion = !meDescripcion
End If
End With
Load = True
ExitHandler:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Exit Function
ErrorHandler:
' Err.Raise Err.Number, "Sharetech.Propuesta"
Load = False
Resume ExitHandler
End Function
Thanks
Jesse Aviles
j a v i l e s @ v e l o x i u s . c o m
capabilities that I think are better served using a class to represent the
table records. I have done a lot of what I want with the class. As I went
along, I tested different things to see how the class responded. I noticed
that some errors were from the class preocedures (which was being called
from a form) but were kind of hard to track. Reading the Access Help,
Access Developers Handbook and VBA Developers Handbook and added an
ErrorHandler as follows:
Err.Raise Err.Number "Sharetech.Propuesta"
Within the form events and functions I have a general error handler as
follows
msgbox "Error: " & Err.Number & vbNewLine & Err.Description & vbNewLine
& "In [FormEnvent or Function],,"Error"
I use this to catch unexpected errors and not see the standard Error Window.
My classes have several properties and functions (public and private) that
were called from the form. Code in the form called the functions in the
class and if there was an error, I received the msgbox error procedure
above. When the class generated an error (null value in a string variable),
the procedure jumped from the class to the form code and I ended up with the
default Error box (the one that has the end and debug buttons) with a very
cryptic message (which provided no information about the actual error).
Stepping through the procedures, I found out that the problem was Err.Raise
line (above). I thought that when the function in the class found an error
in execution, it would pass along the error number along with the class
name to the calling procedure so the error handler from the calling
procedure would present me the actual error that occured but thats not what
happened.
After all this, what I'm asking for is an overview of error handling within
a class. It seems that my perceptions on the way it was supposed to work
was not right. Below is the code that calls a function from the class and
the function itself.
<<<<<-----FORM FUNCTIONS----->>>>>
Private Sub cboPropuesta_AfterUpdate()
' Comments : Set the proposal record to one in the search box
(cboPropuesta)
' Parameters:
' Created : 10 Nov 2005 06:29 JA
' Modified :
' --------------------------------------------------
On Error GoTo ErrorHandler
Dim objPropuesta As Propuesta
Set objPropuesta = New Propuesta
objPropuesta.Propuesta = Me("cboPropuesta")
If objPropuesta.Load Then
Call LlenarForma(objPropuesta)
bolNewPropuesta = False
Else
MsgBox "Error al Buscar Propuesta"
GoTo ExitHandler
End If
Me.Caption = "Propuesta " & Me.txtPropID & " - " & Me.txtPropNombre
ExitHandler:
Set objPropuesta = Nothing
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Num & vbNewLine & Err.Description & vbNewLine &
"In cboPropuesta_AfterUpdate."
Resume ExitHandler
End Sub
Private Function LlenarForma(objPropuesta As Propuesta)
' Comments : Llena la forma principal con la data de un objeto
Propuesta
' Parameters: objPropuesta debidamente inicializado
' Returns : -
' Created : 11 Nov 2005 06:53 JA
' Modified :
' --------------------------------------------------
On Error GoTo ErrorHandler
With Me
.txtPropID = objPropuesta.Propuesta
.txtPropNombre = objPropuesta.Nombre
.txtluCompania = objPropuesta.Compania
.txtluPropTipo = objPropuesta.Tipo
.dtPropFecha = objPropuesta.Fecha
.lutxtPueblo = objPropuesta.Pueblo
.sngDiasEst = objPropuesta.DiasEstimados
.curPrecioEst = objPropuesta.Precio
.lutxtEmpleado = objPropuesta.Empleado
.bolAceptada = objPropuesta.Aceptada
.meDescripcion = objPropuesta.Descripcion
End With
ExitHandler:
Exit Function
ErrorHandler:
MsgBox "Error " & Err.Num & vbNewLine & Err.Description & vbNewLine &
"In LlenarForma."
Resume ExitHandler
End Function
<<<<<-----CLASS FUNCTIONS----->>>>>
Public Function Load() As Boolean
Dim rst As ADODB.Recordset
On Error GoTo ErrorHandler
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblPropuesta WHERE txtPropID = '" & Me.Propuesta
& "'", _
Application.CurrentProject.Connection
With rst
If .EOF = False Then
'Cargar la información de los campos
mstrNombre = !txtPropNombre
mstrCompania = !txtluCompania
mstrTipo = !txtluPropTipo
mdtFecha = !dtPropFecha
mvarPueblo = !lutxtPueblo
msngDias = !sngDiasEst
mcurPrecio = !curPrecioEst
mstrEmpleado = !lutxtEmpleado
mbolAceptada = !bolAceptada
mvarDescripcion = !meDescripcion
End If
End With
Load = True
ExitHandler:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Exit Function
ErrorHandler:
' Err.Raise Err.Number, "Sharetech.Propuesta"
Load = False
Resume ExitHandler
End Function
Thanks
Jesse Aviles
j a v i l e s @ v e l o x i u s . c o m