473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing Variables.

A rather elementary question, In VB5, how can I pass a
variable from one form to another?
Sep 13 '06 #1
7 8173
The Doctor wrote:
A rather elementary question, In VB5, how can I pass a
variable from one form to another?
You have to have a public variable, property, or method (with a
parameter) in the form you want to pass the details to.

A variable compiles the same as a property but a property allows you to
add conditions and perform actions when it is set.
A method is normally used when you want to perform an action like
showing the form modally.

--
Dean Earley (de*********@icode.co.uk)
i-Catcher Development Team

iCode Systems
Sep 13 '06 #2

"The Doctor" <x2**@optusnet.com.auwrote in message
news:45***********************@news.optusnet.com.a u...
A rather elementary question, In VB5, how can I pass a
variable from one form to another?
To amplify on Mr. Earley's response.

There are several other mechanisms you might employ to "pass variables".
If you need a form to have some unique runtime information when loaded you
could create a "Init" method in the other form then call it before showing
the form.
Dim frmTwo As FSecondForm
Set frmTwo = New FSecondForm
frmTwo.Init Var1, Var2, Var3
frmTwo.Show
...
You can possilbly carry this method a bit farther and create a 'Constructor'
with the 'Init' actually calling itself to open.

Another method is to actually pass a Reference of the first form to the
second and let the second form query it. (Usually a moderately bad idea as
sooner or later you will end up with circular references if you aren't
careful. <g>)

-ralph
Sep 13 '06 #3
"Ralph" <nt*************@yahoo.comwrote in message
news:pO******************************@arkansas.net ...
>
"The Doctor" <x2**@optusnet.com.auwrote in message
news:45***********************@news.optusnet.com.a u...
>A rather elementary question, In VB5, how can I pass a
variable from one form to another?

To amplify on Mr. Earley's response.

There are several other mechanisms you might employ to
"pass variables".
If you need a form to have some unique runtime information
when loaded you
could create a "Init" method in the other form then call
it before showing
the form.
Dim frmTwo As FSecondForm
Set frmTwo = New FSecondForm
frmTwo.Init Var1, Var2, Var3
frmTwo.Show
...
You can possilbly carry this method a bit farther and
create a 'Constructor'
with the 'Init' actually calling itself to open.

Another method is to actually pass a Reference of the
first form to the
second and let the second form query it. (Usually a
moderately bad idea as
sooner or later you will end up with circular references
if you aren't
careful. <g>)

-ralph
tya, I appreciate your responses. Although I understand the
replies, I'm struggling with the code to achieve it & I'm
hoping that someone can be a bit more specific.

I can't publish details of the application as it's semi-cic
however I can liken it to a quiz. One question on each of 3
forms using option buttons. I need an answer from form 1
passed to form 2, both answers from forms 1 & 2 passed to
form 3.

My current approach is to write the answers to a temp file
which apart from passing the variables, it makes it easy to
reset the application.

Is there a better way of doing it? A response of more than
'yes' would be appreciated.
Sep 14 '06 #4

"The Doctor" <x2**@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
"Ralph" <nt*************@yahoo.comwrote in message
news:pO******************************@arkansas.net ...

<snipped>
tya, I appreciate your responses. Although I understand the
replies, I'm struggling with the code to achieve it & I'm
hoping that someone can be a bit more specific.

I can't publish details of the application as it's semi-cic
however I can liken it to a quiz. One question on each of 3
forms using option buttons. I need an answer from form 1
passed to form 2, both answers from forms 1 & 2 passed to
form 3.

My current approach is to write the answers to a temp file
which apart from passing the variables, it makes it easy to
reset the application.

Is there a better way of doing it? A response of more than
'yes' would be appreciated.
Provide a little more information about "One question on each of 3 forms
using option buttons."
Something like this...
'' Form1
"I like walks on the beach?"
* Yes
* No
* N/A
'' Form2
Answer to Question One: [ <answer]
"I like to read?"
* Yes
* No
* N/A
'' Form3
Answer to Question One: [ <answer]
Answer to Question Two: [ <answer]
"I like bald women?"
* Yes
* No
* N/A

-ralph
Sep 14 '06 #5
"Ralph" <nt*************@yahoo.comwrote in message
news:Zp******************************@arkansas.net ...
>
"The Doctor" <x2**@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
>"Ralph" <nt*************@yahoo.comwrote in message
news:pO******************************@arkansas.ne t...
>
<snipped>
tya, I appreciate your responses. Although I understand
the
replies, I'm struggling with the code to achieve it & I'm
hoping that someone can be a bit more specific.

I can't publish details of the application as it's
semi-cic
however I can liken it to a quiz. One question on each of
3
forms using option buttons. I need an answer from form 1
passed to form 2, both answers from forms 1 & 2 passed to
form 3.

My current approach is to write the answers to a temp
file
which apart from passing the variables, it makes it easy
to
reset the application.

Is there a better way of doing it? A response of more
than
'yes' would be appreciated.

Provide a little more information about "One question on
each of 3 forms
using option buttons."
Something like this...
'' Form1
"I like walks on the beach?"
* Yes
* No
* N/A
'' Form2
Answer to Question One: [ <answer]
"I like to read?"
* Yes
* No
* N/A
'' Form3
Answer to Question One: [ <answer]
Answer to Question Two: [ <answer]
"I like bald women?"
* Yes
* No
* N/A

-ralph

3 Forms.

Form1
label1.caption = "What colour are your eyes?"
(Control Array of 4 option buttons.)
option1(0).caption = "Green"
option1(1).caption = "Blue"
option1(2).caption = "Red"
option1(3).caption = "Purple"
Rem answer option1(3) = "Purple"

Form2
label1.caption = "What is your shoe size?"
(Control Array of 4 option buttons)
option1(0).caption = "Size 12"
option1(1).caption = "Size 14"
option1(2).caption = "Size 16"
option1(3).caption = "Size 18"
Rem answer option1(2) = "Size 12"

Form3
label1.caption = "Purple"
label2.caption = "Size 12"

Sep 14 '06 #6

"The Doctor" <x2**@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...
I can't publish details of the application as it's semi-cic however I can
liken it to a quiz. One question on each of 3 forms using option buttons. I
need an answer from form 1 passed to form 2, both answers from forms 1 & 2
passed to form 3.
Add a module to your program. Put these three lines in it:
Public Answer1 As String
Public Answer2 As String
Public Answer3 As String
These are now available to all your forms.

Since your forms look so much alike, you might also consider using just one
form, and changing the captions for each question.
Sep 14 '06 #7
Here is a slightly more complex method using a collection. Which would be
more adaptable to changes. Provided more for illustration than as a solution
for something as simple as described.

[Warning!
Most error handling has been removed.
Beware of line wraps.
The original code has been tested but not this edited version.
]

'Three forms as you outlined, but I added a button could have just used
_Close
' modMain.bas
' This uses passing the CStore to each Form before show
' You could just as easily let CStore remain a global, but this shows how
you
' can have multiple stores and not have to rework the forms.
Option Explicit
Private clsStore As CStore
Public Sub main()
Set clsStore = New CStore
Dim ff1 As Form1: Set ff1 = New Form1
ff1.Init clsStore : ff1.Show vbModal
Dim ff2 As Form2: Set ff2 = New Form2
ff2.Init clsStore : ff2.Show vbModal
Dim ff3 As Form3: Set ff3 = New Form3
ff3.Init clsStore : ff3.Show vbModal
End Sub
' end modMain
''
' Form1.frm
Option Explicit
Private mCStore As CStore
' hard-coded for each question/form
Private mMyQuestion As Long
Private mOption As Long
Public Sub Init(cls As CStore)
Set mCStore = cls
Debug.Assert Not mCStore Is Nothing
End Sub
Private Sub Form_Load()
mMyQuestion = 1 ' might be const
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set mCStore = Nothing
End Sub
Private Sub Option1_Click(Index As Integer)
mOption = Index
End Sub
Private Sub cmdDone_Click()
mCStore.QuestionAdd mMyQuestion, Label1.Caption,
Option1(mOption).Caption, mOption
Unload Me
End Sub
' end Form1
''
' Form2.frm
Option Explicit
Private mCStore As CStore
Private mMyQuestion As Long
Private mOption As Long
Public Sub Init(cls As CStore)
Set mCStore = cls
Debug.Assert Not mCStore Is Nothing
End Sub
Private Sub Form_Load()
mMyQuestion = 2
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set mCStore = Nothing
End Sub
Private Sub Option1_Click(Index As Integer)
mOption = Index
End Sub
Private Sub cmdDone_Click()
mCStore.QuestionAdd mMyQuestion, Label1.Caption,
Option1(mOption).Caption, mOption
Unload Me
End Sub
' end Form2
''
' Form3
' Just one label
Option Explicit
Private mCStore As CStore
Public Sub Init(cls As CStore)
Set mCStore = cls
End Sub
Private Sub Form_Load()
Label1.Caption = mCStore.GetAllAnswers()
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set mCStore = Nothing
End Sub
' end Form3
''
' Class CQuestion
' collects the question, the answer, and the option
' the option is useful if you have to go back and have
' the form to show previous values.
Public QuestionNum As Long
Public QuestionStr As String
Public AnswerStr As String
Public AnswerOpt As Long
' end CQuestion
''
' Class colQuestions
Private mCol As Collection
Public Function Add(nQuestion As Long, QuestionStr As String, sAnswer As
String, lOption As Long) As CQuestion
'create a new object
Dim objNewMember As CQuestion
Set objNewMember = New CQuestion
'set the properties passed into the method
objNewMember.QuestionNum = nQuestion
objNewMember.QuestionStr = QuestionStr
objNewMember.AnswerStr = sAnswer
objNewMember.AnswerOpt = lOption
mCol.Add objNewMember, "Q" & CStr(nQuestion)
'return the object created
Set Add = objNewMember
Set objNewMember = Nothing
End Function
' Index is always the question number
Public Property Get Item(vntIndexKey As Long) As CQuestion
Set Item = mCol("Q" & vntIndexKey)
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Long)
mCol.Remove "Q" & vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
' end colQuestions
''
' Class CStore
Private mcolQuestions As colQuestions
Public Sub QuestionAdd(nQuestion As Long, sQuestion As String, sAnswer As
String, lOption As Long)
mcolQuestions.Add nQuestion, sQuestion, sAnswer, lOption
End Sub
Public Function GetAnswer(ByVal nQuestion As Long, sQuestion As String,
sAnswer As String, lOption As Long) As Boolean
Dim cls As CQuestion
Set cls = mcolQuestions.Item(nQuestion)
nQuestion = cls.QuestionNum
sQuestion = cls.QuestionStr
sAnswer = cls.AnswerStr
lOption = cls.AnswerOpt
End Function
Public Function GetAllAnswers() As String
' if order is not important then use For...Each
Dim sTmp As String
Dim lCnt As Long
Dim cls As CQuestion
For lCnt = 1 To mcolQuestions.Count()
Set cls = mcolQuestions.Item(lCnt)
sTmp = sTmp & cls.AnswerStr & vbCrLf
Set cls = Nothing ' redundant
Next lCnt
GetAllAnswers = sTmp
End Function
Private Sub Class_Initialize()
Set mcolQuestions = New colQuestions
End Sub
Private Sub Class_Terminate()
Set mcolQuestions = Nothing
End Sub

-ralph
Sep 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
3
by: Lee | last post by:
Hi All How can I pass options from one webpage into another webpage. When the user clicks on the hyperlink I want them to be go to the next page but I need to pass in a number that the next...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
6
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.