Variable Loses Scope in Recordset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie in ChiTown
    New Member
    • Oct 2006
    • 5

    #1

    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.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.
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    Hi there,

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

    Code:
      'Some module file
      Public sVariableName as String

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Do yo want to store this value for use later or do you wish to use it immediately?

      Comment

      • Newbie in ChiTown
        New Member
        • Oct 2006
        • 5

        #4
        Originally posted by willakawill
        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.

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by Newbie in ChiTown
          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.

          Comment

          • Newbie in ChiTown
            New Member
            • Oct 2006
            • 5

            #6
            Originally posted by willakawill
            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!

            Comment

            Working...