Microsoft Access VBA 无效使用 属性 错误

Microsoft Access VBA invalid use of property error

所以,我有这个 class,我调用了几个 classes 来检查数据。他们返回一个名为 Failcase 的错误 class。现在,当我第一次将错误设置为 true 时出现错误。

错误状态:

Invalid use of Property.

Private Sub btnImport_Click()
    Dim fail As Failcase
    Set fail.Success = True       '<---- This is where the error occures

    Set fail = ImportCheckSpec(Me.txtImportSpec)

    If fail.Success Then
        MsgBox "Error " + CStr(fail.Code) + ": " + fail.Message, vbCritical, "Error"
        Exit Sub
    End If

    Set fail = ImportCheckDate(Me.txtDateTime)

    If fail.Success Then
        MsgBox "Error " + CStr(fail.Code) + ": " + fail.Message, vbCritical, "Error"
        Exit Sub
    Else
        MsgBox "Success"
    End If
End Sub

故障案例 class 看起来像这样:

Option Compare Database
Option Explicit

Public Success As Boolean
Public Code As Integer
Public Message As String

我使用:

它是这样工作的。

Dim fail As New Failcase
fail.Success = True

您正在使用 OOP 而没有创建 class Failcase 的新对象。在模块中试试这个:

Option Explicit

Public Sub TestMe()

    Dim fail As New failcase
    fail.Success = True
    Debug.Print fail.Success

End Sub

在class中:

Option Explicit

Private m_bSuccess As Boolean

Public Property Get Success() As Boolean

    Success = m_bSuccess

End Property

Public Property Let Success(ByVal bNewValue As Boolean)

    m_bSuccess = bNewValue

End Property

这样,你就实现了封装。有了它,您可以设置更多规则来访问您的 属性 - https://www.google.com/search?q=encapsulation+oop&oq=encapsulation+oop&aqs=chrome..69i57j0l5.3599j0j7&sourceid=chrome&ie=UTF-8

上面的代码是early binding的一个例子。这是另一个,后期绑定的例子,做同样的事情:

Public Sub TestLateBinding()

    Dim fail As Object
    Set fail = New failcase
    fail.Success = True
    Debug.Print fail.Success

End Sub

早绑定和晚绑定各有利弊。