473,409 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Array varying length Run-time error 9

121 100+
Hi

I have a field that stores a recurrence pattern that is imported from outlook

example recurrence pattern
(3,20101208,10:00:00,1,0,4,120,2,20110331,12:00:00 ,)

The pattern is not always the same length so it could have up to 13 elements.

I need to split this field up and assign the different elements to variables I have done this using an array

I am getting Run-time error 9 because the array is not always the same length

using the above example I get the error on the following line

Expand|Select|Wrap|Line Numbers
  1. MonthOfYear = aStringArray(11)
  2.  
Here is the code I am using

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim aStringArray() As String 'array of strings
  3.  
  4.  
  5.  
  6.         RecPattern = RstCheckRec!RecPattern ' this the outlook recurrence pattern stored in the table I need to seperate these elements
  7.         aStringArray = Split(RecPattern, ",") 'split string up
  8.  
  9.  
  10.  
  11.        'assigns the variables with the values from the the recurrence pattern stored in the table
  12.  
  13.         RecType = aStringArray(0)
  14.         StartDate = aStringArray(1)
  15.         StartTime = aStringArray(2)
  16.         Interval = aStringArray(3)
  17.         NoEndDate = aStringArray(4)
  18.         Occurrences = aStringArray(5)
  19.         Duration = aStringArray(6)
  20.         Instance = aStringArray(7)
  21.         EndDate = aStringArray(8)
  22.         EndTime = aStringArray(9)
  23.         DayOfMonth = aStringArray(10)
  24.         MonthOfYear = aStringArray(11)
  25.         Regenerate = aStringArray(12)
  26.         DayOfWeekMask = aStringArray(13)
  27.  
Many Thanks

Phill
Jul 13 '10 #1
4 2793
jimatqsi
1,271 Expert 1GB
Let's see what is in your array after the split:

Expand|Select|Wrap|Line Numbers
  1. dim intCount, intMax as integer.
  2. dim strMessage as string
  3.  
  4. intMax = Ubound(aStringArray)
  5. strMessage = "The array contains: " & vbCRLF
  6.  
  7. for intCount=0 to intMax
  8. strMessage = strMessage & intCount & " " &  aStringArray(intCount) & vbCRLF
  9. next
  10.  
  11. msgbox strMessage  ' will show the contents of your array
  12.  
Jim
Jul 13 '10 #2
ADezii
8,834 Expert 8TB
@phill86
As I see it, the key is to:
  1. Populate a variant Array (varStringArray) with all the Elements (Comma Delimited) in the String Variable recPattern.
  2. Calculate the Number of Elements that are 'Missing' up to a MAXIMUM of 14, not 13.
  3. Re-Dimension the Array, while maintaining existing Values, and fill the Missing Elements with the String 'Missing'. You can use whatever String you wish.
  4. The following Code Demo assumes recPattern has 10 Delimited Values in the String. Results are also posted along with the Code.
    Expand|Select|Wrap|Line Numbers
    1. Dim varStringArray As Variant
    2. Dim recPattern As String
    3. Dim intNumOfElements As Integer
    4. Dim bytMissingNumOfElements As Byte
    5. Dim bytCounter As Byte
    6. Const conMAX_NUM_OF_ELEMENTS As Byte = 14     'NOT 13
    7.  
    8. '12 Test Elements
    9. recPattern = "3,20101208,10:00:00,1,0,4,120,2,20110331,12:00:00"
    10.  
    11. varStringArray = Split(recPattern, ",") 'split string up
    12.  
    13. 'Determine the Number of Elements in the Variant Array
    14. intNumOfElements = UBound(varStringArray) + 1
    15.  
    16. If intNumOfElements = 0 Or intNumOfElements > 14 Then Exit Sub
    17.  
    18. 'How many Elements of the Array are 'Missing'?
    19. bytMissingNumOfElements = conMAX_NUM_OF_ELEMENTS - intNumOfElements
    20.  
    21. 'Fill the Missing Elements of the Array with a String. Do this 
    22. 'by Redimensioning the Array while at the same time maintaining 
    23. 'its contents
    24. For bytCounter = intNumOfElements To conMAX_NUM_OF_ELEMENTS - 1
    25.   ReDim Preserve varStringArray(UBound(varStringArray) + 1)
    26.   varStringArray(UBound(varStringArray)) = "Missing"
    27. Next
    28.  
    29. 'Assigns the Variables with the values from the the recurrence pattern stored in
    30. 'the Table, noting any Elements that are Missing
    31. RecType = varStringArray(0)
    32.   Debug.Print RecType
    33. StartDate = varStringArray(1)
    34.   Debug.Print StartDate
    35. StartTime = varStringArray(2)
    36.   Debug.Print StartTime
    37. Interval = varStringArray(3)
    38.   Debug.Print Interval
    39. NoEndDate = varStringArray(4)
    40.   Debug.Print NoEndDate
    41. Occurrences = varStringArray(5)
    42.   Debug.Print Occurrences
    43. Duration = varStringArray(6)
    44.   Debug.Print Duration
    45. Instance = varStringArray(7)
    46.   Debug.Print Instance
    47. EndDate = varStringArray(8)
    48.   Debug.Print EndDate
    49. EndTime = varStringArray(9)
    50.   Debug.Print EndTime
    51. DayOfMonth = varStringArray(10)
    52.   Debug.Print DayOfMonth
    53. MonthOfYear = varStringArray(11)
    54.   Debug.Print MonthOfYear
    55. Regenerate = varStringArray(12)
    56.   Debug.Print Regenerate
    57. DayOfWeekMask = varStringArray(13)
    58.   Debug.Print DayOfWeekMask
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. 3
  2. 20101208
  3. 10:00:00
  4. 1
  5.  
  6. 4
  7. 120
  8. 2
  9. 20110331
  10. 12:00:00
  11. Missing
  12. Missing
  13. Missing
  14. Missing
Jul 13 '10 #3
phill86
121 100+
Hi

Thanks for the reply the solution from ADezii worked many thanks

Phill
Jul 13 '10 #4
ADezii
8,834 Expert 8TB
Line # 16 should be:
Expand|Select|Wrap|Line Numbers
  1. If intNumOfElements = 0 Or intNumOfElements > conMAX_NUM_OF_ELEMENTS Then Exit Sub
Jul 13 '10 #5

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

Similar topics

8
by: M. Clift | last post by:
Hi All, I have a list of varying length. Would someone know the way to get the last two values for this? I can see how this is done with a list that I know the length of, but not one thats...
4
by: Fabian | last post by:
I have a three tier nested array, used to define a map for a javascript game, and can be edited within the web page. Is there a way I can generate a visible copy of this array that I can then c&p...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
6
by: Denis C | last post by:
Hi I have a byte array of length 2404. I want to access subsets of the array and pass the subset to the constructor of a class. i.e. Dim bData(2403) As Byte Dim aThing As SomeClass = new...
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
1
by: The Man with no name | last post by:
Hi, Can we create an array of user defined dimensions in the following way:? 1 Create a class( say CONSTANT) whose lone datamember is an integer which is called say num. 2 In the public section...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.