Search code examples
vbams-access

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


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.


Solution

  • 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