UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Classes    
 
   
BananaRepublic
post Aug 11 2013, 03:13 PM
Post#1


Dungeon Cleaner
Posts: 1,504
Joined: 16-June 07
From: Banana Republic


Classes are awesome. A wiki article on classes is awesome. How can we make it more awesome-r?
Go to the top of the page
 
tina t
post Aug 11 2013, 04:03 PM
Post#2



Posts: 5,194
Joined: 11-November 10
From: SoCal, USA


i read the Wiki article on classes, BR. i know it was clear, but i have to admit - with no little embarrassment - that i still don't really get classes. very frustrating, because i know this lack of understanding is a big hole in my programming skill-set. i also think the problem is me, not the Wike article or other articles i've read over the years to try and understanding classes. (took me three months of almost constant head-banging, years ago, to have that lightbulb moment re using macros, so it's not like i'm always the sharpest knife in the drawer.)
don't have any suggestions for making the article "better". just thought i'd post so that any other folks out there who have a mental block like me, will know that they're not alone. :/
tina
Go to the top of the page
 
BananaRepublic
post Aug 11 2013, 04:10 PM
Post#3


Dungeon Cleaner
Posts: 1,504
Joined: 16-June 07
From: Banana Republic


Tina, don't forget that one other reason why classes are hard to use in Access environment is because IMHO, Access lends itself more toward procedural based processing and that's usually a good thing. We are using Access to solve business problems such as ensuring that invoice's total get calculated correctly. This can be done on a form or inside a query and maybe with a use of VBA function. That is more simple and straightforward approach.
urthermore, forms and reports themselves are classes, which make it easier for us to use forms/reports rather than a standalone class and still have many of the benefits.
There are uses for classes in Access. I can think of one (external functionalities such as zipping files, emailing or that sort) which it's easier to have a class to encapuslate things. Maybe we should list the reasons why to create/use classes. That may help a bit?
Go to the top of the page
 
jleach
post Aug 11 2013, 06:24 PM
Post#4


UtterAccess Editor
Posts: 9,793
Joined: 7-December 09
From: Staten Island, NY, USA


Hi Tina. If you're interested, a while back I posted a reply in regards to "when to use", which is a much less talked about topic than "how" to use them. The only other thing I recall seeing on the topic was something on Albert Kallal's website, but I don't know where (or if I'm even remembering correctly - it was years ago).
Post is here.
That said, I had intended (and voiced that intention, IIRC) to turn that particular post into the beginnings of a wiki article on when to use classes, and/or when to realize when your code might fit well for a class.
Another thing that may help, if I hadn't mentioned it in that post, is to have a good concept understanding of what a VBA UDT (User Defined Type) is and how it works, because on the conceptual level - the one that many people have a block on (myself included), the two concepts are very similar: classes, instead of *just*groups of variables like a UDT, also have functions and subs that can manipulate them.
So, I propose that we can make the Classes article awesome-r by trying to come up with a reasonable explanation of how to recognize when, where, and why. There's probably tens of thousands of articles on how, and the wiki's is no better or worse than hundreds of others, I'm sure, but getting that information tied in would make it exemplary.
Cheers.
(apparently I turned it into a blog post but never got around to cleaning it up enough to be fit for the wiki)
Go to the top of the page
 
tina t
post Aug 11 2013, 10:27 PM
Post#5



Posts: 5,194
Joined: 11-November 10
From: SoCal, USA


thanks, BR and Jack, for responding. haven't had that lightbulb moment yet (maybe i have a bad filament...), but i'll keep trying. Jack, i followed the link you posted, read that post and downloaded the zip file. um, i hate to sound even dumber, but...those .cls files are supposed to be opened (Notepad? Wordpad?) so a person can read them, uh, right? tina
Go to the top of the page
 
Galaxiom
post Aug 11 2013, 10:58 PM
Post#6



Posts: 542
Joined: 19-October 11



Right Click on the Project Explorer window in the VB Editor and click Import File. Browse to the cls file.
Go to the top of the page
 
MadPiet
post Aug 11 2013, 11:00 PM
Post#7



Posts: 2,251
Joined: 27-February 09



check this tutorial by David Ireland out... granted it's really old, but I think he does a really good job of keeping it simple and showing how classes can be useful. The examples are pretty rudimentary, but that's intentional. Until you really get your head around classes and how they're different from records etc (what we're used to in Access), it's a conceptually difficult subject. Once you understand what they are and how they work, they're really cool. (We did a project using shapes in school, but used Java, which is a purely object-oriented language. it takes a little while to get your head around the paradigm shift, but once you do, it's really neat.)
Go to the top of the page
 
Kamulegeya
post Aug 12 2013, 02:36 AM
Post#8



Posts: 1,767
Joined: 5-September 10
From: Kampala,Uganda The Pearl of Africa


Let me add my two cents..
It took me some time to grasp the idea of classes...but i think now i am getting there....
Now after reading this thread, i have decided to make a class module that automates excel, and send a recordset to a worksheet.
In the Class initialise event, an instance of excel is created and workbook added.
It has addworksheet method that adds a worksheet accepting the worksheet number as an argument...remember passing a none existing number results into an error...
It has sendData method which accepts an SQL string as an argument....this does the usual stuff of sending data to excel range and add column heads..
The terminate event sets the object variables to nothing.......perhaps this example can help......
class module code
CODE
Option Compare Database
Option Explicit

Private objExcel As Excel.Application
Private objBook As Excel.Workbook
Private objSheet As Excel.Worksheet
Private Sub Class_Initialize()
' initialse the excel object and set it visible
Set objExcel = New Excel.Application
objExcel.Visible = True
Set objBook = objExcel.Workbooks.Add
End Sub
Public Sub AddExcelSheet(i As Integer)
Set objSheet = objBook.Worksheets(i)

End Sub
Public Sub sendData(stringSQL As String)
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim i As Integer
Set db = CurrentDb()
Set rs = db.OpenRecordset(stringSQL, dbOpenSnapshot)
If Not rs.EOF Then
objSheet.Range("A1").CopyFromRecordset rs
For i = 1 To rs.Fields.Count
objSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next i
End If

rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
Private Sub Class_Terminate()
Set objBook = Nothing
Set objSheet = Nothing
If Not objExcel Is Nothing Then
Set objExcel = Nothing
End If
End Sub

this how i have called it in a command button
CODE
Private Sub Command33_Click()
Dim strSQL As String
''declare a variable pointing to the class
Dim excExcel As clsExcel
strSQL = "Select * from qryPReceipts Where TenantID=" & Me.TenantID
''create an instance of the class
Set excExcel = New clsExcel
'' call add sheet method
excExcel.AddExcelSheet 1
'''send the data
excExcel.sendData strSQL
'''clean
Set excExcel = Nothing
End Sub
Go to the top of the page
 
Galaxiom
post Aug 12 2013, 06:21 PM
Post#9



Posts: 542
Joined: 19-October 11



Keep thinking. There is actually nothing in your class that really needs to be in a class. Functions in a Standard Module could perform identically.
Go to the top of the page
 
CyberCow
post Aug 12 2013, 08:46 PM
Post#10


UdderAccess Admin + UA Ruler
Posts: 19,555
Joined: 27-April 02
From: Upper MI


Sounds like using such a class in THIS CODE ARCHIVE DEMO could be pretty slick.
Go to the top of the page
 
tina t
post Aug 12 2013, 08:56 PM
Post#11



Posts: 5,194
Joined: 11-November 10
From: SoCal, USA


ah...! thank you! :) tina
Go to the top of the page
 
Kamulegeya
post Aug 13 2013, 02:09 AM
Post#12



Posts: 1,767
Joined: 5-September 10
From: Kampala,Uganda The Pearl of Africa


Hello Galaxiom
That i did technically is not a class because it can be done using functions in a module?
Which rules do use to detemine what needs to be done in a module or class?
I thought it is a matter of choice?...
Ronald
Go to the top of the page
 
Galaxiom
post Aug 13 2013, 02:43 AM
Post#13



Posts: 542
Joined: 19-October 11



You have implemented the functions as a class but that doesn't really use any of the functionality that is available in classes over simply having the functions held in a standard module.
lasses really come into their own when they are used as a basis for functionality that is adapated for different instances of the class objects derived from them.
It could be somewhat overkill but to stay with your Excel example you might add a Properties that designated the name of the target workbook and the recordset. The code in the class would determine if the workbook was already open and process the request depending on the results. All that complexity is hidden away inside while the developer using the class simply needs to create an instance and pass the workbook name and the recordset to it. The recordset would persist inside the class object instance.
Then you might have some methods that controlled the interaction between the workbook and recordset.
You could open multiple instances of the class each customised to deal with a particular workbook and associated recordset. The all work the same but manage different objects.
Another way to think about it is the way Access uses the Form class. Each form is based on exactly the same code but you pass properties such as the RecordSource and add Controls to its Controls Collection. Each instance of the class works the same way but is customised by changing the properties.
I hope that makes sense.
Go to the top of the page
 
BananaRepublic
post Aug 13 2013, 07:27 AM
Post#14


Dungeon Cleaner
Posts: 1,504
Joined: 16-June 07
From: Banana Republic


Let's see if this help to start with a bit more blatant example.
Pretend that we have a function named CreateWorkbook() and in the same module, a public variable named WorkbookPath.
Every time you want to create a CreateWorkbook(), you have to first set WorkbookPath then call that procedure. If you forget to set WorkbookPath, then it get created in wrong path, potentially overwriting some other Excel workbook there. The more time you call CreateWorkbook from different places, the more likely this may happen by accident.
Now you're thinking, "but I don't use public variables. Everyone knows they are bad. I'd use WorkbookPath as a parameter to CreateWorkbook." Exactly.
So, we have a function "CreateWorkbook(WorkbookPath As String)" Now you are assured that all time you're calling this function, you are going to be passed in the expected path without risking forgetting to set the variable first.
This is still a function in standard module, though. Now suppose we have another function "ApplyTemplate(WorkbookPath As String)". Sometimes you have the workbook opened already via some other process, and you only need to call ApplyTemplate(). Sometimes you are creating or opening the workbook so you only call the CreateWorkbook(). Other times you need both, so you sequentially call CreateWorkbook() then ApplyTemplate(), passing in the path twice. Like the public variable + function, this works but it is now again relying on you to remember the correct order; what if the workbook doesn't exist when you think it should? Should ApplyTemplate() do all error checking in regards to opening the workbook? The shortcut might be to create a private function "ValidateWorkbook()" that both CreateWorkbook() and ApplyTemplate() should call to handle errors with opening the workbook. But when you add a 3rd function, you must now remember to also call the ValidateWorkbook() because you are never sure that this CreateWorkbook() and ApplyTemplate() won't be already called for this same path.
Creating a class instance means you can simply assign a path and all other functions such as CreateWorkbook() and ValidateWorkbook() no longer need parameters to be passed and can simply operate upon the private variables set by the class when a Path is assigned and the only thing you have to do is to set Path first before calling any other functions, which is much more simpler & maintainable than when they are bunch of separate functions that may or may not operate upon same file.
HTH.
Go to the top of the page
 
Kamulegeya
post Aug 13 2013, 09:43 AM
Post#15



Posts: 1,767
Joined: 5-September 10
From: Kampala,Uganda The Pearl of Africa


Having read BR's and Galaxiom reply, i think my class example was rather trivial and a skelton which can be further developed...
But still not a bad try and example for a biginner!
Ronald
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    11th December 2017 - 02:51 AM