[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Redim Preserve Dynamic Array with User Defined Type

Posted on 2004-11-26
10
Medium Priority
?
389 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:Kev
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 12679927
Hi there

try changing this line

Dim EmployeeData(0) As EmpData

to

Dim EmployeeData() As EmpData

HTH

Scott
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 12679963
Hi

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

Cheers

Scott
0
 
LVL 12

Expert Comment

by:jkaios
ID: 12680080
>> "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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:jkaios
ID: 12680085
Private Sub IncreaseArray()
      ReDim Preserve EmployeeData(UBound(EmployeeData) + 1)
End Sub
0
 
LVL 12

Expert Comment

by:jkaios
ID: 12680150
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
 
LVL 5

Author Comment

by:Kev
ID: 12680187
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
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 2000 total points
ID: 12680200
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
 
LVL 5

Author Comment

by:Kev
ID: 12680311
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
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12680999
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
 
LVL 5

Author Comment

by:Kev
ID: 12685034
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question