Class question

fragso

New member
Local time
Today, 02:37
Joined
Nov 14, 2011
Messages
3
Ok, here's the thing.

I'm currently working on a small app that is filling in until a third-party application has been tailored, (come on, it's only been a year and they haven't agreed the spec yet).

Anyway, I've been using class modules and find this speeds up application development quite a bit.
I'm always striving to make my code more generic and as I noticed that I'm writing virtually the same code for each class to handle gets and sets to the DB. It goes something like this.

< module clsMyClass>

'Loads more fields than this ofc
Public RefNo as long
Public Forename as string
Public Surname as string

'Get method
'**********
Public function Get(varRef as long) as Boolean

dim MyRS as ADO.Recordset
dim varSQL as string

if not isnull(varRef) then
varSQL = "SELECT * FROM myTbl WHERE [refno] = " & varRef
set MyRS = CurrentDB.OpenRecordset(varSQL,dbReadOnly)
With MyRS
me.refno = !refno
me.Forename = !forename
me.Surname = !surname
.close
End With
endif

End Function


Of course there are other checks etc. in there too in the finished version.

What I was wondering was, (and I was playing about with different variations this afternoon), is there some way of passing the name of the received field at run time to the 'me.' part. Something like the following.


'Get method
Public function Get(varRef as long) as Boolean
dim MyRS as ADO.Recordset
dim varSQL as string
dim fld as Field
dim varFldName

if not isnull(varRef) then
varSQL = "SELECT * FROM myTbl WHERE [refno] = " & varRef
set MyRS = CurrentDB.OpenRecordset(varSQL,dbReadOnly)

for each fld in MyRS
varFldName = fld.name
me(varFldName) = fld.Value
next
MyRs.Close
endif

End Function


I've seen something similar done with form objects but haven't seen any examples of it done with user defined classes.
The code I needed for the project has been written out longhand so it's not urgent, but I think, (if it works), the above would be far more useful in the code toolbox as long as the naming of fields in the class reflect the field names in the table.

Any thoughts.
(P.S not tested the above code in Access for syntax errors, just done in notepad so apologies if there are any mistakes, its just to illustrate the idea)
 
What can I say, I was well tired. :)
 

Users who are viewing this thread

Back
Top Bottom