Using Classes in a database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I'm not sure if this is the correct board to post this question so please
direct me to the right one if I'm not in the correct board.

I was going through my Access 2002 Desktop Developer's Handbook and I
noticed that Access has support for Object Oriented design. I created a
simple class (called BankAccount) that had one method and two properties.
However, I am having a hard time thinking of a use for classes in any of the
Access databases that I currently work on.

Has anyone used classes in a project that they worked on? How were they used?

Thanks.
 
Oh yes...

I've used them for

-- handling the insertion/editing/deletion of data from special tables
so that I don't have to duplicate the code in every form that needs to do
that (e.g., assigning unique numbers as primary keys, invoice numbers, etc.;
or for handling child data such as serial numbers for parts)

-- handling the encryption / decryption of passwords

-- handling the presentation of progress meters and other "widely used"
features in the database

-- handling the presentation of "process" messages to a user in a modal
form

-- and so on


Think of things that you do in the database where you must do the same task
over and over in various forms, and then think of how easy it would be to
just instantiate an object with all that code built in and just set
properties, run methods, etc. without having to redo data validation,
setting up of recordsets, etc.
 
That sounds great. I'm having a hard time conceptualizing it though. How
would I put the following procedure called UnlockForm into a class?

Const strFormName As String = "frmTroubleTickets"

Private Sub cmdEdit_Click()

UnlockForm (strFormName)

End Sub

Sub UnlockForm(strFormName As String)
On Error GoTo Err_unlockform
If Forms(strFormName).AllowEdits = False Then
Forms(strFormName).AllowEdits = True
MsgBox "Form is now unlocked for editing."
End If

Exit_unlockform:
Exit Sub

Err_unlockform:
MsgBox Err.Description
Resume Exit_unlockform

End Sub
 
Quite honestly, I would not use a class for this operation. Instead, I'd use
a public subroutine because the code is so simple and amenable to a simple
subroutine.
 
Well done, Albert.
I also use them for that sort of thing. One example I have is for a complex
project cost accounting system, I need to use base data from a table that
contains Accounting Period dates that are not consistent to a calendar and
some periods are 4 weeks where others are 5. I need to determine the period
begin and end dates, the number of working hours in each week of the period,
taking weekends and holidays into account , etc.

Much of this information will be used throughout various forms and reports.
Public functions could be used, but the code would have to be executed for
each record. By using a Class module, I can do all the calculations one time
in the Initialization event of the class and populate all the properties.
Then each record references the class properties to get the values needed.
This is faster because the database is only queried once to retrieve the data
and the calculations are performed only once.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top