Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to share declaration among the forms in one project

Posted on 2003-11-15
9
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Accepted Solution

by:
bingie earned 120 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 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

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 101

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
Suggested Courses

650 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