简体   繁体   中英

How do I reference a variable in a different sub in ms-access VBA?

What I have is a separate Private Sub that gets a value and sets it as variable "Record" with type Double.

Now I need to be able to reference that value even though it is in another sub?

For example:

Private Sub GetValue()

Dim Record as Double

Record = (Code to find Record)

End Sub

How do I reference that "Record" in another Sub?

Thanks in advance, Bob P

I'm more a SQL Server and Excel guy, buy if I had to solve your problem, I'd do it in one of two ways. That can't really be the body of your function, is it - because the Record is set as local, and then.... disappears!

  1. Use a global variable. Just put the Dim Record as Double outside of any sub. This also means that it get set whenever you last call this Sub, assuming all other code only read from it.
  2. Use a Function instead. Example below

Private Function GetRecord() as Double
    Dim Record as Double
    Record = ....
    GetRecord = Record
End Function

Answer is you cant. Its private to that sub

What you need to do is either, have the

Dim Record as Double 

in the general code, or, turn it into a function and return it so you can do

Myrecord = GetValue()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM