(Solved) – Writing and reading from a global variable in vba

(solved)-–-writing-and-reading-from-a-global-variable-in-vba

I am using excel vba and I have this code:

Public cn As ADODB.Connection 'variables for db communication used globally
Public rs As ADODB.Recordset
Global selecteId As Integer 'holds the id of the currently selected intern

Sub getIntern()
    Dim lastname As String
    Dim com As String
    Dim ws As Worksheet

    Set ws = Worksheets("Create and edit intern")
    lastname = ws.Range("C16").Value

    com = "select * from intern_db where nachname = '" & lastname & "'"
    dbConnect
    Set rs = cn.Execute(com)

    selectedId = rs.Fields("ID").Value
    dbClose
    debug.print(selectedId)
 End sub

Sub updateIntern() 
   ...
    Debug.Print (selectedId)
   ...

End Sub

As you can see I want to write to the variable selectedId from the getIntern() method and read it with the updateIntern() method. However, in the updateIntern() method selectedId is blank.

Any ideas why that is? I have set selectedId in the correct scope haven’t I ?

Leave a Reply

Your email address will not be published. Required fields are marked *