PDA

View Full Version : Solved: understanding other people's code



philfer
01-05-2010, 07:53 AM
Hello,

I am working on an Access DB that had been built and automated by someone who has now left. In his code he had done :-

Dim RS
Set RS = CurrentDb.OpenRecordset ("SELECT ........

Is this just an example of sloppy programming or is there a distinct advantage to doing :-

Dim DB As DAO.Database
Dim RS As DAO.Recordset

Set DB = CurrentDb
Set RS = OpenRecordset ("SELECT .........

not just in terms of the readability of code and good programming practice but in terms of performance etc

Any thoughts would be appreciated

Cheers
Phil

orange
01-05-2010, 08:36 AM
Hello,

I am working on an Access DB that had been built and automated by someone who has now left. In his code he had done :-

Dim RS
Set RS = CurrentDb.OpenRecordset ("SELECT ........

Is this just an example of sloppy programming or is there a distinct advantage to doing :-

Dim DB As DAO.Database
Dim RS As DAO.Recordset

Set DB = CurrentDb
Set RS = OpenRecordset ("SELECT .........

not just in terms of the readability of code and good programming practice but in terms of performance etc

Any thoughts would be appreciated

Cheers
Phil

My preference is to explicitly Dim the variables as specific object types.
I also use DAO a lot (I think it's more an age thing- that's what I learned)

If you Dim variables such as

Dim x
Dim y they are dimmed as Variants. They will be resolved when used.

Similarly if you

Dim X , Y , Z as Integer
X and Y will be dimmed as variants , Z will be Integer


Set RS = OpenRecordset ("SELECT .........
will be an ADODB recordset by default. If you move the DAO Library higher up in the List of references, then it will be DAO. However, if you Dim the RS as DAO.Recordset explicitly, you know exactly what it is.

I don't think it's an issue of speed/performance. I think it is a clearer coding practice to explicitly DIM variable and object types.

That's my $.02.

Movian
01-07-2010, 08:39 AM
I personally tend to do a mixture in between

Dim Myrs as Dao.Recordset
Set myrs = Currentdb().openRecordSet("BlaH")

myrs.close
Set myrs = nothing

I like to Dim all my variables (and infact force it with Option Explicit)
However i don't see the point of dimensioning a database variable, Setting it only to have to close it out again.

Dim Myrs as Dao.Recordset
Dim MyDB as DAO.Database
Set MyDB = CurrentDB
Set myrs = MyDB.openRecordSet("BlaH")

myrs.close
Set myrs = nothing
mydb.close
set mydb = nothing

I can see the use of it if dealing with Multiple databases or external DB's however if dealing with only your own DB i think currentDB().openRecordSet should work fine (And in theory be more efficient as you arn't dimensioning an extra variable, although im sure the benefit of this on its own is so minimal its hardly worth it)

Just through I would throw in my two cents