13 Replies - 1711 Views - Last Post: 31 May 2012 - 06:17 AM Rate Topic: ***** 1 Votes

#1 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 07:17 AM

Hi i try to debug something with an array of photo. I got 3 limit of photo(depend of the tab i am) 4,12 and 20.
When i load less than 12 photo and i try to deselect one in a tab that can handle 12 photo or more i got an <Subscript out of range> message . I don't know for wich reason my for loop keep looping to try to reach the limit but i only have 5 photo and the limit is 12.

Here the code : the bold is where it crash
Private Sub image1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  
  Dim i As Integer
  Dim z As String
  Dim p As Integer
  Dim j As Integer
  Dim Idx As Integer
  
  If Button = vbRightButton Then
    On Error GoTo Err_message
    i = ((iCurr_Page - 1) * 12) + Index + 1
    If Tab_Photo$(i) = "" Then
      Beep
    Else
'      On Error GoTo Err_Handler
'      z$ = "C:\irview\i_view32.exe " + Path_Pict$ + "\" + Tab_Photo$(i) + " /one"
      z$ = sPath_Pict_Search + "\" + Tab_Photo$(i)
      Gros_Plan.Image1.Picture = LoadPicture(z$)
      Gros_Plan.Show 1
'      a = Shell(z$, 1)
      On Error GoTo 0
    End If
  Else
    i = Index
    p = ((iCurr_Page - 1) * 12) + i + 1
    If Shape1(i).Visible = True Then
   
      [b]For j = 1 To (iMaxPhoto)
        If Tab_PSel%(j) = p Then
          If j <= 4 Then
            Si(j - 1).Picture = LoadPicture()
          End If
          Tab_PSel%(j) = 0
          iCurr_Sel = j - 1
        End If
      Next j[/b]
      Shape1(i).Visible = False
    Else
      If Tab_Photo$(p) = "" Then
        Beep
        Exit Sub
      End If
      Idx = -1
      For j = 0 To (iMaxPhoto - 1)
        If Tab_PSel(j + 1) = 0 Then
          Idx = j
          j = 10000
        End If
      Next j
      
      If Idx < 0 Then
        Beep
        MsgBox "Vous avez atteint la limite de photo à sélectionner.", vbExclamation, "Phauto+"
      Else
        Tab_PSel%(Idx + 1) = p
       
        If Idx < 4 Then
          Si(Idx).Picture = Image1(i).Picture
        End If
        iCurr_Sel = Idx
        Shape1(i).Visible = True
      End If
    End If
  End If
  
  Exit Sub
Err_Handler:
  Beep
  MsgBox "Le programme d'agrandissement de photos 'C:\irview\i_view32.exe' est inexistant...", vbCritical, "Phauto+"
Err_message:
  Beep
  MsgBox "Vous devez sélectionner une photo.", vbCritical, "Phauto+"
End Sub


Thank you for you time

Is This A Good Question/Topic? 0
  • +

Replies To: Error Subscript out of range when I deselects a photo

#2 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 07:59 AM

Subscript out of range means that you're trying to access an array element that doesn't exist. For example, if I have an array x with 5 elements, x(5) will be out of range because the allowable subscripts are 0, 1, 2, 3, and 4. The most common reason for this error is missing the fact that an array of x elements has an upper bound of x-1.

If you will give us the line of code where the error occurs (rather than asking us to sort through 73 lines of code to get as well-informed as you already are about your error), we can perhaps give you more specific help.
Was This Post Helpful? 1
  • +
  • -

#3 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:09 AM

oops sorry i tough the line were in bold but here where it crash
on the if tab_psel%(j)
If Tab_PSel%(j) = p Then
            If j <= 4 Then
                Si(j - 1).Picture = LoadPicture()
            End If
            Tab_PSel%(j) = 0
            iCurr_Sel = j - 1
        End If
      Next j



Just like you said it's trying to access an array element that doesn't exist , i just don't know how to make him stop :/
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:11 AM

Ok, so put a breakpoint on your line 1. Then go to your immediate window and enter ? j. This will give you the value of j. Then enter ? ubound(tab_psel%). You should find that the current value for j is greater than the upper bound of your array. However, since you haven't posted the code that defines the size of your array (one would think that this is relevant data, n'est-ce pas?), this is a "shot in the dark", but it's the best I can do with your somewhat secretive ways. :)
Was This Post Helpful? 1
  • +
  • -

#5 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:17 AM

the max size of the array is 12. At the moment my size is 5 elements on 12.
On load if i go in the immediate window i got :
?j
 0 
? ubound(tab_psel%)
 5 



but yeah when it crash i got :
?j
 6 
? ubound(tab_psel%)
 5 


Was This Post Helpful? 0
  • +
  • -

#6 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:36 AM

Quote

the max size of the array is 12.
Prove it.
Was This Post Helpful? 0
  • +
  • -

#7 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:46 AM

Private Sub SetFocus_On_Tab()

    Select Case SSTab1.Tab
      Case 0
        ' Véhicule
        iMaxPhoto = 12
        ReDim Preserve Tab_PSel%(iMaxPhoto)
        txt_NoDossier.SetFocus

?iMaxPhoto
 12


So the array should be 12
Was This Post Helpful? 0
  • +
  • -

#8 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 08:55 AM

Problem solve , thank you guys :D
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 30 May 2012 - 02:33 PM

I don't suppose you'd care to explain the solution, so that other people reading this can be helped as well?

This post has been edited by BobRodes: 30 May 2012 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 31 May 2012 - 05:01 AM

Yeah,sorry. I just add a ReDim Preserve Tab_PSel%(iMaxPhoto)
Was This Post Helpful? 0
  • +
  • -

#11 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 31 May 2012 - 06:06 AM

But you already did that, didn't you? What was the problem, and what was the solution exactly?
Was This Post Helpful? 0
  • +
  • -

#12 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 31 May 2012 - 06:12 AM

No i forgot to do that, and i have a function for when i switch tab my number of photo allowed change and i wasn't changing my array size when i was switching tab. In my switching tab function i add my redim preserve and everything was okay . I just see in my post the redim was there ^^ it was after the probleme was solve.
Thx Mister Bobrhodes you were very helpfull and make me discover the immediate windows hehe.
Was This Post Helpful? 0
  • +
  • -

#13 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Error Subscript out of range when I deselects a photo

Posted 31 May 2012 - 06:13 AM

Aha! It's usually something like that. And the debug/immediate window is important, basic knowledge. Glad you made use of it. :)
Was This Post Helpful? 0
  • +
  • -

#14 beetlejuice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 30-May 12

Re: Error Subscript out of range when I deselects a photo

Posted 31 May 2012 - 06:17 AM

I'm still new in the world of programmation and i work in a project that isn't mine , so i'm a bit lost hehe.
Have a nice to you and all other person who read this :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1