Solved

How to share declaration among the forms in one project

Posted on 2003-11-15
9
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Moderator, my recommended disposition is:

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

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now