Results 1 to 5 of 5

Thread: adding items of an array

  1. #1

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    49

    Post


    How do I add the contents of a textbox array without enshrining the number of indexes in my code?

    For example, I can't use:

    Val(text2.text)= Val(text1(1).text) + Val(text1(2).text) ..etc.., because my program creates textboxes at runtime.

    Is there any 'For...Next' way of doing it or something?

    Thanks for any help!

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    Will this work...

    Dim Cnt As Integer
    Dim FinalVal As Integer

    FinalVal=0
    For Cnt= Text1.LBound To Text1.UBound:FinalCnt = FinalCnt + CInt(Text1(Cnt).Text):Next

    Text2.Text = CStr(FinalVal)

    Edited by Chris on 03-12-2000 at 08:26 AM

  3. #3

    Thread Starter
    Member
    Join Date
    May 1999
    Posts
    49

    Post


    Chris, thanks for your help, but I couldn't quite get it to work. I didn't get an error, but textbox3 didn't become visible, as it should if the sum of the text1 array is equal to the figure inserted in the text2 box.

    Any help would be appreciated!

    Private Sub Text2_KeyPress(keyascii As Integer)
    keyascii = Asc(Chr(keyascii))
    Dim Cnt As Integer
    Dim FinalVal As Integer
    FinalVal = 0
    For Cnt = Text1.LBound To Text1.UBound: FinalCnt = FinalCnt + CInt(Text1(Cnt).Text): Next
    If Text2.Text = CStr(FinalVal) And keyascii = vbKeyReturn Then
    Text3.Visible = True
    End If
    End Sub

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Posts
    82

    Post

    i don't really understand what you are trying to do here, but the code seems to work if...

    you enter a number in text2 and press enter
    if the number you entered is equal to the sum of the textboxes, text3 become visible

    if you want text3 to become visible even if the number entered in text2 is not equal to the sum of text1() change and to or in your if test at the bottom your keypress event


  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    Your code should work fine, but i notice the following code may not need in this sub procedure.

    keyascii = Asc(Chr(keyascii))

    because it first convert to character then convert back to ascii code, which it the same as the original keyascii.

    Did other control or sub procedure cause set the Text3 visible properties to false, hence, will will not see the Text3 textbox even your set it visible in the Text2_KeyPress procedure.






    Chris, thanks for your help, but I couldn't quite get it to work. I didn't get an error, but textbox3 didn't become visible, as it should if the sum of the text1 array is equal to the figure inserted in the text2 box.

    Any help would be appreciated!

    Private Sub Text2_KeyPress(keyascii As Integer)
    keyascii = Asc(Chr(keyascii))
    Dim Cnt As Integer
    Dim FinalVal As Integer
    FinalVal = 0
    For Cnt = Text1.LBound To Text1.UBound: FinalCnt = FinalCnt + CInt(Text1(Cnt).Text): Next
    If Text2.Text = CStr(FinalVal) And keyascii = vbKeyReturn Then
    Text3.Visible = True
    End If
    End Sub


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width