Solved

Info about property set function in class modules in VBA

Posted on 2007-12-02
5
3,771 Views
Last Modified: 2013-11-27
VBA for Access (but as a larger question for VBA and VB in general.)

The documentation I have access to gives a rather vague explanation of exactly what the property set function in a class module does and how it relates to the set statement in the standard module that calls it. I don't expect a full tutorial here, but perhaps a souce where I could find a (few) very simple example (so I don't have to wade through a lot of difficult code) in order to see how it works.

It seems to set a reference to an object. After that  is done, where exactly is the object available? Is it available to the standard module, or available only to the class module? This is one of those questions that you are not even sure what the question shoul be because the info is so scanty.
0
Comment
Question by:dondeb1
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Set is used in the basically the same way ... in that in both cases you are referring to an object variable you have defined ... and where that variable is available depends on the scope you defined ... Private, Public, Friend.

In a Class Module (which included Form and Report modules), you have Property Set, Property Let and Property Get.  Set is used is the same way ... to refer to an object variable ( such as a form, recordset, activeX control, etc) as opposed to a variable such as an Integer, String etc.

mx

0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Even though both contain the word set, they are used a little differently.  Set on it's own is used to link a variable to an object.  ie.

  Dim rst as DAO.recordset

  Set rst = DAO.OpenRecordset(....

  Here the variable being 'rst'.   It's scope depends on where rst was Dim'd.  If in the procedure, then it's local and will disappear when the procedure terminates.  if in a form or report, it's when that object closes.  And if in a module, it's for the life of the app.

  Property Set is a bit different.  What your doing is that your defining a property that will appear in a class and that property is a reference to an object.  That would look like this:

Property Set Pen(P As Object)

    Set CurrentPen = P    ' Assign Pen to object.

End Property

  Now when I create an instance of this class, it will have a property of .Pen, and that property will be working with an object.  The companion to that is Property Let.   Again you are defining the property of a class, but this time with a variable:

Property Let PenColor(ColorName As String)
    Select Case ColorName    ' Check color name string.
        Case "Red"
            CurrentColor = RED    ' Assign value for Red.
        Case "Green"
            CurrentColor = GREEN    ' Assign value for Green.
        Case "Blue"
            CurrentColor = BLUE    ' Assign value for Blue.
        Case Else
            CurrentColor = BLACK    ' Assign default value.
    End Select
End Property

   In this case, CurrentColor is the variable that is storing the value for this property.  Again when you create an instance based on this class, it will have a property of .PenColor.

  Property Get is the finial piece of defining properties for classes.  It looks like this:

' Returns the current color of the pen as a string.
Property Get PenColor() As String
    Select Case CurrentColor
        Case RED
            PenColor = "Red"
        Case GREEN
            PenColor = "Green"
        Case BLUE
            PenColor = "Blue"
    End Select
End Property

  In this case, were fetching the value of the property of a class.  internally within the class, that property is stored in the variable CurrentColor.

  So poperty set is is used as part of defining a class and it's properties.  SET on it's own is used outside a class to get a reference to an object variable.  Quite similar, but used for two very different things.

HTH,
JimD

 



0
 

Author Comment

by:dondeb1
Comment Utility
JDettman,

Your answer is very helpful but I wonder if you might flesh out this part a bit more.

Quote -----------------------------------------------------------------------------
  Property Set is a bit different.  What your doing is that your defining a property that will appear in a class and that property is a reference to an object.  That would look like this:

Property Set Pen(P As Object)

    Set CurrentPen = P    ' Assign Pen to object.

End Property
______________________________________________________________---------

The "P as object" being referenced, is that a part of an outside object being instantized within the class module, or is it part of the class module itself or is it something else entirely. That is not clear from your answer. I just need to know a bit more about what and where the objects, their relation to the class module and the outside code using the instantiation object, and what are the events surrounding this function. Does it get called from outside the module and result in some internal event? I am very new to writing class modules, in fact, this is my first effort.  
 
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 125 total points
Comment Utility
<<The "P as object" being referenced, is that a part of an outside object being instantized within the class module, or is it part of the class module itself or is it something else entirely.>>

  It's an outside object pointer that's being passed in.  Let's say you had a class that worked with processing text files and you wanted users of your class to be able to store a reference to a file.  You might setup something like this:

Private objSaveFile as TextFile

Property Set SaveFile (objFile as TextFile)

  Set objSaveFile =  objFile

End Property

  Now in code, someone using your class could do this:

  Set objDoc.SaveFile = New TextFile

  We now have a reference to a specific instance of the class TextFile and I can reference any of it's methods and properties through the object reference of SaveFile in my class.  ie.

  objDoc.SaveFile.Path = "C:\myApp\myApp.txt"
  objDoc.SaveFile.FileOpen

   .Path being a property of the object TextFile and .FileOpen one of it's methods.  Neither of those is part of our class; we just provided a means of pointing to an object with the property .SaveFile.

HTH,
JimD  
0
 

Author Closing Comment

by:dondeb1
Comment Utility
This question is such that there is no clear meaning for "complete" since it was pretty much open-ended. But the answer did give a sufficient handle on the subject to be very useful. If there were several gradations of "partial" instead of only one it would have been graded much nearer the "yes" end instead of in the middle. Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now