2

Conceptually, which is better regarding memory leak and best practice:

Do
 set mTable = "something"
 'do a bunch of stuff
 mTable.Close
 Set mTable = nothing
Loop

Or:

Do
 set mTable = "something"
 'do a bunch of stuff
Loop
mTable.Close
Set mTable = nothing

Is one faster that the other?

1 Answers1

2

You forgot the choice of my preference:

Do
     set mTable = "something"
     'do a bunch of stuff
     mTable.Close
Loop
Set mTable = nothing

mTable.Close commits the transaction, and you might run into troubles with too many nested transactions when running a large number of add or edit actions on the recordset (especially when working with attachments and multi-valued fields, which should be avoided if possible).

Set mTable = Nothing clears the reference to a closed recordset, and there's not really a reason to use this inside a loop, because you're reassigning something else to it after a couple of milliseconds.

It all depends on what you're doing exactly. As @Gustav stated, the last one is mostly fine. However, when editing and inserting, I prefer my option.

Erik A
  • 31,639
  • 12
  • 42
  • 67
  • I will be editing the records in mTable so sounds like this may be the way to go. Thank you for the details. I'm self taught and have huge conceptual gaps and this helps me a lot. – user3112028 Aug 17 '17 at 14:38