473,386 Members | 1,799 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,386 software developers and data experts.

Variable Loses Scope in Recordset

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.OpenRecordset("qryName", dbOpenDynaset)

'Comparison of value from user to recordset
rec.FindFirst "[EmployeeID] =" & Nz(Me!Combo17.Value, 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.Value = ""
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.Value = ""
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 1720
sashi
1,754 Expert 1GB
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 1GB
Do yo want to store this value for use later or do you wish to use it immediately?
Oct 11 '06 #3
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 1GB
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
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
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...
2
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...
0
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...
1
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...
23
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
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,...
1
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...
0
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...
112
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.