By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

Passing Variables.

P: n/a
A rather elementary question, In VB5, how can I pass a
variable from one form to another?
Sep 13 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a

"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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.