Results 1 to 17 of 17

Thread: subscript out of range

  1. #1

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Resolved subscript out of range

    VB Code:
    1. Option Explicit
    2.  
    3. Public prntr As Printer
    4. Public tempstr As String
    5. Public tempint As Integer
    6. Public longname As String
    7. Public shortname As String
    8.  
    9.  
    10. Private Sub cmdExit_Click()
    11.     Unload Me
    12. End Sub
    13.  
    14. Private Sub Form_Load()
    15. Dim names() As String
    16. Dim intcount As Integer
    17. intcount = 0
    18.     For Each prntr In Printers
    19.         If Mid(prntr.DeviceName, 3, 5) = "srver" Then
    20.            intcount = intcount + 1
    21.            'resize array to fit added data
    22.            ReDim Preserve names(intcount, 2)
    23.            'add the full string to the array
    24.            names(intcount, 1) = prntr.DeviceName
    25.            shortname = Right(prntr.DeviceName, 3)
    26.            'add the short string to the array
    27.            names(intcount, 2) = shortname
    28.            'add the short name to the listbox
    29.            List1.AddItem names(intcount, 2)
    30.         End If
    31.     Next prntr
    32. End Sub

    as soon as intcount = 2 i get subscript out of range ... can anyone spot the mistake .... i'm lost on this
    Last edited by aturner; Apr 12th, 2005 at 09:22 AM.
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: subscript out of range

    Try switching your array definitions around:
    VB Code:
    1. Dim names() As String
    2.     Dim intcount As Integer
    3.     intcount = 0
    4.         For Each prntr In Printers
    5.              If Mid(prntr.DeviceName, 3, 5) = "srver" Then
    6.                intcount = intcount + 1
    7.                'resize array to fit added data
    8.                ReDim Preserve names(2, intcount)
    9.                'add the full string to the array
    10.                names(1, intcount) = prntr.DeviceName
    11.                shortname = Right(prntr.DeviceName, 3)
    12.                'add the short string to the array
    13.                names(2, intcount) = shortname
    14.                'add the short name to the listbox
    15.                List1.AddItem names(intcount, 2)
    16.             End If
    17.         Next prntr

  3. #3

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    ok, that worked ...

    but i dont understand why ???????






    ty btw
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  4. #4
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: subscript out of range

    I'm not sure myself - I don't understand the low-level stuff, but I guess it might have something to do with the way the memory is allocated for dynamic multi-dimensioned arrays..? Hopefully, someone cleverer than me will be able to tell you.

  5. #5

  6. #6

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    thank you both
    much appreciated
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  7. #7

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    next problem

    VB Code:
    1. Option Explicit
    2.  
    3. Public prntr As Printer
    4. Public tempstr As String
    5. Public tempint As Integer
    6. Public longname As String
    7. Public shortname As String
    8.  
    9.  
    10. Private Sub cmdExit_Click()
    11.     Unload Me
    12. End Sub
    13.  
    14. Private Sub cmdSetDefault_Click()
    15.     SetDefault (List1.ListIndex + 1)
    16. End Sub
    17.  
    18. Private Sub SetDefault(prndefault As String)
    19.     '[help]
    20.     MsgBox names(1, prndefault) & names(3, prndefault)
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24. Dim names() As String
    25. Dim intcount As Integer
    26. intcount = 0
    27.     For Each prntr In Printers
    28.         If Mid(prntr.DeviceName, 3, 5) = "WDC01" Then
    29.            intcount = intcount + 1
    30.            'resize array to fit added data
    31.            ReDim Preserve names(3, intcount)
    32.            'add the full string to the array
    33.            names(1, intcount) = prntr.DeviceName
    34.            shortname = Right(prntr.DeviceName, 3)
    35.            'add the short string to the array
    36.            names(2, intcount) = shortname
    37.            'add the port to the array
    38.            names(3, intcount) = ",winspool," & prntr.Port
    39.            'add the short name to the listbox
    40.            List1.AddItem names(2, intcount)
    41.         End If
    42.     Next prntr
    43. End Sub

    ok, the main bit of code needs to be done as it filters through the printers.
    the user then selects the printer from the listbox and presses a button to set it as default.

    only problem is, i cant declaire the array as public, but i cant access it when i click the button.

    anyone have any ideas?
    Last edited by aturner; Apr 12th, 2005 at 08:50 AM.
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: subscript out of range

    Does this work, if you dim names() at the form level?




    VB Code:
    1. Option Explicit
    2.  
    3. Public prntr As Printer
    4. Public tempstr As String
    5. Public tempint As Integer
    6. Public longname As String
    7. Public shortname As String
    8. Dim names() As String
    9.  
    10. Private Sub cmdExit_Click()
    11.     Unload Me
    12. End Sub
    13.  
    14. Private Sub cmdSetDefault_Click()
    15.     SetDefault (List1.ListIndex + 1)
    16. End Sub
    17.  
    18. Private Sub SetDefault(prndefault As String)
    19.     '[help]
    20.     MsgBox names(1, prndefault) & names(3, prndefault)
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24. Dim intcount As Integer
    25. intcount = 0
    26.     For Each prntr In Printers
    27.         If Mid(prntr.DeviceName, 3, 5) = "WDC01" Then
    28.            intcount = intcount + 1
    29.            'resize array to fit added data
    30.            ReDim Preserve names(3, intcount)
    31.            'add the full string to the array
    32.            names(1, intcount) = prntr.DeviceName
    33.            shortname = Right(prntr.DeviceName, 3)
    34.            'add the short string to the array
    35.            names(2, intcount) = shortname
    36.            'add the port to the array
    37.            names(3, intcount) = ",winspool," & prntr.Port
    38.            'add the short name to the listbox
    39.            List1.AddItem names(2, intcount)
    40.         End If
    41.     Next prntr
    42. End Sub
    Or are you trying to access names from multiple forms.
    If so, then you should "Public Names() as string" in a module.

  9. #9
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: subscript out of range

    OOPS!
    Just saw this:

    VB Code:
    1. Private Sub SetDefault(prndefault As String)
    2.     '[help]
    3.     MsgBox names(1, prndefault) & names(3, prndefault)
    4. End Sub

    What kind of an array uses strings for indexes?

  10. #10

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    tbh, i didnt know you could do that ??? will try now tho
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  11. #11

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Resolved Re: subscript out of range

    ta very much, worked a treat.
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  12. #12

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    Quote Originally Posted by NotLKH
    OOPS!
    Just saw this:

    VB Code:
    1. Private Sub SetDefault(prndefault As String)
    2.     '[help]
    3.     MsgBox names(1, prndefault) & names(3, prndefault)
    4. End Sub

    What kind of an array uses strings for indexes?
    erm ... *** have i done there??? ...
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  13. #13
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: subscript out of range

    It might be working, but you're making your progie force a conversion, from string to numeric, probably slowing things down.

    {And, more than likely, whatever is calling it is probably forcing a conversin from numeric to string, doubly slowing things down.}

  14. #14

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    Quote Originally Posted by NotLKH
    It might be working, but you're making your progie force a conversion, from string to numeric, probably slowing things down.

    {And, more than likely, whatever is calling it is probably forcing a conversin from numeric to string, doubly slowing things down.}
    yer, it was a crummy oversight ... now fixed
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  15. #15

  16. #16
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: subscript out of range

    jsyk,
    at the form level,
    Dim == Private

    So, its not accesable between forms, its just seeable everywhere in its parent form.

    "Public" is directly accessable on its parent form, but only accessable from other forms via:

    ParentFormName.Varname

    BUT, if you do a Public in a module, then your variable is directly accessable everywhere!

    -Lou

  17. #17

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Re: subscript out of range

    ta very much
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

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