Solved

Set Class Property

Posted on 2013-12-30
7
369 Views
Last Modified: 2013-12-31
I am trying to program a calendar and have created a class for the day buttons which captures the Click event for any of them. For the purpose of handling this event on class level I would like to create a Parent property for the buttons and assign the UserForm to it. This is the code I have,
in the class module 'CalButton'
    Public WithEvents CalBtn As MSForms.Label
    Public pParent As DatePicker

    Public Property Get Parent() As DatePicker
        Set CalForm = pParent
    End Property
    Public Property Set CalForm(CalForm As DatePicker)
        Set pParent = CalForm
    End Property

Private Sub CalBtn_Click()
    MsgBox CalBtn.Name & " was clicked."
    Debug.Print CalBtn.Parent.Name                  ' DOESN'T WORK
End Sub

Open in new window

in the code module of the UserForm (DatePicker - no relation of the Excel form by the same name)
    Dim Btn() As CalButton

Private Sub AssignCalButtons()
    ' assign all day button to CalButton class

    Dim Ctl As Object
    Dim i As Long

    ReDim Btn(1 To Me.Controls.Count)
    For Each Ctl In Me.Controls
        If InStr(1, Ctl.Name, "Day", vbTextCompare) = 1 Then
            i = i + 1
            Set Btn(i) = New CalButton
            Set Btn(i).CalBtn = Ctl
            Set Btn(i).Parent = Me                      ' Compile error: Invalid use of property 
        End If
    Next Ctl
    ReDim Preserve Btn(1 To i)
End Sub

Open in new window

The code runs and works without the Parent property idea. What am I doing wrong, and how to do it right?
0
Comment
Question by:Faustulus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 42

Assisted Solution

by:pcelba
pcelba earned 500 total points
ID: 39747777
Parent is built-in property which is read/only and always assigned by the object itself. It contains a reference to the parent container object.

If you really need to store the Form reference somewhere then create a new property for this purpose.

Update: If you would create your "buttons" as showed e.g. here: http://www.vbaexpress.com/forum/archive/index.php/t-31316.html   then the Parent property should contain the correct value without any assignment.
0
 
LVL 14

Author Comment

by:Faustulus
ID: 39747975
Yes. You are right. The Parent property is available in the code I have as well. I had tested that first and it failed for reasons I now can't find out. That's why I tried to assign the form as a property to the class. That failed, too. That is why I tried to assign it to the Parent property.
OK. The problem is solved. But can you tell me how I can assign the form as a property of the class?
I have been testing this:
In the class module,
    Public pCalForm As MSForms.UserForm
    
    Public Property Get CalForm() As MSForms.UserForm
        Set CalForm = pCalForm
    End Property
    Public Property Set CalForm(Cal As MSForms.UserForm)
        Set pCalForm = Cal
    End Property

Open in new window

And in the code (actually, the form's code module)
    Set CalButton.CalForm = Me

Open in new window

It stops at CalButton and claims "Variable no defined".
Happy New Year!
Faustulus
0
 
LVL 42

Expert Comment

by:pcelba
ID: 39748042
This could mean either CalButton or CalForm is undefined.
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 14

Author Comment

by:Faustulus
ID: 39748286
CalButton is the name of the class module.
CalForm is defined in the code I posted which is in the class module CalButton.
Where is the mistake?
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 39748366
The class definition does not create any variables. You must use some existing instance of the CalButton.

If you have button instances in Btn array:

Dim Btn() As CalButton
Set i = 1
Set Btn(i) = New CalButton   ' Now the Btn(1)  contains reference to the CalButton class instance

then you may write:

 Set Btn(i).CalForm = Me
0
 
LVL 14

Author Closing Comment

by:Faustulus
ID: 39748560
Thank you. Your support was was a great relief to me in my hours of trial (and error).
0
 
LVL 42

Expert Comment

by:pcelba
ID: 39748967
You are welcome!  

BTW, I don't use VB.NET at all. It seems I should install VS finally.

Happy New Year!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question