reading a variable from a different form
I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
how can i do this?
thanks to all in advance.
Re: reading a variable from a different form
Quote:
Originally posted by Jorgin
I have a question.. i have 2 forms.. and the first form loads and the user has to click on a control which assigns a value to a variable.. then the second form loads and the first one hides. i need the program to read the value from the variable declared in the first form from the SECOND FORM to use in the SECOND FORM.
how can i do this?
thanks to all in advance.
you must declare it as public.. then you can simply access it by using
form1.variablename
Re: I respectfully disagree
Quote:
Originally posted by Briantcva
The original problem was how to pass a variable from Form1 to Form2. Form2 is the only variable consumer. Thus the variable only needs to exist within the scope of Form2. Were this variable needed for the whole project and multiple forms needed access and manipulation, then a global would be appropriate.
And yeah, I do realize we're arguing and vs. also. :D
I do agree with you, but want to add something. I try to code for the bigger picture. If two forms are going to need it, then the odds are that more will eventually and the code will have to be re-written, but I also keep in mind that that is not always the case. It will be up to the coder to make that decision..
:) Rudy :)
Passing array between forms
I'm doing something similar, only I'm using a dynamic array. I'm trying to write an app to add a list of users to a group. As
I've got it now, the array is declared and utilized only in Form2 and Form3 (it prints it's contents to a ListBox in 3).
However, I want Form1 to utilize the array once it's been populated, and unload Form2&3.
As I understand this, I need to move the declaration of UserArray() out of the subroutine on Form2 and make it a general
declaration as a public array on Form1? What happens to the array when Form2 gets unloaded? Will the array still be populated
with the data provided by Form2?
VB Code:
'This is Form1 as it stands
Private Sub AddMember_Click()
Dim Group As IADsGroup
Dim GroupName As String
Dim GroupDomain As String
Dim User As IADsUser
Dim UserName As String
Dim UserDomain As String
Dim Prompt As String
Prompt = "Please enter the target group."
GroupName = InputBox(Prompt)
GroupDomain = "TestDomain"
UserName = "target_user_name" 'This is gonna get changed to UserArray()
UserDomain = "LSNA"
'Obviously this next part will have to be changed to utilize an array.
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
Set Group = GetObject("WinNT://" & GroupDomain & "/" & GroupName & ",group")
Group.Add (User.ADsPath)
Group.SetInfo
End Sub
'This is the main subroutine of Form2 as it stands
Private Sub File1_Click()
Dim UserList
Dim UserArray() As String
Dim UserCounter As Integer
Dim X As Integer
Set Fso = CreateObject("Scripting.FileSystemObject")
SelectedFile = File1.Path & "\" & File1.FileName
Set UserList = Fso.OpenTextFile(SelectedFile, 1)
UserCounter = 0
ReDim UserArray(UserCounter + 1)
Do Until UserList.AtEndOfLine = True
ReDim Preserve UserArray(UBound(UserArray) + 1)
UserArray(UserCounter) = UserList.ReadLine
UserCounter = UserCounter + 1
MsgBox (UserCounter)
Loop
Load Form3
For X = 0 To (UserCounter - 1)
Form3.List1.AddItem UserArray(X)
Next X
Form3.Show 1
End Sub