Solved

How to share declaration among the forms in one project

Posted on 2003-11-15
9
259 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

912 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

18 Experts available now in Live!

Get 1:1 Help Now