473,624 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Loses Scope in Recordset

5 New Member
I created a subprocedure that includes a recordset as follows:

Private Sub Combo17_Click()

'Declaring object and string variables
Dim DB As Database
Dim rec As Recordset
Dim sDealName As String

'Assign Object variable to current database
Set DB = CurrentDb

'Assign object variable to recordset
Set rec = DB.OpenRecordse t("qryName", dbOpenDynaset)

'Comparison of value from user to recordset
rec.FindFirst "[EmployeeID] =" & Nz(Me!Combo17.V alue, 0)

'If no match is found, user receives error message _
'exit routine

If rec.NoMatch Then
MsgBox "Check your entry", vbCritical, "No Matches"
Me!Combo17.Valu e = ""
Exit Sub
Else
' If record is found, value entered by user is passed to variable sFirstName
sDealName = Nz(rec.Fields(" Firstname"), "")

If sDealName = "" Then
MsgBox "Your sDealName does not exist"
Me!Combo17.Valu e = ""
Exit Sub
End If

End If

rec.Close

End Sub

The problem is that the variable sDealName loses scope when I close the recordset. I need to pass the value from this sDeal variable to another subprocedure.

Thank you.
Oct 10 '06 #1
5 1730
sashi
1,754 Recognized Expert Top Contributor
Hi there,

Declare the variable in a module file as public variable, kindly refer to below sample, good luck & take care.

Expand|Select|Wrap|Line Numbers
  1.   'Some module file
  2.   Public sVariableName as String
  3.  
Oct 11 '06 #2
willakawill
1,646 Top Contributor
Do yo want to store this value for use later or do you wish to use it immediately?
Oct 11 '06 #3
Newbie in ChiTown
5 New Member
Do yo want to store this value for use later or do you wish to use it immediately?
I want to store this value for use later.
Oct 12 '06 #4
willakawill
1,646 Top Contributor
I want to store this value for use later.
Then you can either use a global variable as per Saashi above or, if the form you are using is not going to be closed, put the following code at the top of the form code module:

Private variable_name As variable_type

This will remain in scope for as long as the form is open and will not suffer from the problems of using global variables which you can get lost with if you have several forms and modules.
Oct 12 '06 #5
Newbie in ChiTown
5 New Member
Then you can either use a global variable as per Saashi above or, if the form you are using is not going to be closed, put the following code at the top of the form code module:

Private variable_name As variable_type

This will remain in scope for as long as the form is open and will not suffer from the problems of using global variables which you can get lost with if you have several forms and modules.

Thank you. I used the global variable and it works!
Oct 13 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
4379
by: | last post by:
Hi, I want to do things this way: I have a bunch of stuff that I want to keep track of while a user is connected to the site. Maybe 50 little peices of information. So I know I can make 50 session variables , or an store an array in a session variable, but what I wanna do is store it all in a class so I can use property get and property let.
2
1670
by: yamafopa | last post by:
Just moving up from Access 97 and trying 2K. I am used to using the FindFirst method of the RecordsetClone object in order to do form navigation with a combo box. I notice that the wizard in 2K writes the following code in order to accomplish this... ' Find the record that matches the control. Dim rs As Object Set rs = Me.Recordset.Clone rs.Find " = " & Str(Me!)
0
1639
by: sneal | last post by:
A little background: We have an Access 2002 based user interface to our SQL Server 2000 database. The interface is a tabbed form with two of the tabs containing a subform. Data is pulled from the SQL Server using several stored procedures, one sproc for the main form and an additional sproc for each subform. All data is pulled from the SQL Server using ADO; the resulting client side (disconnected) ADODB.Recordset is then applied to each...
1
1185
by: Raheel Hussain | last post by:
hi, can any body tell me why the variable loses its value, i m actually declaring a variable which is public, and assigning a values in Function1 and reading the values in Function2 following is a sample code for wht i m doing. --------------------------------------- Public Class SupportRequest Inherits System.Web.UI.Page
23
19165
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
15
2157
by: tegryan | last post by:
Hey All, I'm trying to do something that should be very simple, but I think I've been up for too many hours or something. Basically, I have an asp application that uses a database connection, and I want to declare (create) that connection in an include and use it throughout the application in various functions. I've tried making the connection object a public variable, a constant, making the functions public, passing the connection...
1
25657
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35209
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
112
5415
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8672
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
8614
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...
0
8471
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
4075
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
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
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
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.