PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] [Help] subscript out of range-VBForums
Results 1 to 14 of 14

Thread: [RESOLVED] [Help] subscript out of range

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    74

    Resolved [RESOLVED] [Help] subscript out of range

    VB Code:
    1. Private Sub cmddeposit_Click()
    2.  
    3. amt(size) = Val(txtdeposit.Text)
    4.  
    5. txtbalance.Text = ""
    6. If amt(size) > 0 Then
    7.   If size = 1 Then
    8.     balance(size) = balance(size) + amt(size)
    9.    Else
    10.      balance(size) = balance(size - 1) + amt(size)
    11.    End If
    12.    X = MsgBox("Amount Deposited = $" & amt(size), vbOKOnly, Deposit)
    13.    trans(size) = "Deposit"
    14.    size = size + 1
    15.    
    16. Else
    17.    X = MsgBox("Invalid amt", vbOKOnly, Deposit)
    18. End If
    19. End Sub

    highlighted with subscript out of range is

    VB Code:
    1. amt(size) = Val(txtdeposit.Text)

    any idea why its wrong?
    Hacking is like a drug, one taste and you can't stop. Unless you fill it up with another drug.. Learning.

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [Help] subscript out of range

    How is amt() declared? Btw... UBound (or the last index) equals array's size -1 if it's a 0 (zero) based array.

  3. #3
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: [Help] subscript out of range

    try this

    amt(size-1) = Val(txtdeposit.Text)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    74

    Re: [Help] subscript out of range

    Dim balance(1 To 10) As Double
    Dim size As Integer
    Dim amt(1 To 10) As Double
    Dim trans(1 To 10) As String

    it still doesnt work if it size-1
    Hacking is like a drug, one taste and you can't stop. Unless you fill it up with another drug.. Learning.

  5. #5
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: [Help] subscript out of range

    is "size" between 1 and 10 when you execute this: amt(size) = Val(txtdeposit.Text)

    my guess is that size = 0

  6. #6
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: [Help] subscript out of range

    Yah... size is 0...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    74

    Re: [Help] subscript out of range

    yah i put size 0 too but it says subscript out of range? what does this mean? it always happened when i click on deposit
    Hacking is like a drug, one taste and you can't stop. Unless you fill it up with another drug.. Learning.

  8. #8
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: [Help] subscript out of range

    "subscript out of range" means that the index of the array is either to big or to small. In your case the array is "amt" and the index is "size".

    "Dim amt(1 To 10) As Double" means that the minimum index is 1 and the maximum index is 10.

    When you try to populate your array you do "amt(size) = Val(txtdeposit.Text)" what is the same as "amt(0) = Val(txtdeposit.Text)". But 0 is too small, size should at least be 1. so it gives you the error "subscript out of range".

    "amt(size) = Val(txtdeposit.Text)" will work if size is between 1 and 10

  9. #9
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: [Help] subscript out of range

    Quote Originally Posted by eklips
    yah i put size 0 too but it says subscript out of range? what does this mean? it always happened when i click on deposit

    when you have defined the array subscript range from 1 to 10.. you can't have a subscript index 0... set the value of size to something between 1 to 10... and that should work.. .

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    74

    Re: [Help] subscript out of range

    so my coding is wrong? what should be to right 1?
    Hacking is like a drug, one taste and you can't stop. Unless you fill it up with another drug.. Learning.

  11. #11
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: [Help] subscript out of range

    Make sure the variable size is at least 1 and at most 10 before this code can be executed:
    VB Code:
    1. amt(size) = Val(txtdeposit.Text)

  12. #12
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: [Help] subscript out of range

    maybe something like this would help

    VB Code:
    1. Private Sub Form_Load()
    2.   size=LBound(amt)
    3. End Sub

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    74

    Re: [Help] subscript out of range

    Private Sub Form_Load()
    size = 1
    End Sub

    i used this and its working now ^^
    Hacking is like a drug, one taste and you can't stop. Unless you fill it up with another drug.. Learning.

  14. #14
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [RESOLVED] [Help] subscript out of range

    It's better to use what moinkhan suggested in #12. That way the code won't cause errors if you'll change it to a zero based array.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width