Passing Variables.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The Doctor

    Passing Variables.

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


  • Dean Earley

    #2
    Re: Passing Variables.

    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 (dean.earley@ic ode.co.uk)
    i-Catcher Development Team

    iCode Systems

    Comment

    • Ralph

      #3
      Re: Passing Variables.


      "The Doctor" <x203@optusnet. com.auwrote in message
      news:450828e7$0 $17211$afc38c87 @news.optusnet. com.au...
      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


      Comment

      • The Doctor

        #4
        Re: Passing Variables.

        "Ralph" <nt_consulting6 4@yahoo.comwrot e in message
        news:pOednR0nI9 CPp5XYnZ2dnUVZ_ rWdnZ2d@arkansa s.net...
        >
        "The Doctor" <x203@optusnet. com.auwrote in message
        news:450828e7$0 $17211$afc38c87 @news.optusnet. com.au...
        >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.


        Comment

        • Ralph

          #5
          Re: Passing Variables.


          "The Doctor" <x203@optusnet. com.auwrote in message
          news:4508fdb7$0 $5105$afc38c87@ news.optusnet.c om.au...
          "Ralph" <nt_consulting6 4@yahoo.comwrot e in message
          news:pOednR0nI9 CPp5XYnZ2dnUVZ_ rWdnZ2d@arkansa s.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


          Comment

          • The Doctor

            #6
            Re: Passing Variables.

            "Ralph" <nt_consulting6 4@yahoo.comwrot e in message
            news:ZpSdnT-9mvzi85TYnZ2dnU VZ_qudnZ2d@arka nsas.net...
            >
            "The Doctor" <x203@optusnet. com.auwrote in message
            news:4508fdb7$0 $5105$afc38c87@ news.optusnet.c om.au...
            >"Ralph" <nt_consulting6 4@yahoo.comwrot e in message
            >news:pOednR0nI 9CPp5XYnZ2dnUVZ _rWdnZ2d@arkans as.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
            >
            >
            3 Forms.

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

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

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



            Comment

            • Steve Gerrard

              #7
              Re: Passing Variables.


              "The Doctor" <x203@optusnet. com.auwrote in message
              news:4508fdb7$0 $5105$afc38c87@ news.optusnet.c om.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.


              Comment

              • Ralph

                #8
                Re: Passing Variables.

                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(Can cel As Integer)
                Set mCStore = Nothing
                End Sub
                Private Sub Option1_Click(I ndex As Integer)
                mOption = Index
                End Sub
                Private Sub cmdDone_Click()
                mCStore.Questio nAdd 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(Can cel As Integer)
                Set mCStore = Nothing
                End Sub
                Private Sub Option1_Click(I ndex As Integer)
                mOption = Index
                End Sub
                Private Sub cmdDone_Click()
                mCStore.Questio nAdd 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.GetAllA nswers()
                End Sub
                Private Sub Form_Unload(Can cel 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.Qu estionNum = nQuestion
                objNewMember.Qu estionStr = QuestionStr
                objNewMember.An swerStr = sAnswer
                objNewMember.An swerOpt = 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(vntIndexKe y 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(vntIndex Key As Long)
                mCol.Remove "Q" & vntIndexKey
                End Sub
                Public Property Get NewEnum() As IUnknown
                Set NewEnum = mCol.[_NewEnum]
                End Property
                Private Sub Class_Initializ e()
                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(nQu estion As Long, sQuestion As String, sAnswer As
                String, lOption As Long)
                mcolQuestions.A dd 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.I tem(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.C ount()
                Set cls = mcolQuestions.I tem(lCnt)
                sTmp = sTmp & cls.AnswerStr & vbCrLf
                Set cls = Nothing ' redundant
                Next lCnt
                GetAllAnswers = sTmp
                End Function
                Private Sub Class_Initializ e()
                Set mcolQuestions = New colQuestions
                End Sub
                Private Sub Class_Terminate ()
                Set mcolQuestions = Nothing
                End Sub

                -ralph


                Comment

                Working...