How to release object when I "Call getQuerydefFunction"

I try to release variables after I use them.

So, I coded something as follows  (this is greatly simplified)

Code #1
  sub ChangeQueryDef(qname as string, sql as string)
    dim getChangeQueryDef as querydef
    set getChangeQueryDef = currentdb.querydefs("myname")
    .... getChangeQueryDef = sqlname
    set getChangeQueryDef = nothing
  end sub

But, I want to change the subroutine to so that it can sometimes be called as a function.
 
Code #2
  sub getChangeQueryDef(qname as string, sql as string) as querydef
    set getChangeQueryDef = currentdb.querydefs("myname")
    .... getChangeQueryDef = sqlname
    set getChangeQueryDef = nothing
  end sub

So, here is how I could use the routine and be sure the querydef variable gets de-allocated.

Code #3
   set qdf =  getChangeQueryDef("qGeneric", "select * from table")

   ... do stuff with qdf

   set qdf = nothing


But, what happense if I simnply call the routine?  My theory is the the variable does not need to be de-allocated.
But, I know that Access and Excel both sometimes create hidden variables that don't get deallocated until the application is closed.

I wonder if the following code might have that problem

Code #4
    call  getChangeQueryDef("qGeneric", "select * from table")
LVL 5
rberkeConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ioanePlanning & Analytics ManagerCommented:
I think you're over complicating the issue.

Variables are localised to the function they are in. Once the function ends, the variables are automatically removed.

Actually setting an object variable to nothing before ending a function is really just for peace of mind and habit than necessity.

Your code#2 has issues, i.e. defined as sub but trying to return a value.

As long as you correct code#2, code#3 will work fine. No problem with variables.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
How about this - using a Function, which will return a value - querydef in this case


Public Function getChangeQueryDef(qname as string, sql as string) as QueryDef
    set getChangeQueryDef = currentdb.querydefs(qname )

End Function

"But, I know that Access and Excel both sometimes create hidden variables that don't get deallocated until the application is closed."

Well, if Access is doing this, it's nothing you have to worry about.

mx
0
rberkeConsultantAuthor Commented:
"Once the function ends, the variables are automatically removed."

Yes, that is true WHEN YOU DEFINE A VARIABLE.

But, in the attached code, sub Test1  does not define a variable.  
I have discovered in Excel sometimes generates its own internal variables that don't get deallocated.
For instance, I once discovered that vba creating new instances of Excel were still showing in task manager even after I closed my Excel program.  I traced it to a code that said "debug.print cells.address". Excel noticed that I did not supply an Application object.  Excel generated a module level variable, and used it.  When my subroutine exited, that variable was not deallocated causing the excle instance to hang.

I got to run now, but I will give more details tomorrow.

sub test1()
call  getChangeQueryDef("qGeneric", "select * from table")
end sub

Public Function getChangeQueryDef(qname as string, sql as string) as QueryDef
    set getChangeQueryDef = currentdb.querydefs(qname )

End Function 

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

rberkeConsultantAuthor Commented:
I am afraid I did not state this question clearly, and I will not have time to clean it up for at least a week, maybe more.

So, I am awarding points mainly to reward effort from the experts that commented.

I will eventually resubmit a clarified question. At that time I will crosslink a comment here.

0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
ok ... thx
0
ioanePlanning & Analytics ManagerCommented:
Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.