473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How long does recordset stay open?

I rarely deal with recordsets directly with code, since I usually use
Access queries, so be patient with this question. I want to open a
recordset with various default variables used by my program. I tried:

Public Sub OpenDefaults()
Dim db As dao.Database
Dim globalRst As dao.Recordset
Set db = CurrentDb()
Set globalRst = db.OpenRecordset("tblDefaults")
End Sub

I am able to pull variable I need when I'm IN this module. e.g.

boolUseFormA = globalRst!bFrmA

Whoever, I am not able to get the variables after executing the module.
I thought the recordset stayed open until I did: 'Set globalRst =
nothing'. I wanted to open the recordset when I started up my
application, then put variables from it as needed.

My questions are:
1) Am I doing something wrong? Should I be able to pull data from the
recordset until I reset it to nothing?
2) If this is impossible, which would be best for performance:
a) Opening a form with the variables and pulling them from
the form, e.g.
boolUseFormA = Forms!globalForm!bFrmA
b) Running the above code with the additional line to pull
the data I need and also additional codes to unset the database and
recordset objects?

Thanks!
Jim M

Aug 25 '06 #1
2 3491
Since you declared your variable inside the OpenDefaults procedure, it
ceases to exist as soon as the procedure exits. Its lifetime is just a few
microseconds in your example. The variable is also invisible to anything
else outside this procedure, i.e. its scope is just the procedure you
declared it in.

You can give it wider scope and a longer lifetime if you declare it in the
General Declarations section of your module (at the top, with the Option
statements.) If this is a standard module, its lifetime will last until you
close Access or reset the code. If it is in the module of a form, its
lifetime ceases when you close the form.

Doing that also changes the scope of the variable. In a standard module, it
will default to public scope, i.e. you can use it in any code in this
database. In a form's module, you can use the variable in any procedure in
that module.

Of course, that could have unintended consequences. If you use public
variables, you have to be really careful not to alter the value
inintentionally, and you don't know when you come to use it if some other
process may have altered it. It gets even more confusing if another
module/procedure uses the same name! VBA permits that, so which one is now
being accessed? Which value is being returned from the variable? And which
one is being changed? If you don't use Option Explicit, you now have a
*real* debugging nightmare on your hands!

At a practical level, code that relies on public variables is messy to
develop. You frequently reset during development, so they keep losing their
value, so you can't trust them to hold what you expect. At minimum, you
problaby need another public boolean variable just to hold the value of
whether they are initialized or not, and code to re-initialize them.

To answer your specific questions:
1. It is possible, but not advisable to use variables with a wide scope and
long lifetime.

2. Initializing a control on the form in Form_Open, and then reading the
value of the control when you need it would be faster than repeatedly
opening a recordset to get the same value over and over.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jim M" <ma*****@rci.rutgers.eduwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I rarely deal with recordsets directly with code, since I usually use
Access queries, so be patient with this question. I want to open a
recordset with various default variables used by my program. I tried:

Public Sub OpenDefaults()
Dim db As dao.Database
Dim globalRst As dao.Recordset
Set db = CurrentDb()
Set globalRst = db.OpenRecordset("tblDefaults")
End Sub

I am able to pull variable I need when I'm IN this module. e.g.

boolUseFormA = globalRst!bFrmA

Whoever, I am not able to get the variables after executing the module.
I thought the recordset stayed open until I did: 'Set globalRst =
nothing'. I wanted to open the recordset when I started up my
application, then put variables from it as needed.

My questions are:
1) Am I doing something wrong? Should I be able to pull data from the
recordset until I reset it to nothing?
2) If this is impossible, which would be best for performance:
a) Opening a form with the variables and pulling them from
the form, e.g.
boolUseFormA = Forms!globalForm!bFrmA
b) Running the above code with the additional line to pull
the data I need and also additional codes to unset the database and
recordset objects?

Thanks!
Jim M

Aug 25 '06 #2
Jim,
Allen pretty much said it. What I had to add was this: My take on
storing preferences is to keep them in a text file and load them from
the file. Also, these days I am leaning more toward class modules with
the properties & methods I need for the processes I am implementing
instead of doing things in the db engine. The db then becomes a bucket
that holds object properties instead of the container for the data.
So, in your case I'd probably create a class module that held the
preferences & defaults and create an instance of that as something like
"objDefaults" which could be referenced for any default values. Even
better IMHO is in the design of the objects for an app to include code
that embeds a default value in a property.

Allen Browne wrote:
Since you declared your variable inside the OpenDefaults procedure, it
ceases to exist as soon as the procedure exits. Its lifetime is just a few
microseconds in your example. The variable is also invisible to anything
else outside this procedure, i.e. its scope is just the procedure you
declared it in.

You can give it wider scope and a longer lifetime if you declare it in the
General Declarations section of your module (at the top, with the Option
statements.) If this is a standard module, its lifetime will last until you
close Access or reset the code. If it is in the module of a form, its
lifetime ceases when you close the form.

Doing that also changes the scope of the variable. In a standard module, it
will default to public scope, i.e. you can use it in any code in this
database. In a form's module, you can use the variable in any procedure in
that module.

Of course, that could have unintended consequences. If you use public
variables, you have to be really careful not to alter the value
inintentionally, and you don't know when you come to use it if some other
process may have altered it. It gets even more confusing if another
module/procedure uses the same name! VBA permits that, so which one is now
being accessed? Which value is being returned from the variable? And which
one is being changed? If you don't use Option Explicit, you now have a
*real* debugging nightmare on your hands!

At a practical level, code that relies on public variables is messy to
develop. You frequently reset during development, so they keep losing their
value, so you can't trust them to hold what you expect. At minimum, you
problaby need another public boolean variable just to hold the value of
whether they are initialized or not, and code to re-initialize them.

To answer your specific questions:
1. It is possible, but not advisable to use variables with a wide scope and
long lifetime.

2. Initializing a control on the form in Form_Open, and then reading the
value of the control when you need it would be faster than repeatedly
opening a recordset to get the same value over and over.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Jim M" <ma*****@rci.rutgers.eduwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
I rarely deal with recordsets directly with code, since I usually use
Access queries, so be patient with this question. I want to open a
recordset with various default variables used by my program. I tried:

Public Sub OpenDefaults()
Dim db As dao.Database
Dim globalRst As dao.Recordset
Set db = CurrentDb()
Set globalRst = db.OpenRecordset("tblDefaults")
End Sub

I am able to pull variable I need when I'm IN this module. e.g.

boolUseFormA = globalRst!bFrmA

Whoever, I am not able to get the variables after executing the module.
I thought the recordset stayed open until I did: 'Set globalRst =
nothing'. I wanted to open the recordset when I started up my
application, then put variables from it as needed.

My questions are:
1) Am I doing something wrong? Should I be able to pull data from the
recordset until I reset it to nothing?
2) If this is impossible, which would be best for performance:
a) Opening a form with the variables and pulling them from
the form, e.g.
boolUseFormA = Forms!globalForm!bFrmA
b) Running the above code with the additional line to pull
the data I need and also additional codes to unset the database and
recordset objects?

Thanks!
Jim M
Aug 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sean | last post by:
I'm new to using long raw fields with ASP. I know it would be easier to leave the files on the file system, but the client wants them in the database. Based on what I've read so far, I have used...
4
by: R Bolling | last post by:
I have a text file from a text document that has very long rows (two or three paragraphs from a text document for each row). Importing to text cuts it off at 255 characters, and importing to a...
2
by: Justin Koivisto | last post by:
I am attempting to execute a *long* query string via a ADODB.Recordset.Open (queryStr) call. Most of the time, the query string will be less than 100 characters, but in some cases, it may be up to...
7
by: Bill R via AccessMonster.com | last post by:
I get the error msg "Object variable or With block variable not set" when closing the "persistent" recordset I opened when the application opened. I have the rs variable declared in the...
10
by: Roger Withnell | last post by:
I'm using ASP, VBScript and SQL Server. I'm also using UTF-8 character set and so my codepage is 65001 and SQL Server datatype nvarchar. I can insert unicode characters correctly into the...
23
by: PW | last post by:
Hi, I'd like to close a recordset and set the database to nothing if a recordset is open if an error has occured. Leaving a recordset open and a database open isn't a good idea, right? ...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
6
by: jsacrey | last post by:
Hello everybody, I've got a bit of a situation that I could use some guidance with if possible. I work for an auditing firm where my users audit electronic shipping data for customers to see if...
4
by: eacollie | last post by:
Hi: I'm running into an overflow problem with the following code (with the variable nCNOS), but get a compile error (AddInvoice rsInvoice, nRNOS, nCNOS) if I change it to Long. Can someone help?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.