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
Solved

How to share declaration among the forms in one project

Posted on 2003-11-15
9
261 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to parse out a json string in VB6? 10 224
Using an encrypted  value to decrypt and display contents in vb6 9 56
vbModal 12 57
VB6 - Scroll Mouse wheel on Picturebox 13 77
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 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