473,695 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.OpenRecordse t("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!globalFor m!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 3506
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.ru tgers.eduwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.OpenRecordse t("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!globalFor m!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
"objDefault s" 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.ru tgers.eduwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.OpenRecordse t("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!globalFor m!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
3479
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 the following code to read files from a hard drive and stuff them in the database. <% '==================================================================================== 'Function to read a binary file from disk
4
1971
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 memo still cuts it short. TransferText cut it short too. Someone suggested that I use the following code. It works, but it populates only one row of the text. Missing something critical. Any ideas on how to get this to work? 'Beginning of...
2
2959
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 2000 chracters. Right now the code looks like this: Private Sub Command3_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim rslt As ADODB.Recordset, rslt2 As ADODB.Recordset Dim sqlstr As String, sqlstr2 As String Dim...
7
4432
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 declarations section of a module: Public rsAlwaysOpen As DAO.Recordset Then, when the switchboard form loads I set the variable:
10
12293
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 database table using INSERT.... (field1) ...VALUES ......... (N'Characters'). How do I do this using Rs.Update viz-a-viz:
23
52705
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? Thanks, -paul w
5
5886
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 function returns a long string. This works. 2. A query has a calculated field based on the custom function above. This works when the query is run directly.
6
2673
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 they've been overcharged for shipments by truck and rail carriers. 99.9% of the time, one of our auditors needs to see all data sent by a customer in datasheet view so that they can sort records in many different ways looking for duplication...
4
2353
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? Dim stDocName As String Dim nRetreatNumber As Integer Dim nRNOS As Integer 'Dim nCNOS As Integer Dim nCNOS As Long
0
8586
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3025
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.