Link to home
Start Free TrialLog in
Avatar of Kev
KevFlag for Australia

asked on

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
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi there

try changing this line

Dim EmployeeData(0) As EmpData

to

Dim EmployeeData() As EmpData

HTH

Scott
Hi

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

Cheers

Scott
>> "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.
Private Sub IncreaseArray()
      ReDim Preserve EmployeeData(UBound(EmployeeData) + 1)
End Sub
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.
Avatar of Kev

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Shiju S
Shiju S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kev

ASKER

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#.
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

 
Avatar of Kev

ASKER

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