Release memory (set object = nothing), but where?

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I've got the code below and i want to release memory from objCommand (set objcommand = nothing), but where can I do this? If I do it inside the "If .. Then .. End If" then it wont be executed if the if-sentence is false and I can't do it after the if-sentence because I have to make a new command inside the if-sentence. I know it's not a big problem, but I want it to be made correctly.

Code:
Set objCommand = New ADODB.Command

With objCommand
    .ActiveConnection = objConnection
    .Execute
End With

If objCommand.Parameters(0).Value = "T" Then
End If
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

MisterBates

Board Regular
Joined
Oct 17, 2003
Messages
174
Releasing object variables would normally be done on exit from a sub/function. If you have mutiple "exit sub" or "exit function" in your code, add a label at near the end of the function, put the "set object = nothing" statements after the label, then change each "exit sub"/"exit function" to "goto label" instead.
 

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I thought I should release objects when I know that I'm done using the object in the way it's 'setted'. I will use many commands in my procedure. Shouldn't I release the object before i set a new object with the 'same name'?
 

MisterBates

Board Regular
Joined
Oct 17, 2003
Messages
174
I've only ever used set object = nothing part-way through a procedure if I either want to:
1) check if the subsequent statement worked (set to nothing first means I can check "if object is nothing" directly after the subsequent statement
2) release an object reference early (in the case of shared or locked resources)

In your case, if what you're doing will lock a table or records in the table, then include set objCommand = nothing in your code as soon as you're able to release those records.

I did a quick google, and the following article (while not directly applicable) may be of interest when-are-you-required-to-set-objects-to-nothing.aspx
 

Watch MrExcel Video

Forum statistics

Threads
1,127,633
Messages
5,626,009
Members
416,152
Latest member
TedX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top