By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Subscript out of range?

P: 3
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx...

Private Sub getBday()
bDay = List1.List(nextBday)
bDayArr = Split(bDay, " - ")
Text1.Text = bDayArr(0) ' subscript out of range
Text2.Text = bDayArr(2) ' subscript out of range
nextBday = nextBday + 1
End Sub
Jan 22 '08 #1
Share this Question
Share on Google+
6 Replies


kadghar
Expert 100+
P: 1,295
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx...

Private Sub getBday()
bDay = List1.List(nextBday)
bDayArr = Split(bDay, " - ")
Text1.Text = bDayArr(0) ' subscript out of range
Text2.Text = bDayArr(2) ' subscript out of range
nextBday = nextBday + 1
End Sub
How is bDayArr defined? what is its lower bound? what is its upper bound?
debug the code and right after passing the Split part check the locals window (or just place the mouse over the array in the code if using a newer version of VB) that'll show you what's its range.
Jan 23 '08 #2

Expert 5K+
P: 8,434
If you haven't already, I suggest you add Option Explicit at the top of the module (if this is VB6). And turn on the Require Variable Declaration option under Editor settings to enforce it in future. Allowing VB to create variable whenever and wherever it sees fit may be slightly more convenient, but can cause tons of problems. Not knowing exactly what variables you have, their scope and so on, is not a good way to work.

If that doesn't help to clear up the bug, here are a couple of more specific points I'd like to mention:
  • Are you sure that the date you're splitting is delimited by a dash and two spaces, as specified in this piece of code?
  • How many times have you looped before hitting the error? My guess is that you've simply gone one place too far. Unless I'm mistaken, the index for List1.List() starts at 0, and so ends one lower than you might expect. In other words, if you think it goes from 1 to 12, most likely it goes from 0 to 11.

P.S. Even if you're not using VB6, the "force variable declaration" thing is still important. I'm just not sure how you turn it on in the later versions.
Jan 23 '08 #3

kadghar
Expert 100+
P: 1,295
(...)

P.S. Even if you're not using VB6, the "force variable declaration" thing is still important. I'm just not sure how you turn it on in the later versions.
Option Explicit On (this one is default in VB 2005)
and
Option Strict On (It doesnt allow you to mix types of variables or assume anything, e.g. if Str1 is a string, and Str1 = "25" , and Int1 is an integer, then Int1=Str1 wont be allowed, you have to make the type conversion)(this one is off by default, but it's quite useful while working with math functions)
Jan 23 '08 #4

P: 3
Thx, funny, all I had to change was (2) to (1) , thx guys for making things clearer though.
Text2.Text = bDayArr(1) ' subscript out of range
Jan 23 '08 #5

Expert 5K+
P: 8,434
Option Explicit On...
Option Strict On ...
Hm... Option Strict sounds like it would have certain uses, but seems less important than Explicit. Of course, that may be just because I'm unfamiliar with it. In fact, it sounds like the sort of thing I try to do, and also try to encourage in others. That is, "know thy data (type)".

I think that in many cases it's not necessary to use the same data types for thigns to work properly. But if it means you have to know what data types you're using, that would be a good thing.

Anyway, thanks for the info kadghar.
Jan 23 '08 #6

Expert 5K+
P: 8,434
Thx, funny, all I had to change was (2) to (1) , thx guys for making things clearer though.
Text2.Text = bDayArr(1) ' subscript out of range
Glad to hear it's sorted out, then. :)

I did notice you were using 0 and 2, but didn't know enough about the data (yet) to know what you were picking out of it.

Anyway, I hope the discussion was helpful. I know I learned a bit.
Jan 23 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.