Results 1 to 2 of 2

Thread: [Resolved] Problem Initiating Parameterized Class in VBA - Syntax Problem?

  1. #1

    Thread Starter
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Resolved [Resolved] Problem Initiating Parameterized Class in VBA - Syntax Problem?

    Hey, guys.

    I am using classes for the first time in VBA and I can't initialize my classes properly. I have two classes as follows:

    (1) TestMonth
    Code:
     
    Private b_Included As Boolean
    Private db_Hours As Double
    Public Property Get Included() As Boolean
        Included = b_Included
    End Property
    Public Property Get Hours() As Double
        Hours = db_Hours
    End Property
    Public Property Get Self() As TestMonth
        Set Self = Me
    End Property
    Public Property Get Value(ByVal ARate As Double) As Double
        If Included Then
            Value = Hours * ARate
        Else
            Value = 0
        End If
    End Property
    Public Property Let AMonth(ByVal Value As TestMonth)
        Set Me = Value
    End Property
    Public Function Create(ByVal ToInclude As Boolean, ByVal SomeHours As Double) As TestMonth
        With New TestMonth
            .b_Included = ToInclude
            .db_Hours = SomeHours
            Set Create = .Self
        End With
    End Function
    and (2) TestBill:
    Code:
    Private tm_Month1 As TestMonth
    Private tm_Month2 As TestMonth
    Private tm_Month3 As TestMonth
    Private db_ARate As Double
    Public Property Get Month1() As TestMonth
        Month1 = tm_Month1
    End Property
    Public Property Get Month2() As TestMonth
        Month2 = tm_Month2
    End Property
    Public Property Get Month3() As TestMonth
        Month3 = tm_Month3
    End Property
    Public Property Get Report() As String
        Dim TheVal As Double
        TheVal = tm_Month1.Value + tm_Month2.Value + tm_Month3.Value
        Report = "The total value of this bill is " & TheVal
    End Property
    Public Function Create(Optional ByVal FirstMonth As TestMonth = Nothing, _
            Optional ByVal SecondMonth As TestMonth = Nothing, Optional ByVal _
            ThirdMonth As TestMonth = Nothing, Optional ByVal ARate As Double _
            = 0) As TestBill
        Set tm_Month1 = FirstMonth
        Set tm_Month2 = SecondMonth
        Set tm_Month3 = ThirdMonth
        db_ARate = ARate
    End Function

    I have a simple Form (UserForm1) which has three checkboxes labeled for months 1, 2, and 3 and five text boxes (the first three textboxes correspond to the checkboxes and take hour values for each month; the fourth textbox takes a comp rate which is passed back to the TestBill object; and the fifth textbox is a readonly I use for getting back the report.

    The code for the UserForm is as follows:
    Code:
    Private Sub CmdGo_Click()
        Dim MyFirstMonth As TestMonth
        Dim MySeondMonth As TestMonth
        Dim MyThirdMonth As TestMonth
        Set MyFirstMonth = TestMonth.Create(Me.ChkBxIncludeMonth1.Value, CDbl(Me.TBoxHours1.Text))
        Set MySecondMonth = TestMonth.Create(Me.ChkBxIncludeMonth2.Value, CDbl(Me.TBoxHours2.Text))
        Set MyThirdMonth = TestMonth.Create(Me.ChkBxIncludeMonth3.Value, CDbl(Me.TBoxHours3.Text))
        Dim MyTestBill As New TestBill
        Set MyTestBill = TestBill.Create(MyFirstMonth, MySecondMonth, MyThirdMonth, CDbl(Me.TBoxRate.Text))
        Me.TBoxReport.Text = MyTestBill.Report
    End Sub
    The execution halts on the colored line with a run-time 424 object required error. I understand that I am failing to initialize my object properly but I don't think I am getting the syntax right or maybe the order.

    I'd really appreciate some help.

    Thank you in advance.
    Last edited by New2VB; Sep 7th, 2021 at 10:59 PM. Reason: [Reolved]

  2. #2

    Thread Starter
    Addicted Member New2VB's Avatar
    Join Date
    Jun 2000
    Location
    Los Angeles
    Posts
    161

    Resolved [RESOLVED] Problem Initiating Parameterized Class in VBA - Syntax Problem?

    The problem was in using the Create function. That line should have read:
    Code:
     Set MyFirstMonth = MyFirstMonth.Create(Me.ChkBxIncludeMonth1.Value, CDbl(Me.TBoxHours1.Text))
    and should have continued to implement the same syntax below. There are other problems with the code, but that was the one stumping me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width