Results 1 to 11 of 11

Thread: Effectively using Classes and Property Let/Get; referring to parent or sub class...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by xld
    I don't understand, why can't you access these properties?
    Hi Bob, I hope your trip was a good one :-)

    Well, I may have worded that poorly. The form's Name is but one property that doesn't seem to show up in intellisense, but I thought it a good example as it actually falls down if used.

    By example - in a new wb, if I create a new userform and insert code:
    [vba]Option Explicit

    Dim cButtons As clsCreatedButtons

    Private Sub UserForm_Initialize()

    Set cButtons = New clsCreatedButtons
    Set cButtons.ReferencedForm = Me

    cButtons.ReferencedFormName = Me.Name
    cButtons.CreateButton
    End Sub[/vba] And in the Class:
    [vba]Option Explicit

    Private CallingForm As MSForms.UserForm
    Private WithEvents MyButton As MSForms.CommandButton
    Private strFormName As String

    Property Set ReferencedForm(UForm As UserForm)
    Set CallingForm = UForm
    End Property
    Property Get ReferencedForm() As UserForm
    Set ReferencedForm = CallingForm
    End Property

    Property Let ReferencedFormName(UFormName As String)
    strFormName = UFormName
    End Property
    Property Get ReferencedFormName() As String
    ReferencedFormName = strFormName
    End Property

    Function CreateButton()
    Set MyButton = ReferencedForm.Controls.Add("Forms.CommandButton.1", "cmdBttn_1", True)
    With MyButton
    .Left = 20
    .Top = 20
    .Width = 64
    .Height = 18
    End With
    End Function

    Private Sub MyButton_Click()
    'With property ReferencedForm and variable CallingForm both declared As UserForm...
    CallingForm.Caption = "Hello"
    '...plunks "Hello" onto the userform; but it's BELOW the titlebar!

    'Also, I can get to the userform's name in a roundabout fashion, via...
    CallingForm.Caption = CallingForm.ActiveControl.Parent.Name
    '... (which of course still ends up below the titlebar), but if I try and use .Name...
    CallingForm.Caption = CallingForm.Name
    '... I get error 438, "Object doesn't support this property or method.
    End Sub[/vba] On the other hand, if I declare Private CallingForm As Object, as well as change it to As Object in the Property Set and Get, then when I step through (having already REM'd CallingForm.Caption = CallingForm.ActiveControl.Parent.Name) "Hello" shows up properly in the form's titlebar and everything is sunshine and flowers with CallingForm.Name.

    I would mention that I have a bit of a grip (albeit loosely) on some events not being available when a given activex control is created in a class, though I would not pretend as to know the exact 'whys'. I was figuring certain properties not being available might be similar, but am confused as to why if I declare as Object, certain things work (like .Name), whereas if As UserForm, it goes Kaboom!

    Similarily (and presuming I set the form's StartUpPosition to 0 during Initialize), then if it is passed to the class As Object, I can change the .Left and .Top properties, and watch it move. But As UserForm causes the same falling down.

    Again confusing is that when As UserForm, setting the the form's Caption results in the string appearing below the titlebar! I hope that I am not missing something really simple and that I worded that a bit better.

    Quote Originally Posted by xld
    Have you tried using the Properties collection?
    Do you mean like ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Name").Value ?

    Thank you,

    Mark
    Last edited by Aussiebear; 07-08-2011 at 02:37 AM. Reason: ...

Posting Permissions

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