We help IT Professionals succeed at work.

in vba access 2010 how can i get a string from a veriable in a  public sub in the same form

Medium Priority
709 Views
Last Modified: 2012-05-11
hi

i'm not succeed to get the contect  from a veriable in the same form from another public sub and when i make msgbox and the veriable name i get a blank msgbox and its seems that the veriable is already empty
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Variables declared within a procedure are in scope only within that procedure.  If they are declared within a module's header, their scope defaults to Private, and are available only to procedures within that module.  If they are  declared within a module's header as Public then they are available globally.

It is good practice to prefix the name of a global variable with "g_" so that it's obvious that is is global.  It's also good practice to avoid global variables whenever possible, as they can be cause problems and be hard to debug.

I hope this helps.
CERTIFIED EXPERT

Commented:
i.e.
'Module Header
'Defaults to Private so available to the procedures within this module
Dim m_strString as String

Public Sub SetTheString(NewValue As String)
    'A way to set the value
    m_strString = NewValue

End Sub

Public Sub GetTheString()
   'A way to display the value
    MsgBox m_strString

End Sub

Open in new window


Does that give you something to work with?

Author

Commented:
thanks alot for your answer i try your code but i don't succeed here is a copy from my code:
Option Compare Database

Dim Gsqlstatement As String

Public Sub GetTheSqlStatement(getValue As String)
getValue = " Test "
Gsqlstatement = getValue
End Sub
_____________
and this what i wrote in my form to get a message box this content from getvalue veriable:

Public Sub Command28_Click()
MsgBox Gsqlstatement
End Sub
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks for your both you give my excellent and very clear  examples that learn me a lot
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.