Redim Preserve Dynamic Array with User Defined Type

Hi, I need help with dynamically resizing an array, i receive error messages "array already dimensioned", code as follows:

Public Type EmpData
   LastName As String
   FirstName As String
   Date_Start As String
   HourlyPay As Double
   Address As String
   Suburb As String
   Rating As Integer
   Current As String
End Type

Dim EmployeeData(0) As EmpData

Private Sub LoadEmplyeeData()
   'Open sales file, Initialise array and adds data to combobox
   i = 0
   Open EmployeesFile For Input As #2
   
   Do While Not EOF(2)
      With EmployeeData(i)
         Input #2, .LastName, .FirstName, .Date_Start, .HourlyPay, .Address, .Suburb, .Rating, .Current
         List2.AddItem .LastName & " " & .FirstName & " " & .Date_Start & " " & .HourlyPay & " " & .Address & " " & .Suburb & " " & .Rating & " " & .Current
      i = i + 1
      End With
         Call IncreaseArray
   Loop
End Sub

Private Sub IncreaseArray()
      ReDim Preserve EmployeeData(UBound(EmployeeData) + 1) As EmpData
End Sub

There is obvously alot more code to this project, the entire project is avail if anyone is interested in helping.

Kev
LVL 5
KevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ColosseoCommented:
Hi there

try changing this line

Dim EmployeeData(0) As EmpData

to

Dim EmployeeData() As EmpData

HTH

Scott
0
ColosseoCommented:
Hi

When you declare a dynamic array the parenthesis have to be empty thats why your redim statement was failing

Cheers

Scott
0
jkaiosIT DirectorCommented:
>> "array already dimensioned"

This is an excerpt from the VB online CHM help file.

--- You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array. However, you can't declare an array of one data type and later use ReDim to change the array to another data type, unless the array is contained in a Variant. If the array is contained in a Variant, the type of the elements can be changed using the "As" type clause, unless you’re using the Preserve keyword, in which case, no changes of data type are permitted. ---

So in your IncreaseArray() sub routine, try removing the "As EmpData"

Hope this will resolve the problem.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jkaiosIT DirectorCommented:
Private Sub IncreaseArray()
      ReDim Preserve EmployeeData(UBound(EmployeeData) + 1)
End Sub
0
jkaiosIT DirectorCommented:
budorat,

Pls disregard my above comments -- I agree with Colosseo.

Sorry, I didn't look very carefully at the first variable array declaration -- Dim EmployeeData(0)
A static array can only be dimensioned once, only dynamic arrays can be redimensioned.
So, in your case you'll have to either remove the redimensioning or use a dynamic array as with Colosseo's comment

Again, give the credits to Colosseo.
0
KevAuthor Commented:
Alrighty, I originall had Dim EmployeeData() As EmpData instead of Dim EmployeeData(0) As EmpData, but when I use the first option I get the error "subscript out of range" which then points to  

Private Sub LoadEmplyeeData()
.
With EmployeeData(i)

I have now made the following amendments.....

Dim EmployeeData() As EmpData

Private Sub LoadEmplyeeData()
   ReDim EmployeeData(0)
   'Open sales file, Initialise array and adds data to combobox
   i = 0
   Open EmployeesFile For Input As #2
   
   Do While Not EOF(2)
      With EmployeeData(i)
         Input #2, .LastName, .FirstName, .Date_Start, .HourlyPay, .Address, .Suburb, .Rating, .Current
         List2.AddItem .LastName & " " & .FirstName & " " & .Date_Start & " " & .HourlyPay & " " & .Address & " " & .Suburb & " " & .Rating & " " & .Current
      i = i + 1
      End With
         Call IncreaseArray
   Loop

End Sub

Private Sub IncreaseArray()
      ReDim Preserve EmployeeData(UBound(EmployeeData) + 1)
End Sub

This appears to be working, but is it technically correct or is there a more efficient way to do it? BTW I am completing a uni assignment whcih explains why I am aiming for the most correct solution.

Cheers

Kev
0
Shiju SasidharanAssoc Project ManagerCommented:
Hi
instead of calling IncreaseArray use that variable i to redim the array
try this
' data will be stored within the index values
' 1 to Ubound(EmployeeData)
'------------------------------------------------------------------------------------
Public Type EmpData
   LastName As String
   FirstName As String
   Date_Start As String
   HourlyPay As Double
   Address As String
   Suburb As String
   Rating As Integer
   Current As String
End Type

Dim EmployeeData() As EmpData

Private Sub LoadEmplyeeData()
   'Open sales file, Initialise array and adds data to combobox
   i = 1
   Open EmployeesFile For Input As #2
   
   Do While Not EOF(2)
      Redim Preserve EmployeeData(i)
      With EmployeeData(i)
         Input #2, .LastName, .FirstName, .Date_Start, .HourlyPay, .Address, .Suburb, .Rating, .Current
         List2.AddItem .LastName & " " & .FirstName & " " & .Date_Start & " " & .HourlyPay & " " & .Address & " " & .Suburb & " " & .Rating & " " & .Current
      i = i + 1
      End With
  Loop
End Sub
'-------------------------------------------------------

;-)
Shiju
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KevAuthor Commented:
Thanks for the shiju, one last question.... I am having problems declaring the following

Const Today As Date = Now

Basically I wasnt to set constant Today as todays date. ATM I am having to resort to

Const Today As Date = #11/24/2004#.
0
Shiju SasidharanAssoc Project ManagerCommented:
hi budorat
 thank u for the points
u can assign Literal, other constant, or any combination that includes all arithmetic or logical operators except "Is"  to a Constant variable

 in my belief it is not possible to assign a variable or a function value to a Const Variable.

 my suggestion is to store current date in a Global variable and try Not to Alter it

;-)
Shiju

 
0
KevAuthor Commented:
Hi, FYI I decided on the following solution

Dim Today as Date
Today = Now
txtDateCreated = Format$(Today, "DD/MM/YYYY")

Thanks for your assistance, greatly appreciated.

Budorat
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.