Results 1 to 23 of 23

Thread: call variable from different form

  1. #1

    Thread Starter
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120

    call variable from different form

    i got a multiple form in my project
    i have to call the variable from the different form

    the variable is not public declared

    can i do it

    all the from are loaded
    with Regards
    sameer Mulgaonkar

  2. #2
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    You could have the variable as a public property of your form.

    example:
    VB Code:
    1. 'Private variable in form1
    2. Private m_myVar as string
    3.  
    4. 'Public property Read/Write
    5. Public Property Get myVar() As String
    6.   myVar = m_myVar
    7. End Property
    8.  
    9. Public Property Let myVar(ByVal RHS As String)
    10.   m_myVar = RHS
    11. End Property
    12.  
    13. 'You can then call from anywhere like
    14. Form1.myVar = "Test"
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Or add a module, and make the variable public there.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4

    Thread Starter
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120
    i dont want to declare the variable public of a reson
    i just want to know can we do it this way
    with Regards
    sameer Mulgaonkar

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Okay, if you dont want to declare it Public, then declare it Global
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    The variable will not be publicly declared if you do it as a property of the form.

    It will only be accessible through the form in which you place it.
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  7. #7

    Thread Starter
    Lively Member sameer spitfire's Avatar
    Join Date
    Nov 2001
    Location
    India
    Posts
    120
    no public no global just priavte for the form
    and the form is loaded

    can i call them from different form
    with Regards
    sameer Mulgaonkar

  8. #8
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    the only other alternative is create a method on the form that alters the variable.

    It has to be public somewhere if you want to change it from anywhere other than the form

    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  9. #9
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Wow strict rules!!

    You could also create a public (scared to use that word ) function in the form and call it from the other form like this...

    VB Code:
    1. 'Form1------------------------
    2. Private Sub Command1_Click()
    3.     Form2.Show
    4.     Debug.Print Form2.PassVar
    5. End Sub
    6.  
    7. 'Form2------------------------------
    8. Option Explicit
    9.  
    10. Dim MyPrivVar As Integer
    11.  
    12. Private Sub Form_Load()
    13.     MyPrivVar = 2
    14. End Sub
    15.  
    16. Function PassVar() As Integer
    17.     PassVar = MyPrivVar
    18. End Function
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think people just have a thing against public variables and functions ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Yeah
    but i'd much rather use a module with public variable than a form level variable or function. Cos then u have to cater for the form load events that can fire when u didnt want them to.
    Bah!
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  12. #12
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243


    Strange. I actually prefer the method of accessing them from properties on the form.

    Each to their own I suppose
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  13. #13
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I like modules because they're totally independant.
    Though the only downside is that when you use a variable or function from a module, the entire module is then loaded into memory
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  14. #14
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    can you not store your info in the form.tag property?

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Performance wise storing something in the .Tag property is like pulling a loaded trailer up a steep hill in a ten year old skoda.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    how come?

  17. #17
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    All properties in VB are far far far slower than a variable.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4. Private strTag As String
    5.  
    6. Private Sub Form_Load()
    7.     Dim time1 As Long, time2 As Long
    8.     Dim i As Long
    9.    
    10.     Tag = "I 0wnz"
    11.     strTag = "I 0wnz"
    12.    
    13.     time1 = GetTickCount
    14.     For i = 0 To 100000
    15.         If Tag = "I 0wnz" Then
    16.            
    17.         End If
    18.     Next
    19.     time1 = GetTickCount - time1
    20.    
    21.     time2 = GetTickCount
    22.     For i = 0 To 100000
    23.         If strTag = "I 0wnz" Then
    24.            
    25.         End If
    26.     Next
    27.     time2 = GetTickCount - time2
    28.    
    29.     MsgBox time1 & vbCrLf & time2
    30.    
    31. End Sub

    144 v 35 in favour of variables
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  18. #18
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    You wrote a function to prove that ehh

  19. #19
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Where Jamie works they always give him things like that to do so that the secretaries dont sue for constant sexual harrassment
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Nah I just like code optimization.
    Gotten into a real routine now. When I come up with a different way of doing something, I click on the little VB6 icon in my taskbar thingy, new project, then click the Show Desktop button on my taskbar, double click the shortcut to win32api. Hit CTRL-F and type in GetTickCount and hit find next twice.
    Copy the line. Alt-tab into VB. Right click on form and do View Code. Type Private and then hit CTRL-V

    Then double click on form to create the on load code, declare the 2 time variables and a counter. Then compare, msgbox, and write down the results
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  21. #21
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    ooo



    I wanna be like you jamie

    I will start comparing code with others'

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Ah shag off.
    Not my fault I ownz is it
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  23. #23
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    But do you....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width