?
Solved

How to release object when I "Call getQuerydefFunction"

Posted on 2010-03-26
6
Medium Priority
?
271 Views
Last Modified: 2012-05-09
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")
0
Comment
Question by:rberke
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
ioane earned 600 total points
ID: 28718760
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
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 400 total points
ID: 28721526
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
 
LVL 5

Author Comment

by:rberke
ID: 28723788
"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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 5

Author Comment

by:rberke
ID: 29008421
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
 
LVL 75
ID: 29008527
ok ... thx
0
 
LVL 13

Expert Comment

by:ioane
ID: 29022833
Cheers.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question