Arrays Problem SUBSCRIPT OUT OF RANGE
Author |
Message |
Vin Catrambo #1 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
My code below causes an error during run time. the VB Interpreter says my Array has SUBSCRIPT OUT OF RANGE error message According to documentation this shouldn't be happening. Any help will be appreciated Thanks in advance Vin Private Sub Command1_Click() 'Dim arrData(5, 1 To 1) ' This Array works when done this way Dim arrData() ' trying to do an array on the fly but doesn't work Dim ArrayPosition As Integer Dim ArrayItem As Integer Dim ArraySize As Integer ' Initialization ArrayItem = 1 Buffer = "" CharItem = "" ' Start Procedure ' Code Below ' Now getting to the code for array resizing now ' ArrayItem is given a value in the code ArraySize = ArrayItem ReDim Preserve arrData(ArraySize, 1 To 1) arrData(ArrayItem, 1) = Buffer Buffer = "" CharItem = "" ArrayItem = ArrayItem + 1 Next ArrayPosition ' Put array into a chart MSChart1.ChartData = arrData MSChart1.Visible = True End Sub ************************************* *PC Technical Support and products * * http://www.*-*-*.com/ * *************************************
|
Tue, 20 Nov 2001 03:00:00 GMT |
|
 |
Jeff Ashle #2 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
When you redim a dimension of an array to 1 (and you are using the default Option Base 0), the 1 item you have dim'ed is item ZERO. With your variables, ubound(arrData) is always ArraySize - 1, yet you try to ref member ArrayItem which = ArraySize. You are out of bounds! Try this: ArrayItem = 0 ' initialization in your loop: ReDim Preserve arrData(ArrayItem + 1, 1 To 1) arrData(ArrayItem, 1) = Buffer I won't even ask why you are using a second dimension with a single valid value . . . Quote:
> My code below causes an error during run time. the VB Interpreter > says my Array has SUBSCRIPT OUT OF RANGE error message > According to documentation this shouldn't be happening. > Any help will be appreciated > Thanks in advance > Vin > Private Sub Command1_Click() > 'Dim arrData(5, 1 To 1) ' This Array works when done this way > Dim arrData() ' trying to do an array on the fly but doesn't work > Dim ArrayPosition As Integer > Dim ArrayItem As Integer > Dim ArraySize As Integer > ' Initialization > ArrayItem = 1 > Buffer = "" > CharItem = "" > ' Start Procedure > ' Code Below > ' Now getting to the code for array resizing now > ' ArrayItem is given a value in the code > ArraySize = ArrayItem > ReDim Preserve arrData(ArraySize, 1 To 1) > arrData(ArrayItem, 1) = Buffer > Buffer = "" > CharItem = "" > ArrayItem = ArrayItem + 1 > Next ArrayPosition > ' Put array into a chart > MSChart1.ChartData = arrData > MSChart1.Visible = True > End Sub > ************************************* > *PC Technical Support and products * > *http://www.helpfixmypc.com * > *************************************
|
Tue, 20 Nov 2001 03:00:00 GMT |
|
 |
Vin Catrambo #3 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
Thanks for the reply. The basic reason for this code is so that I can extract data after certain sequence of characters etc. Exxample 1234=456.678, 3456=456.789, I need the 456.678 and 456.789 so the code searches 1234= I tell the code (input box) get the next 9 characters I have tried your suggestion, and it did not work so I will show the whole code Private Sub Command1_Click() 'Dim arrData(5, 1 To 1) Dim arrData() Dim TextSize As Integer Dim Buffer As String Dim NewPosition As Integer Dim ArrayPosition As Integer Dim CharItem As String Dim LastPosition As Integer Dim ArrayItem As Integer Dim ArraySize As Integer ' Initialization ArrayItem = 0 ' initialized here NewPosition = 1 LastPosition = 1 ArrayPosition = 1 TextSize = 0 Buffer = "" CharItem = "" ' Start Procedure ' Get text size from file TextSize = Len(RichTextBox2.Text) 'Start Loop For ArrayPosition = 1 To TextSize ArrayPosition = LastPosition ' Start inner loop For NewPosition = 1 To TextSize NewPosition = LastPosition ' check each character out if comma break out CharItem = Mid(RichTextBox2.Text, NewPosition, 1) If CharItem = "," Then LastPosition = NewPosition + 1 GoTo 1 End If Buffer = Buffer + CharItem LastPosition = NewPosition + 1 Next NewPosition 1 '************the buffer has correct message so far ArraySize = ArrayItem '************** get error message subscript out of range I believe on ' *************eithr this line or next ReDim Preserve arrData(ArrayItem + 1, 1 To 1) arrData(ArrayItem, 1) = Buffer ' Set the labels in the first series. Buffer = "" CharItem = "" ArrayItem = ArrayItem + 1 Next ArrayPosition MSChart1.ChartData = arrData MSChart1.Visible = True End Sub On Fri, 04 Jun 1999 23:40:09 -0400, Jeff Ashley Quote:
>When you redim a dimension of an array to 1 (and you are using the >default Option Base 0), the 1 item you have dim'ed is item ZERO. With >your variables, ubound(arrData) is always ArraySize - 1, yet you try to >ref member ArrayItem which = ArraySize. You are out of bounds! >Try this: > ArrayItem = 0 ' initialization >in your loop: > ReDim Preserve arrData(ArrayItem + 1, 1 To 1) > arrData(ArrayItem, 1) = Buffer >I won't even ask why you are using a second dimension with a single valid >value . . .
>> My code below causes an error during run time. the VB Interpreter >> says my Array has SUBSCRIPT OUT OF RANGE error message >> According to documentation this shouldn't be happening. >> Any help will be appreciated >> Thanks in advance >> Vin >> Private Sub Command1_Click() >> 'Dim arrData(5, 1 To 1) ' This Array works when done this way >> Dim arrData() ' trying to do an array on the fly but doesn't work >> Dim ArrayPosition As Integer >> Dim ArrayItem As Integer >> Dim ArraySize As Integer >> ' Initialization >> ArrayItem = 1 >> Buffer = "" >> CharItem = "" >> ' Start Procedure >> ' Code Below >> ' Now getting to the code for array resizing now >> ' ArrayItem is given a value in the code >> ArraySize = ArrayItem >> ReDim Preserve arrData(ArraySize, 1 To 1) >> arrData(ArrayItem, 1) = Buffer >> Buffer = "" >> CharItem = "" >> ArrayItem = ArrayItem + 1 >> Next ArrayPosition >> ' Put array into a chart >> MSChart1.ChartData = arrData >> MSChart1.Visible = True >> End Sub >> ************************************* >> *PC Technical Support and products * >> *http://www.helpfixmypc.com * >> *************************************
************************************* *PC Technical Support and products * *http://www.helpfixmypc.com * *************************************
|
Wed, 21 Nov 2001 03:00:00 GMT |
|
 |
Peter Beac #4 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
Vin, If using Redim Preserve I believe you can only redim the "last" dimension. Thus you can go: Redim Preserve A(1,1) . . . Redim Preserve A(1,2) but not: Redim Preserve A(1,1) . . . Redim Preserve A(2,1) HTH Peter Quote:
>Thanks for the reply. The basic reason for this code is so that I can >extract data after certain sequence of characters etc. >Exxample >1234=456.678, 3456=456.789, >I need the 456.678 and 456.789 >so the code searches 1234= >I tell the code (input box) get the next 9 characters >I have tried your suggestion, and it did not work so I will show the >whole code >Private Sub Command1_Click() > 'Dim arrData(5, 1 To 1) > Dim arrData() > Dim TextSize As Integer > Dim Buffer As String > Dim NewPosition As Integer > Dim ArrayPosition As Integer > Dim CharItem As String > Dim LastPosition As Integer > Dim ArrayItem As Integer > Dim ArraySize As Integer > ' Initialization > ArrayItem = 0 ' initialized here > NewPosition = 1 > LastPosition = 1 > ArrayPosition = 1 > TextSize = 0 > Buffer = "" > CharItem = "" > ' Start Procedure > ' Get text size from file > TextSize = Len(RichTextBox2.Text) > 'Start Loop > For ArrayPosition = 1 To TextSize > ArrayPosition = LastPosition >' Start inner loop > For NewPosition = 1 To TextSize > NewPosition = LastPosition > ' check each character out if comma break out > CharItem = Mid(RichTextBox2.Text, NewPosition, 1) > If CharItem = "," Then > LastPosition = NewPosition + 1 > GoTo 1 > End If > Buffer = Buffer + CharItem > LastPosition = NewPosition + 1 > Next NewPosition >1 >'************the buffer has correct message so far > ArraySize = ArrayItem >'************** get error message subscript out of range I believe on >' *************eithr this line or next > ReDim Preserve arrData(ArrayItem + 1, 1 To 1) > arrData(ArrayItem, 1) = Buffer ' Set the labels in the first series. > Buffer = "" > CharItem = "" > ArrayItem = ArrayItem + 1 > Next ArrayPosition > MSChart1.ChartData = arrData > MSChart1.Visible = True >End Sub >On Fri, 04 Jun 1999 23:40:09 -0400, Jeff Ashley
>>When you redim a dimension of an array to 1 (and you are using the >>default Option Base 0), the 1 item you have dim'ed is item ZERO. With >>your variables, ubound(arrData) is always ArraySize - 1, yet you try >to >>ref member ArrayItem which = ArraySize. You are out of bounds! >>Try this: >> ArrayItem = 0 ' initialization >>in your loop: >> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >> arrData(ArrayItem, 1) = Buffer >>I won't even ask why you are using a second dimension with a single >valid >>value . . .
>>> My code below causes an error during run time. the VB Interpreter >>> says my Array has SUBSCRIPT OUT OF RANGE error message >>> According to documentation this shouldn't be happening. >>> Any help will be appreciated >>> Thanks in advance >>> Vin >>> Private Sub Command1_Click() >>> 'Dim arrData(5, 1 To 1) ' This Array works when done this way >>> Dim arrData() ' trying to do an array on the fly but doesn't >work >>> Dim ArrayPosition As Integer >>> Dim ArrayItem As Integer >>> Dim ArraySize As Integer >>> ' Initialization >>> ArrayItem = 1 >>> Buffer = "" >>> CharItem = "" >>> ' Start Procedure >>> ' Code Below >>> ' Now getting to the code for array resizing now >>> ' ArrayItem is given a value in the code >>> ArraySize = ArrayItem >>> ReDim Preserve arrData(ArraySize, 1 To 1) >>> arrData(ArrayItem, 1) = Buffer >>> Buffer = "" >>> CharItem = "" >>> ArrayItem = ArrayItem + 1 >>> Next ArrayPosition >>> ' Put array into a chart >>> MSChart1.ChartData = arrData >>> MSChart1.Visible = True >>> End Sub >>> ************************************* >>> *PC Technical Support and products * >>> *http://www.helpfixmypc.com * >>> ************************************* >************************************* >*PC Technical Support and products * >*http://www.helpfixmypc.com * >*************************************
|
Wed, 21 Nov 2001 03:00:00 GMT |
|
 |
Vin Catrambo #5 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
Understood I found this snippet of code to guide me on the first use of MSCHART control Private Sub Command1_Click() Dim arrData(4, 1 To 1) arrData(1, 1) = 1 ' Set the labels in the first series. arrData(2, 1) = 2 arrData(3, 1) = 3 arrData(0, 1) = 8 MSChart1.ChartData = arrData End Sub Because of this I was trying to manipulate this simple code to an application where I will graph scientific Data from a chemical analyzer with comma delimeted data. As you can see from the example above (tested and works) I need to do the above from the text from my RichTextBox2. Where Private Sub Command1_Click() Dim arrData(4, 1 To 1) arrData(ArayItem, 1) = Buffer ' Set labels in the first series. arrData(ArayItem, 1) = Buffer ' which I want to do dynamically arrData(ArayItem, 1) = Buffer ' which I want to do dynamically arrData(ArayItem, 1) = Buffer ' which I want to do dynamically MSChart1.ChartData = arrData End Sub Any suggextion on how to do above dynamically??? Thanks In Advance Vin On Sat, 5 Jun 1999 15:01:52 +0100, "Peter Beach" Quote:
>Vin, >If using Redim Preserve I believe you can only redim the "last" dimension. >Thus you can go: > Redim Preserve A(1,1) > . . . > Redim Preserve A(1,2) >but not: > Redim Preserve A(1,1) > . . . > Redim Preserve A(2,1) >HTH >Peter
Quote: >>Thanks for the reply. The basic reason for this code is so that I can >>extract data after certain sequence of characters etc. >>Exxample >>1234=456.678, 3456=456.789, >>I need the 456.678 and 456.789 >>so the code searches 1234= >>I tell the code (input box) get the next 9 characters >>I have tried your suggestion, and it did not work so I will show the >>whole code >>Private Sub Command1_Click() >> 'Dim arrData(5, 1 To 1) >> Dim arrData() >> Dim TextSize As Integer >> Dim Buffer As String >> Dim NewPosition As Integer >> Dim ArrayPosition As Integer >> Dim CharItem As String >> Dim LastPosition As Integer >> Dim ArrayItem As Integer >> Dim ArraySize As Integer >> ' Initialization >> ArrayItem = 0 ' initialized here >> NewPosition = 1 >> LastPosition = 1 >> ArrayPosition = 1 >> TextSize = 0 >> Buffer = "" >> CharItem = "" >> ' Start Procedure >> ' Get text size from file >> TextSize = Len(RichTextBox2.Text) >> 'Start Loop >> For ArrayPosition = 1 To TextSize >> ArrayPosition = LastPosition >>' Start inner loop >> For NewPosition = 1 To TextSize >> NewPosition = LastPosition >> ' check each character out if comma break out >> CharItem = Mid(RichTextBox2.Text, NewPosition, 1) >> If CharItem = "," Then >> LastPosition = NewPosition + 1 >> GoTo 1 >> End If >> Buffer = Buffer + CharItem >> LastPosition = NewPosition + 1 >> Next NewPosition >>1 >>'************the buffer has correct message so far >> ArraySize = ArrayItem >>'************** get error message subscript out of range I believe on >>' *************eithr this line or next >> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >> arrData(ArrayItem, 1) = Buffer ' Set the labels in the first series. >> Buffer = "" >> CharItem = "" >> ArrayItem = ArrayItem + 1 >> Next ArrayPosition >> MSChart1.ChartData = arrData >> MSChart1.Visible = True >>End Sub >>On Fri, 04 Jun 1999 23:40:09 -0400, Jeff Ashley
>>>When you redim a dimension of an array to 1 (and you are using the >>>default Option Base 0), the 1 item you have dim'ed is item ZERO. With >>>your variables, ubound(arrData) is always ArraySize - 1, yet you try >>to >>>ref member ArrayItem which = ArraySize. You are out of bounds! >>>Try this: >>> ArrayItem = 0 ' initialization >>>in your loop: >>> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >>> arrData(ArrayItem, 1) = Buffer >>>I won't even ask why you are using a second dimension with a single >>valid >>>value . . .
>>>> My code below causes an error during run time. the VB Interpreter >>>> says my Array has SUBSCRIPT OUT OF RANGE error message >>>> According to documentation this shouldn't be happening. >>>> Any help will be appreciated >>>> Thanks in advance >>>> Vin >>>> Private Sub Command1_Click() >>>> 'Dim arrData(5, 1 To 1) ' This Array works when done this way >>>> Dim arrData() ' trying to do an array on the fly but doesn't >>work >>>> Dim ArrayPosition As Integer >>>> Dim ArrayItem As Integer >>>> Dim ArraySize As Integer >>>> ' Initialization >>>> ArrayItem = 1 >>>> Buffer = "" >>>> CharItem = "" >>>> ' Start Procedure >>>> ' Code Below >>>> ' Now getting to the code for array resizing now >>>> ' ArrayItem is given a value in the code >>>> ArraySize = ArrayItem >>>> ReDim Preserve arrData(ArraySize, 1 To 1) >>>> arrData(ArrayItem, 1) = Buffer >>>> Buffer = "" >>>> CharItem = "" >>>> ArrayItem = ArrayItem + 1 >>>> Next ArrayPosition >>>> ' Put array into a chart >>>> MSChart1.ChartData = arrData >>>> MSChart1.Visible = True >>>> End Sub >>>> ************************************* >>>> *PC Technical Support and products * >>>> *http://www.helpfixmypc.com * >>>> ************************************* >>************************************* >>*PC Technical Support and products * >>*http://www.helpfixmypc.com * >>*************************************
************************************* *PC Technical Support and products * *http://www.helpfixmypc.com * *************************************
|
Wed, 21 Nov 2001 03:00:00 GMT |
|
 |
Peter Beac #6 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
Vin, I'm still not quite clear on where you are having a problem. Do you mean that you have some CSV data of the following type: Series 1, 1234, 2222, 3245 Series 2, 4444, 3333, 2222 Series 3, 1111, 2222, 3333 and you want to construct a graph from this data? If this is right you have two problems - the first is parsing the CSV text, the second is getting it into the chart. Neither of these is particularly difficult, but I don't want to waste bandwidth describing things you already know, or indeed answering the wrong question. Perhaps you could get back to me, and I'll see what I can put together. BTW your question had me look at the help text for the ChartData property, and in particular the example given (VB5). I think it breaks new records for overall lack of clarity and typographical and logic errors per line! FWIW the following code constructed a small histogram of 6 bars: Option Explicit Private Sub Command1_Click() Dim V(5, 1) As Variant V(0, 0) = "Things" V(1, 0) = "Books" V(2, 0) = "Clothes" V(3, 0) = "Phones" V(4, 0) = "Cars" V(5, 0) = "Toys" V(0, 1) = 25 V(1, 1) = 12 V(2, 1) = 13 V(3, 1) = 14 V(4, 1) = 15 V(5, 1) = 16 cht.ChartData = V End Sub Look forward to hearing from you. Best wishes, Peter Quote:
>Understood I found this snippet of code to guide me on the first use >of MSCHART control >Private Sub Command1_Click() > Dim arrData(4, 1 To 1) > arrData(1, 1) = 1 ' Set the labels in the first series. > arrData(2, 1) = 2 > arrData(3, 1) = 3 > arrData(0, 1) = 8 > MSChart1.ChartData = arrData >End Sub >Because of this I was trying to manipulate this simple code to an >application where I will graph scientific Data from a chemical >analyzer with comma delimeted data. >As you can see from the example above (tested and works) I need to do >the above from the text from my RichTextBox2. >Where >Private Sub Command1_Click() > Dim arrData(4, 1 To 1) > arrData(ArayItem, 1) = Buffer ' Set labels in the first series. > arrData(ArayItem, 1) = Buffer ' which I want to do dynamically > arrData(ArayItem, 1) = Buffer ' which I want to do dynamically > arrData(ArayItem, 1) = Buffer ' which I want to do dynamically > MSChart1.ChartData = arrData >End Sub >Any suggextion on how to do above dynamically??? >Thanks In Advance >Vin >On Sat, 5 Jun 1999 15:01:52 +0100, "Peter Beach"
>>Vin, >>If using Redim Preserve I believe you can only redim the "last" >dimension. >>Thus you can go: >> Redim Preserve A(1,1) >> . . . >> Redim Preserve A(1,2) >>but not: >> Redim Preserve A(1,1) >> . . . >> Redim Preserve A(2,1) >>HTH >>Peter
>>>Thanks for the reply. The basic reason for this code is so that I >can >>>extract data after certain sequence of characters etc. >>>Exxample >>>1234=456.678, 3456=456.789, >>>I need the 456.678 and 456.789 >>>so the code searches 1234= >>>I tell the code (input box) get the next 9 characters >>>I have tried your suggestion, and it did not work so I will show the >>>whole code >>>Private Sub Command1_Click() >>> 'Dim arrData(5, 1 To 1) >>> Dim arrData() >>> Dim TextSize As Integer >>> Dim Buffer As String >>> Dim NewPosition As Integer >>> Dim ArrayPosition As Integer >>> Dim CharItem As String >>> Dim LastPosition As Integer >>> Dim ArrayItem As Integer >>> Dim ArraySize As Integer >>> ' Initialization >>> ArrayItem = 0 ' initialized here >>> NewPosition = 1 >>> LastPosition = 1 >>> ArrayPosition = 1 >>> TextSize = 0 >>> Buffer = "" >>> CharItem = "" >>> ' Start Procedure >>> ' Get text size from file >>> TextSize = Len(RichTextBox2.Text) >>> 'Start Loop >>> For ArrayPosition = 1 To TextSize >>> ArrayPosition = LastPosition >>>' Start inner loop >>> For NewPosition = 1 To TextSize >>> NewPosition = LastPosition >>> ' check each character out if comma break out >>> CharItem = Mid(RichTextBox2.Text, NewPosition, 1) >>> If CharItem = "," Then >>> LastPosition = NewPosition + 1 >>> GoTo 1 >>> End If >>> Buffer = Buffer + CharItem >>> LastPosition = NewPosition + 1 >>> Next NewPosition >>>1 >>>'************the buffer has correct message so far >>> ArraySize = ArrayItem >>>'************** get error message subscript out of range I believe >on >>>' *************eithr this line or next >>> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >>> arrData(ArrayItem, 1) = Buffer ' Set the labels in the first >series. >>> Buffer = "" >>> CharItem = "" >>> ArrayItem = ArrayItem + 1 >>> Next ArrayPosition >>> MSChart1.ChartData = arrData >>> MSChart1.Visible = True >>>End Sub >>>On Fri, 04 Jun 1999 23:40:09 -0400, Jeff Ashley
>>>>When you redim a dimension of an array to 1 (and you are using the >>>>default Option Base 0), the 1 item you have dim'ed is item ZERO. >With >>>>your variables, ubound(arrData) is always ArraySize - 1, yet you >try >>>to >>>>ref member ArrayItem which = ArraySize. You are out of bounds! >>>>Try this: >>>> ArrayItem = 0 ' initialization >>>>in your loop: >>>> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >>>> arrData(ArrayItem, 1) = Buffer >>>>I won't even ask why you are using a second dimension with a single >>>valid >>>>value . . .
>>>>> My code below causes an error during run time. the VB >Interpreter >>>>> says my Array has SUBSCRIPT OUT OF RANGE error message >>>>> According to documentation this shouldn't be happening. >>>>> Any help will be appreciated >>>>> Thanks in advance >>>>> Vin >>>>> Private Sub Command1_Click() >>>>> 'Dim arrData(5, 1 To 1) ' This Array works when done this way >>>>> Dim arrData() ' trying to do an array on the fly but doesn't >>>work >>>>> Dim ArrayPosition As Integer >>>>> Dim ArrayItem As Integer >>>>> Dim ArraySize As Integer >>>>> ' Initialization >>>>> ArrayItem = 1 >>>>> Buffer = "" >>>>> CharItem = "" >>>>> ' Start Procedure >>>>> ' Code Below >>>>> ' Now getting to the code for array resizing now >>>>> ' ArrayItem is given a value in the code >>>>> ArraySize = ArrayItem >>>>> ReDim Preserve arrData(ArraySize, 1 To 1) >>>>> arrData(ArrayItem, 1) = Buffer >>>>> Buffer = "" >>>>> CharItem = "" >>>>> ArrayItem = ArrayItem + 1 >>>>> Next ArrayPosition >>>>> ' Put array into a chart >>>>> MSChart1.ChartData = arrData >>>>> MSChart1.Visible = True >>>>> End Sub >>>>> ************************************* >>>>> *PC Technical Support and products * >>>>> *http://www.helpfixmypc.com * >>>>> ************************************* >>>************************************* >>>*PC Technical Support and products * >>>*http://www.helpfixmypc.com * >>>************************************* >************************************* >*PC Technical Support and products * >*http://www.helpfixmypc.com * >*************************************
|
Thu, 22 Nov 2001 03:00:00 GMT |
|
 |
Vin Catrambo #7 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
You are quite right on lack of clarity (using VB6 I believe it is a new control), The data I'm looking at is as follows; [Entry 0] 16201=3051.8800 41211=34.8000 [Entry 1] 16201=3051.8800 41211=34.8000 [Entry 2] 16201=3073.0850 41211=24.0900 etc. So my code by user input will find "16201=" and by user input get the next 6 characters and put's the data into RichTextbox2 for the whole original file; The richtextbox2.text will look like this 3051.8,3051.8,3073.0, The code as shown below reads the richtextbox2 and charts the data. Well when I tried to set up the array dynamically using the orginal snippet of code it does not work. I did make it work using a FOR loop, and using a new approach of counting the commas and ReDim'ng the array to that count bot ways work. But, being self-taught in VB I want to KNOW WHY, it does not work. By the way I came up with the counting commas Ideas yesterday. Here is the new code below if intrested. Regards Vin Private Sub Command1_Click() Dim arrData() Dim TextSize As Integer Dim Buffer As String Dim NewPosition As Integer Dim ArrayPosition As Integer Dim CharItem As String Dim LastPosition As Integer Dim ArrayItem As Integer Dim Upper As Integer Dim SomeNo As Integer ' Initialization ArrayItem = 0 ' initialized here NewPosition = 1 LastPosition = 1 ArrayPosition = 1 TextSize = 0 Buffer = "" CharItem = "" ' Start Procedure TextSize = Len(RichTextBox2.Text) For NewPosition = 1 To TextSize ' starts counting commas CharItem = Mid(RichTextBox2.Text, NewPosition, 1) If CharItem = "," Then ArrayItem = ArrayItem + 1 End If Next NewPosition ' end counting commas and reset variables again to ' check textbox NewPosition = 1 CharItem = "" ' checking whole file for data For ArrayPosition = 1 To TextSize ArrayPosition = LastPosition ' checks data and puts into buffer until comma pops up For NewPosition = 1 To TextSize NewPosition = LastPosition CharItem = Mid(RichTextBox2.Text, NewPosition, 1) If CharItem = "," Then LastPosition = NewPosition + 1 GoTo 1 End If Buffer = Buffer + CharItem LastPosition = NewPosition + 1 Next NewPosition ' out of inner loop, now will add data to array 1 ' ArrayItem is the comma data aquired ReDim Preserve arrData(ArrayItem, 1 To 1) ' needed to use SomeNo to initialized newly created array back to ' 0 position and then increment to the next open space. ' I think this may have been the source of my original problem. arrData(SomeNo, 1) = Buffer SomeNo = SomeNo + 1 Buffer = "" CharItem = "" Next ArrayPosition MSChart1.ChartData = arrData MSChart1.Visible = True End Sub On Sun, 6 Jun 1999 11:04:03 +0100, "Peter Beach" Quote:
>Vin, >I'm still not quite clear on where you are having a problem. Do you mean >that you have some CSV data of the following type: >Series 1, 1234, 2222, 3245 >Series 2, 4444, 3333, 2222 >Series 3, 1111, 2222, 3333 >and you want to construct a graph from this data? >If this is right you have two problems - the first is parsing the CSV text, >the second is getting it into the chart. Neither of these is particularly >difficult, but I don't want to waste bandwidth describing things you already >know, or indeed answering the wrong question. Perhaps you could get back to >me, and I'll see what I can put together. >BTW your question had me look at the help text for the ChartData property, >and in particular the example given (VB5). I think it breaks new records >for overall lack of clarity and typographical and logic errors per line! >FWIW the following code constructed a small histogram of 6 bars: >Option Explicit >Private Sub Command1_Click() > Dim V(5, 1) As Variant > V(0, 0) = "Things" > V(1, 0) = "Books" > V(2, 0) = "Clothes" > V(3, 0) = "Phones" > V(4, 0) = "Cars" > V(5, 0) = "Toys" > V(0, 1) = 25 > V(1, 1) = 12 > V(2, 1) = 13 > V(3, 1) = 14 > V(4, 1) = 15 > V(5, 1) = 16 > cht.ChartData = V >End Sub >Look forward to hearing from you. >Best wishes, >Peter
Quote: >>Understood I found this snippet of code to guide me on the first use >>of MSCHART control >>Private Sub Command1_Click() >> Dim arrData(4, 1 To 1) >> arrData(1, 1) = 1 ' Set the labels in the first series. >> arrData(2, 1) = 2 >> arrData(3, 1) = 3 >> arrData(0, 1) = 8 >> MSChart1.ChartData = arrData >>End Sub >>Because of this I was trying to manipulate this simple code to an >>application where I will graph scientific Data from a chemical >>analyzer with comma delimeted data. >>As you can see from the example above (tested and works) I need to do >>the above from the text from my RichTextBox2. >>Where >>Private Sub Command1_Click() >> Dim arrData(4, 1 To 1) >> arrData(ArayItem, 1) = Buffer ' Set labels in the first series. >> arrData(ArayItem, 1) = Buffer ' which I want to do dynamically >> arrData(ArayItem, 1) = Buffer ' which I want to do dynamically >> arrData(ArayItem, 1) = Buffer ' which I want to do dynamically >> MSChart1.ChartData = arrData >>End Sub >>Any suggextion on how to do above dynamically??? >>Thanks In Advance >>Vin >>On Sat, 5 Jun 1999 15:01:52 +0100, "Peter Beach"
>>>Vin, >>>If using Redim Preserve I believe you can only redim the "last" >>dimension. >>>Thus you can go: >>> Redim Preserve A(1,1) >>> . . . >>> Redim Preserve A(1,2) >>>but not: >>> Redim Preserve A(1,1) >>> . . . >>> Redim Preserve A(2,1) >>>HTH >>>Peter
>>>>Thanks for the reply. The basic reason for this code is so that I >>can >>>>extract data after certain sequence of characters etc. >>>>Exxample >>>>1234=456.678, 3456=456.789, >>>>I need the 456.678 and 456.789 >>>>so the code searches 1234= >>>>I tell the code (input box) get the next 9 characters >>>>I have tried your suggestion, and it did not work so I will show the >>>>whole code >>>>Private Sub Command1_Click() >>>> 'Dim arrData(5, 1 To 1) >>>> Dim arrData() >>>> Dim TextSize As Integer >>>> Dim Buffer As String >>>> Dim NewPosition As Integer >>>> Dim ArrayPosition As Integer >>>> Dim CharItem As String >>>> Dim LastPosition As Integer >>>> Dim ArrayItem As Integer >>>> Dim ArraySize As Integer >>>> ' Initialization >>>> ArrayItem = 0 ' initialized here >>>> NewPosition = 1 >>>> LastPosition = 1 >>>> ArrayPosition = 1 >>>> TextSize = 0 >>>> Buffer = "" >>>> CharItem = "" >>>> ' Start Procedure >>>> ' Get text size from file >>>> TextSize = Len(RichTextBox2.Text) >>>> 'Start Loop >>>> For ArrayPosition = 1 To TextSize >>>> ArrayPosition = LastPosition >>>>' Start inner loop >>>> For NewPosition = 1 To TextSize >>>> NewPosition = LastPosition >>>> ' check each character out if comma break out >>>> CharItem = Mid(RichTextBox2.Text, NewPosition, 1) >>>> If CharItem = "," Then >>>> LastPosition = NewPosition + 1 >>>> GoTo 1 >>>> End If >>>> Buffer = Buffer + CharItem >>>> LastPosition = NewPosition + 1 >>>> Next NewPosition >>>>1 >>>>'************the buffer has correct message so far >>>> ArraySize = ArrayItem >>>>'************** get error message subscript out of range I believe >>on >>>>' *************eithr this line or next >>>> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >>>> arrData(ArrayItem, 1) = Buffer ' Set the labels in the first >>series. >>>> Buffer = "" >>>> CharItem = "" >>>> ArrayItem = ArrayItem + 1 >>>> Next ArrayPosition >>>> MSChart1.ChartData = arrData >>>> MSChart1.Visible = True >>>>End Sub >>>>On Fri, 04 Jun 1999 23:40:09 -0400, Jeff Ashley
>>>>>When you redim a dimension of an array to 1 (and you are using the >>>>>default Option Base 0), the 1 item you have dim'ed is item ZERO. >>With >>>>>your variables, ubound(arrData) is always ArraySize - 1, yet you >>try >>>>to >>>>>ref member ArrayItem which = ArraySize. You are out of bounds! >>>>>Try this: >>>>> ArrayItem = 0 ' initialization >>>>>in your loop: >>>>> ReDim Preserve arrData(ArrayItem + 1, 1 To 1) >>>>> arrData(ArrayItem, 1) = Buffer >>>>>I won't even ask why you are using a second dimension with a single >>>>valid >>>>>value . . .
>>>>>> My code below causes an error during run time. the VB >>Interpreter >>>>>> says my Array has SUBSCRIPT OUT OF RANGE error message >>>>>> According to documentation this shouldn't be happening. >>>>>> Any help will be appreciated >>>>>> Thanks in advance >>>>>> Vin >>>>>> Private Sub Command1_Click() >>>>>> 'Dim arrData(5, 1 To 1) ' This Array works when done this way >>>>>> Dim arrData() ' trying to do an array on the fly but doesn't >>>>work >>>>>> Dim ArrayPosition As Integer >>>>>> Dim ArrayItem As Integer >>>>>> Dim ArraySize As Integer >>>>>> ' Initialization >>>>>> ArrayItem = 1 >>>>>> Buffer = "" >>>>>> CharItem = "" >>>>>> ' Start Procedure >>>>>> ' Code Below >>>>>> ' Now getting to the code for array resizing now >>>>>> ' ArrayItem is given a value in the code >>>>>> ArraySize = ArrayItem >>>>>> ReDim Preserve arrData(ArraySize, 1 To 1)
... read more »
|
Thu, 22 Nov 2001 03:00:00 GMT |
|
 |
Peter Beac #8 / 8
|
 Arrays Problem SUBSCRIPT OUT OF RANGE
Vin, If I understand correctly you have (for example) 3 values you wish to plot on a chart which are held in a text box as "3051.8,3051.8,3073.0," Now the contents of the text box are simply a string of characters and the chart control doesn't handle strings of characters (it could do but as far as I know it doesn't). There are several ways of adding data to charts. In this case I think the easiest would be to use the data method of the control. Something like (untested code I'm afraid): Cht1.ColumnCount = 1 Cht1.RowCount = 3 ' You could count the number of commas to get this generally Cht1.Column = 1 Tempstr$ = RichText2.Text for i% = 1 to 3 Cht1.Row = i% nPos% = instr(Tempstr$,",") Cht1.Data = val(left$(Tempstr$,nPos%-1)) if i% < 3 then Tempstr$ = mid$(Tempstr$,nPos%+1) next i% Basically it's your job to pump the data into the chart control. Although there is the ChartData property available, this is only useful if you already have the data in an appropriate state. Although there is probably some small performance hit in doing the way given above, it would appear to be easier than trying to dynamically redimension arrays. As to the *why* question, the ChartData property expects a variant array in a specific format. A rich text box isn't a variant array and so that is rejected immediately. The format of the variant array expected by ChartData appears to be something like: 1 2 3 4 5 Series1 123 234 345 456 567 Series2 234 321 111 222 333 Series3 111 222 333 444 555 where the outer row/column gives the series description and the rest of the array give the data points. The outline given above will plot 5 points for 3 series. Thus you would need to dimension the array as something like: Redim chtarr(2,4) as variant chtarr(0,0) = "Series 1" chtarr(1,0) = "Series 2" chararr(0,1) = 123 etc. HTH Peter Quote:
>You are quite right on lack of clarity (using VB6 I believe it is a >new control), The data I'm looking at is as follows; >[Entry 0] >16201=3051.8800 >41211=34.8000 >[Entry 1] >16201=3051.8800 >41211=34.8000 >[Entry 2] >16201=3073.0850 >41211=24.0900 >etc. >So my code by user input will find "16201=" and by user input get the >next 6 characters and put's the data into RichTextbox2 for the whole >original file; >The richtextbox2.text will look like this >3051.8,3051.8,3073.0, >The code as shown below reads the richtextbox2 and charts the data. >Well when I tried to set up the array dynamically using the orginal >snippet of code it does not work. >I did make it work using a FOR loop, and using a new approach of >counting the commas and ReDim'ng the array to that count bot ways >work. But, being self-taught in VB I want to KNOW WHY, it does not >work. >By the way I came up with the counting commas Ideas yesterday. Here >is the new code below if intrested. >Regards >Vin >Private Sub Command1_Click() > Dim arrData() > Dim TextSize As Integer > Dim Buffer As String > Dim NewPosition As Integer > Dim ArrayPosition As Integer > Dim CharItem As String > Dim LastPosition As Integer > Dim ArrayItem As Integer > Dim Upper As Integer >Dim SomeNo As Integer > ' Initialization > ArrayItem = 0 ' initialized here > NewPosition = 1 > LastPosition = 1 > ArrayPosition = 1 > TextSize = 0 > Buffer = "" > CharItem = "" > ' Start Procedure > TextSize = Len(RichTextBox2.Text) > For NewPosition = 1 To TextSize > ' starts counting commas > CharItem = Mid(RichTextBox2.Text, NewPosition, 1) > If CharItem = "," Then > ArrayItem = ArrayItem + 1 > End If > Next NewPosition >' end counting commas and reset variables again to >' check textbox > NewPosition = 1 > CharItem = "" >' checking whole file for data > For ArrayPosition = 1 To TextSize > ArrayPosition = LastPosition >' checks data and puts into buffer until comma pops up > For NewPosition = 1 To TextSize > NewPosition = LastPosition > CharItem = Mid(RichTextBox2.Text, NewPosition, 1) > If CharItem = "," Then > LastPosition = NewPosition + 1 > GoTo 1 > End If > Buffer = Buffer + CharItem > LastPosition = NewPosition + 1 > Next NewPosition >' out of inner loop, now will add data to array >1 >' ArrayItem is the comma data aquired > ReDim Preserve arrData(ArrayItem, 1 To 1) >' needed to use SomeNo to initialized newly created array back to >' 0 position and then increment to the next open space. >' I think this may have been the source of my original problem. > arrData(SomeNo, 1) = Buffer > SomeNo = SomeNo + 1 > Buffer = "" > CharItem = "" > Next ArrayPosition > MSChart1.ChartData = arrData > MSChart1.Visible = True >End Sub
|
Thu, 22 Nov 2001 03:00:00 GMT |
|
|
|