Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
P***ing Variables.
Old
  (#1)
The Doctor
Guest
 
Posts: n/a
Default P***ing Variables. - 06-04-2007, 10:31 AM

A rather elementary question, In VB5, how can I p*** a
variable from one form to another?


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: P***ing Variables.
Old
  (#2)
Dean Earley
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM

The Doctor wrote:
> A rather elementary question, In VB5, how can I p*** 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 p*** 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 (EMAIL REMOVED)
i-Catcher Development Team

iCode Systems
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#3)
Ralph
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM


"The Doctor" <EMAIL REMOVED> wrote in message
news:450828e7$0$17211$EMAIL REMOVED u...
> A rather elementary question, In VB5, how can I p*** a
> variable from one form to another?
>


To amplify on Mr. Earley's response.

There are several other mechanisms you might employ to "p*** 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 p*** 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#4)
The Doctor
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM

"Ralph" <EMAIL REMOVED> wrote in message
newsEMAIL REMOVED ...
>
> "The Doctor" <EMAIL REMOVED> wrote in message
> news:450828e7$0$17211$EMAIL REMOVED u...
>> A rather elementary question, In VB5, how can I p*** a
>> variable from one form to another?
>>

>
> To amplify on Mr. Earley's response.
>
> There are several other mechanisms you might employ to
> "p*** 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 p*** 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
p***ed to form 2, both answers from forms 1 & 2 p***ed to
form 3.

My current approach is to write the answers to a temp file
which apart from p***ing 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.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#5)
Ralph
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM


"The Doctor" <EMAIL REMOVED> wrote in message
news:4508fdb7$0$5105$EMAIL REMOVED ...
> "Ralph" <EMAIL REMOVED> wrote in message
> newsEMAIL REMOVED ...
> >
>><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
> p***ed to form 2, both answers from forms 1 & 2 p***ed to
> form 3.
>
> My current approach is to write the answers to a temp file
> which apart from p***ing 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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#6)
The Doctor
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM

"Ralph" <EMAIL REMOVED> wrote in message
news:ZpSdnT-EMAIL REMOVED...
>
> "The Doctor" <EMAIL REMOVED> wrote in message
> news:4508fdb7$0$5105$EMAIL REMOVED ...
>> "Ralph" <EMAIL REMOVED> wrote in message
>> newsEMAIL REMOVED ...
>> >
>>><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
>> p***ed to form 2, both answers from forms 1 & 2 p***ed to
>> form 3.
>>
>> My current approach is to write the answers to a temp
>> file
>> which apart from p***ing 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"



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#7)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM


"The Doctor" <EMAIL REMOVED> wrote in message
news:4508fdb7$0$5105$EMAIL REMOVED ...

> 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 p***ed to form 2, both answers from forms 1 & 2
> p***ed 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.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: P***ing Variables.
Old
  (#8)
Ralph
Guest
 
Posts: n/a
Default Re: P***ing Variables. - 06-04-2007, 10:31 AM

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 p***ing 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.***ert 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.***ert 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
''
' Cl*** 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
''
' Cl*** 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 p***ed 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 Cl***_Initialize()
Set mCol = New Collection
End Sub
Private Sub Cl***_Terminate()
Set mCol = Nothing
End Sub
' end colQuestions
''
' Cl*** 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 Cl***_Initialize()
Set mcolQuestions = New colQuestions
End Sub
Private Sub Cl***_Terminate()
Set mcolQuestions = Nothing
End Sub

-ralph


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top