Help with Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cephal0n
    New Member
    • Jan 2008
    • 38

    Help with Array

    I have a problem that's been bothering me for a week now. I want to learn more array in vba so I searched on forums and books made my own, according to my understanding.

    I have three textboxes: txtResult1, txtResult2, txtResult3
    and a user entry textbox to fill up my array: txtInput

    dim j as integer
    dim k as integer

    k=me.txtInput.v alue
    j=k

    for k=0 to 3

    Me("txtResult" & k).Value = j

    Next k

    this code works a little, my question is, Why can't my array store the first values I entered
    in my txtInput. My txtResult seems to accept only the last entry. please help how do I fix my array?

    thanks in advance!
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Originally posted by cephal0n
    .....
    my question is, Why can't my array store the first values I entered
    in my txtInput. My txtResult seems to accept only the last entry. please help how do I fix my array?
    .....
    Sorry, what array do you mean?

    Kind regards,
    Fish

    Comment

    • cephal0n
      New Member
      • Jan 2008
      • 38

      #3
      Thank you FishVal for reading this. the part where I input values in my txtInput textbox and store it all in k is my array,
      then all my results are shown my by txtResult1, txtResult2 and txtResult3

      I hope this clears it.
      thanks in advance!

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by cephal0n
        ....
        I hope this clears it.
        ....
        Not really.

        k is not an array. 3 controls may be considered as a kind of conceptual array consisting of 3 elements but that is more a terminology issue.
        Try to explain in words what kind of functionality you try to implement?

        Regards,
        Fish.

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          Yes in your code k is just a variable and it can only ever store a single value.
          If you set it to a different value, the previous value will be lost

          An array is like this
          [code=vb]
          Dim ArrayName(4) as integer
          [/code]

          here the variable ArrayName can hold 4 values and you access each value via the index

          so
          [code=vb]
          ArrayName(0)=3
          ArrayName(1)=5
          ArrayName(2)=45
          ArrayName(3)=12 3
          [/code]

          so if you now had something like this
          [code=vb]
          Dim i as integer
          i=ArrayName(0)+ ArrayName(2)
          [/code]

          then i would equal 48


          Hope this helps you

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by cephal0n
            I have a problem that's been bothering me for a week now. I want to learn more array in vba so I searched on forums and books made my own, according to my understanding.

            I have three textboxes: txtResult1, txtResult2, txtResult3
            and a user entry textbox to fill up my array: txtInput

            dim j as integer
            dim k as integer

            k=me.txtInput.v alue
            j=k

            for k=0 to 3

            Me("txtResult" & k).Value = j

            Next k

            this code works a little, my question is, Why can't my array store the first values I entered
            in my txtInput. My txtResult seems to accept only the last entry. please help how do I fix my array?

            thanks in advance!
            This will work nicely:
            [CODE=vb]
            Dim intCounter As Integer
            Dim aStrTextBoxValu es(1 To 3) As String

            For intCounter = 1 To 3
            aStrTextBoxValu es(intCounter) = Nz(Me.Controls( "txtResult" & CStr(intCounter )))
            Next

            'Let's make sure we caught the Text Box values, yes we did!
            For intCounter = LBound(aStrText BoxValues) To UBound(aStrText BoxValues)
            MsgBox aStrTextBoxValu es(intCounter)
            Next[/CODE]

            Comment

            • cephal0n
              New Member
              • Jan 2008
              • 38

              #7
              thanks Fishval and Delerna for your questions and they make sense
              What I want to achieve is that when I typed in 3 values in my txtInput
              This values will be cached by my variable k and hoping that for each values
              k receives it will be passed on my 3 control textbox (txtResult1, txtResult2, txtResult3)
              And hopefully it will be displayed as well, but clearly Fishval said it clear that my k is not a variable and quite impossible. I hope it's not.

              I learned a lot from Delerna's sample, but this are declared values from the array. What I wasn't able to do is that, to take the values upon entry from my txtInput and store them in my variable k and pass them to my controls. Displaying the strings that I entered earlier.

              I hope this clears some of your questions.
              Thanks guys for contributing and hoping to hear from you again!
              *************** *************** *************** *************** **************
              Thank you Adezii

              I was overwhelmed by your code sample and thank you for helping. But I was confused by some of it. What happened to my txtInput and I tried the code but only display 3 msgboxes with no values. Can you explain it?
              to me a bit simpler, my understanding in vba is foggy and still working on it.

              Thanks in advance!

              Comment

              • Delerna
                Recognized Expert Top Contributor
                • Jan 2008
                • 1134

                #8
                I am not clear on exactly what you are trying to achieve
                So to take what I showed you previously a little further towards your posts.

                Take a copy of your form and clear all code from its code page
                Paste this code and see if it clears things up for you
                [code=vb]
                Option Compare Database

                Dim idx As Integer
                Dim ArrayVariable(3 ) As String

                Private Sub Form_Load()
                idx = 0
                End Sub

                Private Sub txtInput_AfterU pdate()
                ArrayVariable(i dx) = txtInput.Text
                idx = idx + 1
                If idx > 2 Then idx = 0
                DisplayValues

                End Sub

                Private Sub DisplayValues()
                For k = 1 To 3
                Me("txtResult" & k) = ArrayVariable(k - 1)
                Next
                End Sub[/code]

                Comment

                • cephal0n
                  New Member
                  • Jan 2008
                  • 38

                  #9
                  Hi Delerna!

                  I apologize for the delayed response and would like to thank you
                  ALOT for helping me. the sample code you provided really is a big help
                  and I immediately used it and light begins to shine on me once again. I tried
                  applying it on other controls like my command button, when clicked the
                  stored value displays. and it worked amazingly!

                  A HUGE thank you for helping me!

                  Comment

                  • Delerna
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1134

                    #10
                    You a welcome, it is always a pleasure to help apreciative people.

                    Comment

                    Working...