Custom Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Albert D. Kallal

    #16
    Re: Custom Classes

    I give some thoughts and helpfull advice here:


    Using Class objects with ms-access.
    By Albert D. Kallal
    Tuesday, September 16, 2003

    Why would I want to use a class object in ms-access?


    --
    Albert D. Kallal (Access MVP)
    Edmonton, Alberta Canada
    pleaseNOOSpamKa llal@msn.com



    Comment

    • Keith Wilby

      #17
      Re: Custom Classes

      "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
      news:GkHbh.3984 09$5R2.297739@p d7urf3no...
      >I give some thoughts and helpfull advice here:
      >
      >
      Using Class objects with ms-access.
      By Albert D. Kallal
      Tuesday, September 16, 2003
      >
      Why would I want to use a class object in ms-access?
      >
      http://www.members.shaw.ca/AlbertKal.../WhyClass.html
      Thank you Albert, I will read that now.


      Comment

      • Keith Wilby

        #18
        Re: Custom Classes

        "jlepack" <jlepack@gmail. comwrote in message
        news:1164905447 .476289.149010@ l39g2000cwd.goo glegroups.com.. .
        Keith,
        >
        Your problem here is that oyu have followed sound programming
        principles. You have created your variables within your class as
        Private. Therefore, only methods and functions within your class can
        access them, same as a private variable anywhere else for that matter.
        >
        >
        As Gord correctly noted, these Get and Let methods can be accessed
        publicly and therefore will allow non-local functions and procedures
        that have an instance of your class to modify/view those values.
        Duly noted, thanks.
        >
        PS. You took the "unintersti ng path" and chose the employee and not
        the slot machine ;)
        That's the kinda guy I am ;)
        >
        Cheers,
        Jason Lepack

        Comment

        • Keith Wilby

          #19
          Re: Custom Classes

          "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
          news:GkHbh.3984 09$5R2.297739@p d7urf3no...
          >I give some thoughts and helpfull advice here:
          >
          >
          Using Class objects with ms-access.
          By Albert D. Kallal
          Tuesday, September 16, 2003
          >
          Why would I want to use a class object in ms-access?
          >
          http://www.members.shaw.ca/AlbertKal.../WhyClass.html
          Just a quick question Albert.

          "when I create and set the myTour = "tour id", then all of the recordsets
          for bus company, hotels, rates etc gets loaded into the object."

          Could you just expand a little on this please? How does the data get
          loaded, do you open a recordset in a subroutine? Still missing the point
          here I think!

          Thanks.

          Keith.


          Comment

          • Gord

            #20
            Re: Custom Classes


            Keith Wilby wrote:
            "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
            news:GkHbh.3984 09$5R2.297739@p d7urf3no...
            I give some thoughts and helpfull advice here:


            Using Class objects with ms-access.
            By Albert D. Kallal
            Tuesday, September 16, 2003

            Why would I want to use a class object in ms-access?

            http://www.members.shaw.ca/AlbertKal.../WhyClass.html
            >
            Just a quick question Albert.
            >
            "when I create and set the myTour = "tour id", then all of the recordsets
            for bus company, hotels, rates etc gets loaded into the object."
            >
            Could you just expand a little on this please? How does the data get
            loaded, do you open a recordset in a subroutine? Still missing the point
            here I think!
            >
            Thanks.
            >
            Keith.
            Hi, Keith.

            Here is a simple example of what I *think* Albert is driving at. Notice
            that the "Property Let "Salary_Gra de" not only saves the Salary_Grade
            you pass it, as in...

            objEmployee.Sal ary_Grade = 3

            ....but it also looks up the corresponding Annual_Salary and saves that
            as well, so you can retrieve it via...

            objEmployee.Ann ual_Salary

            ....without doing the lookup yourself.


            '-----class module code follows-----

            Option Compare Database
            Option Explicit

            Private mstrLastName As String
            Private mintSalary_Grad e As Integer
            Private mcurAnnual_Sala ry As Currency

            Public Property Get LastName()
            ' return property value
            LastName = mstrLastName
            End Property

            Public Property Let LastName(ByVal vNewValue As Variant)
            ' store property value
            mstrLastName = vNewValue
            End Property

            Public Property Get Salary_Grade() As Variant
            ' return property value
            Salary_Grade = mintSalary_Grad e
            End Property

            Public Property Let Salary_Grade(By Val vNewValue As Variant)
            ' store property value...
            mintSalary_Grad e = vNewValue
            ' ... and look up annual salary from reference table
            mcurAnnual_Sala ry = _
            DLookup("Annual _Salary", "Salaries", "Salary_Gra de=" &
            mintSalary_Grad e)
            End Property

            Public Property Get Annual_Salary() As Variant
            ' return property value
            Annual_Salary = mcurAnnual_Sala ry
            End Property

            Comment

            • Albert D. Kallal

              #21
              Re: Custom Classes

              >
              "when I create and set the myTour = "tour id", then all of the recordsets
              for bus company, hotels, rates etc gets loaded into the object."
              >
              Could you just expand a little on this please? How does the data get
              loaded, do you open a recordset in a subroutine? Still missing the point
              here I think!
              Yes, in code I could go

              dim tour1 as new clsrides
              dim tour2 as new clsRides

              tour1.TourID = 123 ' load tour 123
              tour2.TourID = 34 'load tour 34

              At this point, I have TWO tours loaded in ram, and thus go

              msgbox "hotel for tour 123 is " & Tour1.HotelName

              msgbox "hotel for tour 34 is" & Tour2.HotelName

              So, note how the above was VERY little coding, and I can have TWO tours in
              memory at the SAME time. And, even if I just need one in code...it is still
              much easer then a bunch of subroutines and global variables that would trip
              over them selves....

              The class object is over 600 lines of code in total.....

              Additional properties of the class object are outlined here:


              The left side does NOT display all properties, but gives you an idea.....


              the actual code in the class object for TourID is:

              Public Property Let TourId(LoadID As Long)

              ' Loads/setsup the tour object (pricing etc..)
              ' DO NOT load if id is same as passed....

              Dim myDB As Database
              Dim qryBusList As QueryDef

              Set myDB = DBEngine(0)(0)

              If m_TourId <0 Then
              ' tour is loaded, check for same tour...
              If m_TourId = LoadID Then

              ' tour already loaded...don't re-load
              Exit Property

              End If
              End If

              m_TourId = LoadID

              ' loads the load rates....

              Set m_rstTour = myDB.OpenRecord set("select * from tbltours where id = " &
              m_TourId)

              With m_rstTour
              If .RecordCount 0 Then

              m_FromDate = !FromDate
              m_Todate = !ToDate
              m_HotelId = !Hotel
              m_dtDepositDue = m_FromDate - !DaysDue

              ' load up hotel informaton
              Set m_rstHotel = myDB.OpenRecord set("select * from tblHotels where
              id = " & m_rstTour!Hotel )

              ' load up buses attachted to this tour
              Set qryBusList = myDB.QueryDefs( "qryTourBusList B")
              qryBusList.Para meters(0) = m_TourId
              Set m_rstBusList = qryBusList.Open Recordset

              ' load up seasonal pricing talbes
              Call TLoadRates

              ' set tax tax.
              If m_rstTour!NoGst = True Then
              m_GstRate = 0
              Else
              m_GstRate = gblGst(m_FromDa te)
              m_PstRate = gblPst(m_FromDa te)
              End If

              Else
              m_TourId = 0
              End If
              End With

              End Property



              --
              Albert D. Kallal (Access MVP)
              Edmonton, Alberta Canada
              pleaseNOOSpamKa llal@msn.com


              Comment

              • Albert D. Kallal

                #22
                Re: Custom Classes

                By the way, that appendix link where I outline that access class object is

                is part of larger article here:



                In that article, I explain why I used class objects in ms-access....

                --
                Albert D. Kallal (Access MVP)
                Edmonton, Alberta Canada
                pleaseNOOSpamKa llal@msn.com



                Comment

                • Keith Wilby

                  #23
                  Re: Custom Classes

                  "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
                  news:%Ooch.4118 71$R63.320926@p d7urf1no...
                  By the way, that appendix link where I outline that access class object is
                  >
                  is part of larger article here:
                  >

                  >
                  In that article, I explain why I used class objects in ms-access....
                  >
                  --
                  Albert D. Kallal (Access MVP)
                  Edmonton, Alberta Canada
                  pleaseNOOSpamKa llal@msn.com
                  >
                  >
                  >
                  Many thanks Albert (and Gord), I shall read though all of this today.

                  Regards,
                  Keith.


                  Comment

                  • Keith Wilby

                    #24
                    Re: Custom Classes

                    "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
                    news:GGoch.4118 63$R63.146206@p d7urf1no...
                    >
                    tour1.TourID = 123 ' load tour 123
                    tour2.TourID = 34 'load tour 34
                    >
                    At this point, I have TWO tours loaded in ram, and thus go
                    >
                    msgbox "hotel for tour 123 is " & Tour1.HotelName
                    >
                    msgbox "hotel for tour 34 is" & Tour2.HotelName
                    >
                    <snip>
                    >
                    the actual code in the class object for TourID is:
                    >
                    Public Property Let TourId(LoadID As Long)
                    >
                    Albert,

                    Dumb question: how do you supply the argument to the property? You have
                    "tour1.Tour ID = 123" but wouldn't you get an "argument not optional" error
                    there?

                    Thanks for your patience, I'll get the hang of this yet!

                    Keith.


                    Comment

                    • Gord

                      #25
                      Re: Custom Classes


                      Keith Wilby wrote:
                      "Albert D. Kallal" <PleaseNOOOsPAM mkallal@msn.com wrote in message
                      news:GGoch.4118 63$R63.146206@p d7urf1no...
                      >
                      tour1.TourID = 123 ' load tour 123
                      tour2.TourID = 34 'load tour 34

                      At this point, I have TWO tours loaded in ram, and thus go

                      msgbox "hotel for tour 123 is " & Tour1.HotelName

                      msgbox "hotel for tour 34 is" & Tour2.HotelName
                      <snip>

                      the actual code in the class object for TourID is:

                      Public Property Let TourId(LoadID As Long)
                      >
                      Albert,
                      >
                      Dumb question: how do you supply the argument to the property? You have
                      "tour1.Tour ID = 123" but wouldn't you get an "argument not optional" error
                      there?
                      Property Let statements behave differently than Function and Sub
                      statements do. 123 *is* the argument.

                      Comment

                      • Keith Wilby

                        #26
                        Re: Custom Classes

                        "Gord" <gdt@kingston.n etwrote in message
                        news:1165251261 .788681.111140@ l12g2000cwl.goo glegroups.com.. .
                        >
                        >>
                        >Dumb question: how do you supply the argument to the property? You have
                        >"tour1.TourI D = 123" but wouldn't you get an "argument not optional"
                        >error
                        >there?
                        >
                        Property Let statements behave differently than Function and Sub
                        statements do. 123 *is* the argument.
                        >
                        Thanks for your patience Gord. Another dumb question but hopefully the
                        answer will make the penny drop. In a simple example, if I have a table
                        "tblHoliday s" with fields "ID" and "Country" and I have a class
                        "clsHoliday ", how do I set and retrieve a "Country" property? I'm calling
                        the class using:

                        Dim hlHoliday As clsHoliday
                        Set hlHoliday = New clsHoliday
                        hlHoliday.HolID = "ABC123"

                        Here's the class code:

                        Property Let HolID(strID As String)

                        Dim db As DAO.Database, rs As DAO.Recordset
                        Dim strCountry As String
                        Set db = CurrentDb
                        Set rs = db.OpenRecordse t("Select * from tblHolidays Where ID = '" & strID &
                        "'")
                        With rs
                        If .RecordCount 0 Then
                        '*** What goes here to get the other properties? ***
                        End If
                        End With

                        rs.Close
                        Set rs = Nothing
                        db.Close
                        Set db = Nothing

                        End Property


                        Comment

                        • Gord

                          #27
                          Re: Custom Classes

                          Keith Wilby wrote:
                          "Gord" <gdt@kingston.n etwrote in message
                          news:1165251261 .788681.111140@ l12g2000cwl.goo glegroups.com.. .
                          >
                          Dumb question: how do you supply the argument to the property? You have
                          "tour1.Tour ID = 123" but wouldn't you get an "argument not optional"
                          error
                          there?
                          Property Let statements behave differently than Function and Sub
                          statements do. 123 *is* the argument.
                          >
                          Thanks for your patience Gord. Another dumb question but hopefully the
                          answer will make the penny drop. In a simple example, if I have a table
                          "tblHoliday s" with fields "ID" and "Country" and I have a class
                          "clsHoliday ", how do I set and retrieve a "Country" property? I'm calling
                          the class using:
                          >
                          Dim hlHoliday As clsHoliday
                          Set hlHoliday = New clsHoliday
                          hlHoliday.HolID = "ABC123"
                          >
                          Here's the class code:
                          >
                          Property Let HolID(strID As String)
                          >
                          Dim db As DAO.Database, rs As DAO.Recordset
                          Dim strCountry As String
                          Set db = CurrentDb
                          Set rs = db.OpenRecordse t("Select * from tblHolidays Where ID = '" & strID &
                          "'")
                          With rs
                          If .RecordCount 0 Then
                          '*** What goes here to get the other properties? ***
                          End If
                          End With
                          >
                          rs.Close
                          Set rs = Nothing
                          db.Close
                          Set db = Nothing
                          >
                          End Property
                          Hi, Keith. Try this:

                          '-----start of class module----
                          Option Compare Database
                          Option Explicit

                          Private mstrHolID As String
                          Private mstrCountry As String

                          Public Property Get HolID() As String
                          ' return the property value
                          HolID = mstrHolID
                          End Property

                          Public Property Let HolID(ByVal strID As String)
                          Dim db As DAO.Database, rs As DAO.Recordset

                          ' save the ID property value
                          mstrHolID = strID

                          ' retrieve other attributes
                          Set db = CurrentDb
                          Set rs = db.OpenRecordse t( _
                          "Select * from tblHolidays Where ID = '" & strID & "'")
                          With rs
                          If .RecordCount 0 Then
                          '*** What goes here to get the other properties? ***
                          mstrCountry = rs!Country
                          End If
                          End With

                          rs.Close
                          Set rs = Nothing
                          db.Close
                          Set db = Nothing

                          End Property

                          Public Property Get Country() As String
                          ' return the property value
                          Country = mstrCountry
                          End Property
                          '-----end of class module----


                          '-----start of usage example----
                          Sub HolidayTest()
                          Dim hlHoliday As clsHoliday

                          Set hlHoliday = New clsHoliday
                          hlHoliday.HolID = "ABC123"
                          Debug.Print hlHoliday.Count ry

                          Set hlHoliday = Nothing
                          End Sub
                          '-----end of usage example----

                          Comment

                          • Keith Wilby

                            #28
                            Re: Custom Classes

                            "Gord" <gdt@kingston.n etwrote in message
                            news:1165316503 .135209.145400@ n67g2000cwd.goo glegroups.com.. .
                            >
                            Hi, Keith. Try this:
                            <snip>

                            Duh, I feel so stupid now ;-) The penny has finally dropped, for this
                            example at least. Many thanks again Gord.

                            Keith.


                            Comment

                            Working...