Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    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)

    I have a module1 and Userform1

    Module1:<pre>Option Explicit
    Public blnOK As Boolean

    Sub Show()
    blnOK = False
    UserForm1.Show
    MsgBox blnOK
    End Sub</pre>

    In Userform1:<pre>Option Explicit

    Private Sub cmdOK_Click()
    Module1.blnOK = True
    Me.Hide
    End Sub
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  2. #17
    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)

    Another alternative is to call your userform from another class object (i.e., another userform) in the first place and use the WithEvents keyword to instantiate the called object and any of its controls you want to monitor. Then you can trap the events associated with the called userform and respond directly to things like the click event of a button on the user form without having to add any special code in the form itself. I do this all the time with Access and it works the same way in VB. I haven't done it in Word or Excel, but it should work exactly the same way there as well.
    Charlotte

  3. #18
    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, these were very informative. And Charlotte, I think I've got it now! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  4. #19
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    >Since VBA (Word97 version) does not allow for run-time control creation,

    Great Thread, good reading.

    Are you certain of this assertion above? I've written Word97SR2/VBA which dynamically adds tabs to a form, and i *think* I've written forms that add controls. I just can't lay my hands on it.

    And we ARE talking about adding a control, not hiding/showing or enabling/disabling an existing control.


    If you can't find a reference that says 'You can't create" I'm going to have to post a code sample which shows you CAN (grin)

  5. #20
    JustCallMeAl
    Guest

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

    I see. Thanks

  6. #21
    JustCallMeAl
    Guest

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

    There is a level of programming that I am comfortable with and easily understand. The information you have provided is moving me to another level--so, I need to chew on it a while.

    When I last looked at WithEvents for Word 97 VBA, I seem to remember there were some concerns/problems. In fact, I thought the problem was that it wasn't available in Word 97 VBA. However, after some more research, I find that it is.

  7. #22
    JustCallMeAl
    Guest

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

    Charlotte wrote:

    <hr>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.<hr>

    This is the one main reason I use the Property Get/Let, so I have absolute control over the values placed there.

  8. #23
    JustCallMeAl
    Guest

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

    Again, I'm going to reply to myself as several additional comments can be addressed together.

    Consider Geoff's thought, "You could achieve the same by having the userform set a value in the calling routine" and Charlotte's thought, "Another alternative is to call your userform from another class object (i.e., another userform) in the first place and use the WithEvents keyword..."

    How do these thoughts relate to encapsulation?

    In other words, since a userform module is a class module, and one of the basic purposes of classes (as far as I can remember) is to encapsulate (hide) what is going on, then it seems to me that defining properties for a userform beyond the default properties and controlling them with Property Get/Let encapsulates the information within the "class" (userform).

    Therefore, aren't these thoughts quoted above moving away from encapsulation?

    BTW, yesterday I came up with a new use for a Property Get/Let in a userform, which may explain what I am trying to say above.

    I am currently working on a userform which, among other things, determines a new filename for the ActiveDocument. This is based on several factors, including a choice between two optionbuttons.

    If the user chooses one optionbutton, the new filename is a variation of the existing one. If the user chooses the other optionbutton, the new filename is really new, based on information the user types in a textbox.

    I created a property DocumentName. Here is the code within the Property Get. One other explanation, there is an if statement based on whether a text box is visible. That decision of visible or not is based on the choice of one of the optionbuttons listed above.

    <pre>Public Property Get DocumentName() As String

    If txtMatterNumber.Visible Then
    DocumentName = txtMatterNumber.Text & "PCT"
    Else
    DocumentName = USMatterNumber & "PCT"
    End If 'txtMatterNumber.Visible

    End Property</pre>


    Therefore, by using a Property Get, I don't even have to know, in fact I don't care, outside the form, how that value for DocumentName was obtained, and that means I can use that form in different situations where all I am concerned about is a DocumentName.

  9. #24
    JustCallMeAl
    Guest

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

    I humbly bow in your correction.

    There are two issues that got confused in my mind: (1) Creating a control at run time and (2) control arrays.

    Word 97 VBA allows for creating a control at run time; however, it does not support control arrays.

    Thank you for that clarification.

  10. #25
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    > I humbly bow in your correction.

    On reflection, correction: "direction", surely?


    > Word 97 VBA ... does not support control arrays

    What's a Control Array? Please.

  11. #26
    JustCallMeAl
    Guest

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

    See. It doesn't support it, which is why you don't know about it. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    A control array is an array made up of similar controls. (Now, I am reaching way back for this, so I could be wrong.) Let's say you have 25 text boxes on a form, and you want to load the .text that the user enters into an array. You can declare those 25 text boxes as a control array (can't remember the exacty manner now), then you can just cycle through them, like a collection, and assign the value of the .text to the array. Allows for some really fast way of manipulating data.

    From VB6 help file:

    <hr>A group of controls that share common names, types, and event procedures. Each control has a unique index. When a control in the array recognizes an event, it calls the event procedure for the group and passes the index as an argument, allowing your code to determine which control recognized the event.<hr>

    It also allows for quick creation of controls at run time:

    <hr>Why Use Control Arrays?
    Adding controls with control arrays uses fewer resources than simply adding multiple controls of the same type to a form at design time. Control arrays are also useful if you want several controls to share code. For example, if three option buttons are created as a control array, the same code is executed regardless of which button was clicked.

    If you want to create a new instance of a control at run time, that control must be a member of a control array. With a control array, each new element inherits the common event procedures of the array.

    Using the control array mechanism, each new control inherits the common event procedures already written for the array. For example, if your form has several text boxes that each receive a date value, a control array can be set up so that all of the text boxes share the same validation code.
    <hr>

  12. #27
    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)

    One thought.

    If, for performance/memory considerations, you had to close the form rather than hide it when you hit the "OK", the Property let/get would not work.

    You could achieve a degree of encapsulation by having a class module with variables you used only for interacting with forms.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  13. #28
    JustCallMeAl
    Guest

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

    Good points.

Page 2 of 2 FirstFirst 12

Posting Permissions

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