Results 1 to 4 of 4

Thread: I'm getting Overflow and Subscript out of range errors

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Question I'm getting Overflow and Subscript out of range errors

    I have scrollable picture boxes within a single picture box in which I'm getting two types of errors depending on the number of picture boxes I'm trying to scroll. I've been working on solutions to prevent these overflows, but one solution seems to lead to another occurring somewhere else. I could really use some help nailing this down. I have attached the VB6 files and included the line of code where the errors are occurring.
    Errors that I'm getting:

    Run-time error '9':
    Subscript out of range
    Code:
    If ClientArrayTop(i) - l < -853 Then

    Run-time error '6':
    Overflow
    Code:
    VScroll1.Max = -1524 + picClient(picClient.UBound).Top + picClient(picClient.UBound).Height

    Here is the Entire code:
    Code:
    Dim l%
    Dim ClientArrayTop() As Integer
    Dim CurrentListCount%
    
    Private Sub cmdOK_Click()
    'Get the current highest array number of picClient Picture Boxes
    CurrentListCount = picClient.Count - 1
    
    'Number in txtNumber must be > than 0
    If txtNumber.Text < 1 Then
    MsgBox ("You must enter in a number greater than 0!")
    End If
    
    
    'create new picture boxes according to txtNumber.Text
    Dim i%
    For i = 0 To txtNumber.Text - 1
        If i > CurrentListCount Then
            Load picClient(picClient.UBound + 1)
            Set picClient(picClient.UBound).Container = _
            picMain
            With picClient(picClient.UBound)
            .Left = picClient(0).Left
            .Top = picClient(picClient.UBound - 1).Top + _
            picClient(picClient.UBound - 1).Height + 120
            .Visible = True
            End With
        'create new text boxes according to txtNumber.Text
            Load txtClient(txtClient.UBound + 1)
            Set txtClient(txtClient.UBound).Container = _
            picClient(picClient.UBound)
            With txtClient(txtClient.UBound)
            .Left = txtClient(0).Left
            .Top = txtClient(0).Top
            .Text = "Text" & picClient.UBound + 1
            .Visible = True
            End With
        End If
    Next
    ChangeClientSize
    End Sub
    
    Private Sub Form_Load()
    ChangeClientSize
    End Sub
    
    
    Private Sub txtNumber_KeyPress(KeyAscii As Integer)
    'character in txtNumber must be a number
    If InStr("0123456789", Chr(KeyAscii)) = 0 Then
        KeyAscii = 0
    End If
    End Sub
    
    Private Sub VScroll1_Change()
    VertScroll
    End Sub
    
    Private Sub VScroll1_Scroll()
    VertScroll
    End Sub
    
    Sub VertScroll()
    l = VScroll1.Value
    Dim i As Integer
    For i = 0 To picClient.UBound
    If ClientArrayTop(i) - l < -853 Then
    picClient(i).Top = -853
    ElseIf ClientArrayTop(i) - l > 1692 Then
    picClient(i).Top = 1692
    Else
    picClient(i).Top = ClientArrayTop(i) - l
    End If
    Next
    End Sub
    
    Sub ChangeClientSize()
    VScroll1.Max = -1524 + picClient(picClient.UBound).Top + picClient(picClient.UBound).Height
    ReDim ClientArrayTop(0 To picClient.UBound) As Integer
    Dim i As Integer
    For i = 0 To picClient.UBound
    ClientArrayTop(i) = picClient(i).Top
    Next
    End Sub
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    Jun 2007
    Location
    Kent, UK
    Posts
    4

    Re: I'm getting Overflow and Subscript out of range errors

    The overflow problem occurs because for a scroll bar .Max is an integer and the height of your container can be greater than 32767 (the maximum for an integer). I would suggest you use some sort of scaling for your scroll bar.

    Have not reproduced the other error!

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: I'm getting Overflow and Subscript out of range errors

    For your subscript out of range error, think you'll find that your UBound(ClientArrayTop) < picClient.UBound. Easy enough to check. If this is so, verify that changing VScroll1.Max doesn't cause a VScroll1_Change event to occur. If it does, this could be causing the problem

    For your overflow error, think you'll find that the MaxValue is less/greater than the min/max integer range. Maybe using scalemode of pixels may correct that problem. A twip which is what I assume is the current scalemode is about 15x more than a pixel; therefore, your values are 15x larger than needed here
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    13

    Re: I'm getting Overflow and Subscript out of range errors

    Thanks for the input. I changed the scale mode to characters and adjusted all my numbers accordingly. That seems to have taken care of the overflow problem.

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