Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    JustCallMeAl
    Guest

    Property Get/Let (VBA/Word97 SR2)

    How about that? Gone weeks without any questions, work just chugging along, making good headway, then BOOM, BAMM.

    My understanding of Property Get/Let is this: These allow data to be stored as a property of a userform. Property Get makes that data available to be read from outside the form while protecting the data from change. Property Let "lets" you change the data from outside the form.

    Now, I have had great success in using Property Get alone to store information as a result of userform selections, and accessing that information from the outside.

    So, says I, assigning, from outside, a value to a property should be nothing but using Property Let alone.

    So I set up a Property Let. Ran the form, and got an error. In tracking down the error, I found out that a Property Get was required when you use the Property Let, but you can get along without a Property Let if you use a Property Get. In other words, you can use Get alone, but you can't use Let alone.

    Can some understanding on this be provided? Why does the Let require a Get, but the Get does not require a Let?

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    As I don't really know how you are implementing your use of Property Get, I cannot really understand why it even works for you out side of a class module. Whilst I think they are permitted outside of class modules, such use does not make much sense.

    As far as I know you must have a match in your Property Gets and Property Lets ( or Property Set when dealing with objects). Property Let is used to allow changes to the value of a Property, and hence is meaningless without the Property.

    Hope that helps explain.

    Andrew C

  4. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    It's probably working in the userform because the userform's code module is a class module.

  5. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Property Get/Let (VBA/Word97 SR2)

    You don't really need a Property Get/Let to do this with a form. You can simply declare a public variable at the module level for that form and it functions as a public property that can be read or set from outside the form. Of course, if you do this, you have no control over what goes into the variable/property. To do that, you must use the Property Let/Get.

    I wasn't aware that MS Forms required a Get for every Let. Certainly, Access classes don't have that restriction and VB classes don't either. You still have to use the correct syntax for a Let, but I've got plenty of class modules with Property Lets in them without a matching Get. Generally, I use those to allow conditions or flags to be set where the user doesn't need to know the existing value, only to set his preference.
    Charlotte

  6. #5
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    Get, Set and Let are used in class modules (which forms are) to assign and query values. I've never seen them used in a userform but they can be since it's a class but I don't see why they're needed since typically forms contain controls and the controls properties are the ones you care about. I guess I need to understand your usage, however.

    GET is like a function in that it 'gets' some value that's typcally stored as private in the class. GET/SET are usually in pairs since you normally want to store some value then retrieve it later (SET it first, then GET it later). You can create read-only properties by supplying only GET not SET.

    <pre>Property Get MyName() as String
    MyName = m_localname
    End Property
    </pre>

    Usage: person = obj.MyName

    LET is similar to GET but it needs some variable on the left side (hence the "L" since that's how I remmber if. It too is like a function.
    <pre>Property Let MyName(byval newname as String)
    m_localname = newname ' private variable
    End Property
    </pre>

    Usagebj.MyName = "Deb"

    "obj" is an object of the type of class you're using (declared as Dim obj as New someClass) in your case UserForm is the type (the class) and by default the object name is UserForm1 so that's why you do

    <pre>UserForm1.Caption = "blah blah blah"</pre>

    and Caption is a public SET or function (they look the same from the user's side) inside the object.

    SET is used in place of LET when the property is an object data type.

    Unless you specifically created your own class modules, there should be need for GET, SET, LET. This can be a very confusing part of programming languages (those that support classes and OO generally) so I'd leave it for an advanced usage although classes are really great ways to organize your project into workable pieces. It takes more upfront planning, however.

    For pure guide beginners to OO programming, John Smiley's "Learn to Program Objects with VB 6" is well done and uses a unique teaching approach (you follow a group of students learning this subjec through a class). A bit hokey but good for the raw beginner.

    Deb <img src=/S/clown.gif border=0 alt=clown width=15 height=15>

  7. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    For what it's worth, if you keep the form object in memory, you can access many of its properties even without global variables:

    <pre>Sub FormProperties()
    Dim frmUF As New UserForm1
    With frmUF
    .Tag = "Some Value"
    .TextBox1.Value = "My Default String"
    .CommandButton1.Caption = "Click Me You Fool!"
    .Show
    MsgBox .TextBox1.Value
    End With
    Unload frmUF
    Set frmUF = Nothing
    End Sub</pre>


  8. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    Thanks Deb for this explanation.

    Is it accurate to say that we're constantly using property Get and Set in VBA (every time we set or return the value of any property of any object), but the messy details of the functions are hidden from us? - you allude to that in your
    UserForm1.Caption = "blah blah" example, but it's true outside of userforms/class modules as well:

    myRange.Text = strText
    is a kind of Property Set - set the Text property of myRange = strText

    strText = myRange.Text
    is a kind of Property Get - get the Text property of myRange and apply it to variable strText

    I don't know enough about this topic to do more than surmise, but it seems that the VBA object model works to encapsulate more basic programming functions, so that all the user has to think about is manipulating the symbolic objects, without doing the heavy lifting behind the scenes.
    - I'd welcome comment on whether this is a correct surmise as well as any more info!

    Gary

  9. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Property Get/Let (VBA/Word97 SR2)

    <hr>myRange.Text = strText
    is a kind of Property Set - set the Text property of myRange = strText<hr>
    Nope, it's a kind of Property Let. If you said Set myUserForm = Me, that would be a kind of Property Set.
    Charlotte

  10. #9
    JustCallMeAl
    Guest

    Re: Property Get/Let (VBA/Word97 SR2)

    I have to agree with this: Smiley's book(s) is/are quite good! Lots of good stuff in them.

  11. #10
    JustCallMeAl
    Guest

    Re: Property Get/Let (VBA/Word97 SR2)

    I'll put this as a response to myself, as several made the same comment.

    Why would I need definable properties in a userform?

    Let me give one or two examples.

    After the user has clicked OK on the form, it is nice to know that the OK button was clicked. In other words, there are several ways to exit a form, and I may want to know, after the form has been hidden, how the person exited the form.

    Therefore, I create a form property of UserClickedOK. It is boolean, and if the user clicks OK, then the value of that property is set to True. Now, I don't want outside the form to be able to change that value. Therefore, I insert ONLY a Property Get with its accompanying private variable declared in the declaration area like this:

    <pre>Option Explicit
    Private mblnUserClickedOK As Boolean

    Private Sub cmdOK_Click()

    If Not ValidateForm Then Exit Sub

    mblnUserClickedOK = True
    Me.Hide

    End Sub

    Public Property Get UserClickedOK() As Boolean
    UserClickedOK = mblnUserClickedOK
    End Property

    </pre>


    As you see, this example of real code also calls a validate function.

    So, now I can know from outside the form that the user had exited via the OK button. This has saved my behind several times.

    So by defining a userform property to handle whether a command button was used or not can be quite valuable. Also, since it is a property of the userform, it is very clear from outside the userform to what it refers.

    Another way that I use it is with textboxes and labels. Now I realize that I don't really *have* to, but to me, it is a little clearer (and less typing) to see:

    UserForm.FirstName

    instead of

    UserForm.txtFirstName.text

    outside the UserForm. (Now, I realize that in this particular example, I don't have to type ".text" as that is the default property of a textbox, but who knows what MS will do next year!)

    I found out, however, in the process of giving the above UserForm property a value (from outside the UserForm), that a Get must be used with the Let. From my understanding, the Let "lets" you assign that value from outside the UserForm, and since the Get could be used by itself, I thought the Let could too.

    And that prompted the question.

    (added a few minutes after clicking Post It.)

    I just thought of another reason. I make my userforms reusable. Therefore, I want all needed information inside the form, and any manipulation of data into a document is done outside the form. The userform is given information from the outside via Get/Let properties, and actions can be preserved (i.e., click a command button) and "seen" from outside the form via Get/Let properties. The userform is just a data gatherer then. Very similar, from what I understand, to how a class module works.

    I have found that creating my userforms with that in mind, I have an incredible amount of variety possible with each userform. Since VBA (Word97 version) does not allow for run-time control creation, by setting userform properties (custom ones that I give it), I can really manipulate the look of the form. In a way, it is a method of "passing" values to the userform.

  12. #11
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    Yes, you're correct on the GET (it's like a function, it returns something into the variable provided). The SET is used to 'set' or 'assign' or 'store' a value to something inside of the object/class. Your example is for a LET (a good way to remember is that the object sits on the Left side of the equation so "L" is the reminder for me). For a UserForm, you'd 'assign' the caption to a new string.

    <pre>UserForm1.Caption = "hello there" 'property SET
    cap = UserForm1.Caption 'property GET</pre>


    Actually internally you don't know if the GET here is really a function, but you don't care, it does the same thing. So this takes your string "hello there" and stores it inside the object called UserForm1 and somehow it appears in the caption area (the 'how' is the object's responsibility).Because the caption is a read/write value you can also read the current caption using the internal GET property of the object.

    SET is only use if the thing you're "setting" or creating is an object itself. In VB/VBA you use the Set keyword as:

    <pre>Set myHouse = new House</pre>

    Since you created a class called House and that's an object (a thingy) that has attributes and methods to retrieve those attributes that allow you to assign/retrieve attributes about the House (its size, cost, address, color, # of bedrooms, the owner, etc.)

    Both VB and VBA encapsulate (fancy word for 'hide') tons of complicated Windows stuff behind the scenes which is both good (few lines of code to create a button) and bad (less control over what you can do). I appreciate this encapsulation a lot as in the "old" days I did Windows SDK programming where creating a button was 200 lines of code - forget about being able to move it or react to mouse clicks, yikes that's even more code. Any graphical interface is very complicated to deal with under the hood.

    You're benefiting from the work a programmer did to simplify a very complicated task of creating a button (for example) that has text, that can be re-positioned on the screen, that reacts to actions, etc. into just a few lines of code.

    You got it now! <img src=/S/fanfare.gif border=0 alt=fanfare width=31 height=23>
    Deb

  13. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    Al,

    Two questions:

    1) Do you create a property for EVERY control on EVERY userform? And if so, is that overhead really worth it?

    2) When you assign values to these properties from outside the form, do any events fire? Like the form initialize event?

    A very interesting technique nonetheless.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #13
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Property Get/Let (VBA/Word97 SR2)

    You could achieve the same by having the userform set a value in the calling routine, rather than the calling routine interrogate values in the userform.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  15. #14
    JustCallMeAl
    Guest

    Re: Property Get/Let (VBA/Word97 SR2)

    1. No, I do not create a Property for every control.

    2. Sometimes. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> I usually use load frmUserForm and at that time, frmUserForm_Initialize fires. Just after the Load command, if there is a property assignment, I do it then. I have not noticed any events firing, except for the Property Let (which is not an event?).

  16. #15
    JustCallMeAl
    Guest

    Re: Property Get/Let (VBA/Word97 SR2)

    You're referring to the UserClickedOK property?

    Could you explain a little more with an example?

    Thanks

Page 1 of 2 12 LastLast

Posting Permissions

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