Home > Net >  What's the Best Way to Handle Data Entry Errors Inside Property Let in VBA
What's the Best Way to Handle Data Entry Errors Inside Property Let in VBA

Time:09-17

in the Property Let in a ModelClass, I'd like to know what's the best way to advice the client class (ViewClass) about each data entry error.

please, find the ModelClass sample code, as follows:

Option Compare Database
Option Explicit

Private m_idCompra As String

Public Property Get codCompra() As String
    codCompra = m_idCompra
End Property

Public Property Let codCompra(ByVal codigoCompra As String)
    'Data entry roughly treated, just to illustrate.
    If Len(codigoCompra) = 0 Then
        'Advice ViewClass somehow
    ElseIf Not IsNumeric(codigoCompra) Then
        'Advice ViewClass somehow
    'Elseif...
    
    Else
        m_idCompra = codigoCompra
        'Advice ViewClass somehow
    End If
End Property

m_idCompra must be String type, in this case.

thanks in advance.

CodePudding user response:

For similar cases, I add methods like:

Public Function SetCompra(ByVal codigoCompra As String) As Boolean

    Dim Valid As Boolean

    Valid = IsValidCompra(codigoCompra)
    If Valid Then
        ' Set property.
        m_idCompra = codigoCompra
    Else
        ' Raise error or pop message box.
    End If

    SetCompra = Valid

End Function


Public Function IsValidCompra(ByVal codigoCompra As String) As Boolean

    Dim Valid As Boolean

    If <your validation rules> = True Then
        Valid = True
    End If

    IsValidCompra = Valid

End Function


    

CodePudding user response:

An alternative approach is to continue to use Property Let, but add an Enum of error codes to your class to let the user determine what action to take if an error is raised:

eg MySimpleClass module:

Option Explicit

Private m_strId As String

Public Enum MSC_Errors
    'MS uses this offset to avoid in-built automation error codes AFAIK
    MSC_ID_EMPTY = vbObjectError   513
    MSC_ID_NOTNUMBER
End Enum

Public Property Get Id() As String
    Id = m_strId
End Property

Public Property Let Id(ByVal sNewVal As String)
    If Len(sNewVal) = 0 Then
        Err.Raise MSC_Errors.MSC_ID_EMPTY, Description:="Id cannot be empty string"
    ElseIf Not IsNumeric(sNewVal) Then
        Err.Raise MSC_Errors.MSC_ID_NOTNUMBER, Description:="Id must be a number"
    End If
    
    m_strId = sNewVal
End Property

Then as an example of client code:

Sub TestId()
    Dim myobj As New MySimpleClass
    
    On Error GoTo IdError
    
    myobj.Id = InputBox("Enter a numeric Id", "Id Entry")
    
    MsgBox "Good Id=" & myobj.Id
    
    Exit Sub
    
IdError:
    MsgBox Err.Description
    TestId
End Sub

The client can go further and query the Error code that you have provided, eg:

If Err.Number = MSC_Errors.MSC_ID_EMPTY Then
    'Assign a default id    
    myobj.Id=42
End If

Of course, this a somewhat contrived example, but it gives a way to communicate the reason for the error to the client code.

  • Related