|
-
Apr 11th, 2005, 10:22 AM
#1
Thread Starter
Addicted Member
subscript out of range
VB Code:
Option Explicit
Public prntr As Printer
Public tempstr As String
Public tempint As Integer
Public longname As String
Public shortname As String
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim names() As String
Dim intcount As Integer
intcount = 0
For Each prntr In Printers
If Mid(prntr.DeviceName, 3, 5) = "srver" Then
intcount = intcount + 1
'resize array to fit added data
ReDim Preserve names(intcount, 2)
'add the full string to the array
names(intcount, 1) = prntr.DeviceName
shortname = Right(prntr.DeviceName, 3)
'add the short string to the array
names(intcount, 2) = shortname
'add the short name to the listbox
List1.AddItem names(intcount, 2)
End If
Next prntr
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
-
Apr 11th, 2005, 10:30 AM
#2
Frenzied Member
Re: subscript out of range
Try switching your array definitions around:
VB Code:
Dim names() As String
Dim intcount As Integer
intcount = 0
For Each prntr In Printers
If Mid(prntr.DeviceName, 3, 5) = "srver" Then
intcount = intcount + 1
'resize array to fit added data
ReDim Preserve names(2, intcount)
'add the full string to the array
names(1, intcount) = prntr.DeviceName
shortname = Right(prntr.DeviceName, 3)
'add the short string to the array
names(2, intcount) = shortname
'add the short name to the listbox
List1.AddItem names(intcount, 2)
End If
Next prntr
-
Apr 11th, 2005, 10:33 AM
#3
Thread Starter
Addicted Member
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
-
Apr 11th, 2005, 10:42 AM
#4
Frenzied Member
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.
-
Apr 11th, 2005, 11:25 AM
#5
Re: subscript out of range
When you use Preserve, you can only resize the last dimension.
-
Apr 12th, 2005, 02:58 AM
#6
Thread Starter
Addicted Member
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
-
Apr 12th, 2005, 08:47 AM
#7
Thread Starter
Addicted Member
next problem
VB Code:
Option Explicit
Public prntr As Printer
Public tempstr As String
Public tempint As Integer
Public longname As String
Public shortname As String
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdSetDefault_Click()
SetDefault (List1.ListIndex + 1)
End Sub
Private Sub SetDefault(prndefault As String)
'[help]
MsgBox names(1, prndefault) & names(3, prndefault)
End Sub
Private Sub Form_Load()
Dim names() As String
Dim intcount As Integer
intcount = 0
For Each prntr In Printers
If Mid(prntr.DeviceName, 3, 5) = "WDC01" Then
intcount = intcount + 1
'resize array to fit added data
ReDim Preserve names(3, intcount)
'add the full string to the array
names(1, intcount) = prntr.DeviceName
shortname = Right(prntr.DeviceName, 3)
'add the short string to the array
names(2, intcount) = shortname
'add the port to the array
names(3, intcount) = ",winspool," & prntr.Port
'add the short name to the listbox
List1.AddItem names(2, intcount)
End If
Next prntr
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
-
Apr 12th, 2005, 08:58 AM
#8
Re: subscript out of range
Does this work, if you dim names() at the form level?
VB Code:
Option Explicit
Public prntr As Printer
Public tempstr As String
Public tempint As Integer
Public longname As String
Public shortname As String
Dim names() As String
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdSetDefault_Click()
SetDefault (List1.ListIndex + 1)
End Sub
Private Sub SetDefault(prndefault As String)
'[help]
MsgBox names(1, prndefault) & names(3, prndefault)
End Sub
Private Sub Form_Load()
Dim intcount As Integer
intcount = 0
For Each prntr In Printers
If Mid(prntr.DeviceName, 3, 5) = "WDC01" Then
intcount = intcount + 1
'resize array to fit added data
ReDim Preserve names(3, intcount)
'add the full string to the array
names(1, intcount) = prntr.DeviceName
shortname = Right(prntr.DeviceName, 3)
'add the short string to the array
names(2, intcount) = shortname
'add the port to the array
names(3, intcount) = ",winspool," & prntr.Port
'add the short name to the listbox
List1.AddItem names(2, intcount)
End If
Next prntr
End Sub
Or are you trying to access names from multiple forms.
If so, then you should "Public Names() as string" in a module.
-
Apr 12th, 2005, 09:00 AM
#9
Re: subscript out of range
OOPS!
Just saw this:
VB Code:
Private Sub SetDefault(prndefault As String)
'[help]
MsgBox names(1, prndefault) & names(3, prndefault)
End Sub
What kind of an array uses strings for indexes?
-
Apr 12th, 2005, 09:01 AM
#10
Thread Starter
Addicted Member
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
-
Apr 12th, 2005, 09:02 AM
#11
Thread Starter
Addicted Member
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
-
Apr 12th, 2005, 09:04 AM
#12
Thread Starter
Addicted Member
Re: subscript out of range
 Originally Posted by NotLKH
OOPS!
Just saw this:
VB Code:
Private Sub SetDefault(prndefault As String)
'[help]
MsgBox names(1, prndefault) & names(3, prndefault)
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
-
Apr 12th, 2005, 09:06 AM
#13
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.}
-
Apr 12th, 2005, 09:08 AM
#14
Thread Starter
Addicted Member
Re: subscript out of range
 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
-
Apr 12th, 2005, 09:17 AM
#15
Re: subscript out of range
-
Apr 12th, 2005, 09:34 AM
#16
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
-
Apr 12th, 2005, 09:36 AM
#17
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|