Solved

How to share declaration among the forms in one project

Posted on 2003-11-15
9
260 Views
Last Modified: 2011-08-18
i have many forms in my project. so in each form, i use the same file to operate.

Right now, i kinda use declaration or assign the file (let say log.txt) to a variale ; e.g TextFile = "c:\TextFile". becoz each form use it, so i just do like, TextFile1 = "c:\Log.txt" for Form1, TextFile2 = "c:\Log.txt" for Form2, etc.. it work fine but if i want to change the file name, i need to make changes in each form.

So if you guys can help me to make a declaration, e.g TextFile = "c:\Log.txt" and it can share among the the forms..
meaning that, eah form just can refer to that and if i want to change the file name (say LogEdit.txt), i just change the declaration/variable and it will effects all the forms.

I think this is very easy for u guys..
0
Comment
Question by:michelle_branch
9 Comments
 
LVL 11

Accepted Solution

by:
bingie earned 30 total points
ID: 9757538
Add a module to your project and declare the variables as follows in the module:

Public textfile1 = "c:\log.txt"

this can be used for any forms.



0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9757543
You have two basic options:

1) Declare TextFile Public on Form1.

Public TextFile As String

Private Sub Form_Load()
    TextFile = "c:\Log.txt"
End Sub

Then from any form, you can use this syntax to access the variable:

Form1.TextFile

2) Declare TextFile on a Module

Dim TextFile As String

Then you can use the variable name directly from anywhere as if you had declared it locally already.

Idle_Mind
0
 

Author Comment

by:michelle_branch
ID: 9757841
still cannot. it cannot call the variable from other Form.

i just do this in Form1
---------------------------
Public A As String

Private Sub Form_Load()
    A = "OK!"
End Sub


then in Form2
----------------
Private Sub Form_load()
Dim B As String
     B = Form1.A
     MsgBox B
End Sub


but the msgbox don't give anything. just blank. Is my code right?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9757953
Are you telling Form2 to display itself?  Works great on my system.

Idle_Mind

' Form1
Public A As String

Private Sub Form_Load()
    A = "OK!"
    Form2.Show   '  <<<------------------------------------------
End Sub

' Form2
Private Sub Form_Load()
    Dim B As String
    B = Form1.A
    MsgBox B
End Sub
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9758846
Create a module and put the common or global variables in it.
Create a MAIN() subroutine in the module and have it initialize all common variables then set MAIN as the startup routine.

To do it your way
Form1 must be loaded to get the values of the public variables.  You can access the values but that doesn't load the form.
Or modify your code to

' Form2
Private Sub Form_Load()
    Dim B As String

    LOAD FORM1

    B = Form1.A
    MsgBox B
End Sub

mlmcc
0
 

Expert Comment

by:biao81
ID: 9761127
Yup, i think using global variable is a better way comparing to stored the value in form.
Create a Module, and simply create ur variable by writing
Public Name as String
Name="Adrian"

then in your form, you can simply get back the value by calling Name
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10548454
Moderator, my recommended disposition is:

    Accept bingie's comment(s) as an answer.

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question