Azog the Debugger
Azog the Debugger

Reputation: 227

Microsoft Access VBA invalid use of property error

So, I have this class where I call a few classes where data is checked. They give back an error class, named Failcase. Now I get an error when I first set the error to true.

The error states:

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

The Failcase class looks like this:

Option Compare Database
Option Explicit

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

I use:

Upvotes: 1

Views: 659

Answers (2)

Vityata
Vityata

Reputation: 43595

You are using OOP without creating a new object of class Failcase. Try this in the module:

Option Explicit

Public Sub TestMe()

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

End Sub

In the 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

Thus, you would achieve encapsulation. With it, you may set a bit more rules for accessing your property - https://www.google.com/search?q=encapsulation+oop&oq=encapsulation+oop&aqs=chrome..69i57j0l5.3599j0j7&sourceid=chrome&ie=UTF-8

The code above was an example of early binding. This is another one, example of late binding, doing the same:

Public Sub TestLateBinding()

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

End Sub

Early and late binding have different pros and cons.

Upvotes: 2

Ali Osman Yavuz
Ali Osman Yavuz

Reputation: 417

It works like this.

Dim fail As New Failcase
fail.Success = True

Upvotes: 1

Related Questions