Using arrays to manage and sort data

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-05-01

I am writing a program that will take a series of data inputs from a .txt file, put the data into an array (or arrays), and then be able to sort it.

The info in the text file looks like this:

0022, "D", 000155.04, "23SEP95"
0001, "D", 000176.97, "16JAN02"
0002, "C", 000023.96, "24JAN99

the first column of numbers are "customer numbers" - they are what needs to be sorted into numerical order.

What I can't figure out, is if I can put the data into one array, or put all the customer numbers into an array, and then put the second column of data into a second array, and so on.  

...Or do I make an array for the first line of data, and a second array for the next line, and so on?

A portion of the code that I have so far:

    Open App.Path & "\cs261as04sp03T" For Input As #1
    'determine amt of data
    numcustomers = 0
    Do While Not EOF(1)
        Input #1, custnumber
        numcustomers = numcustomers + 1
    Close #1
    'create arrays for the data
    Dim custnumber(1 To numcustomers) As String
    Dim CorD(1 To numcustomers) As String
    Dim custbalance(1 To numcustomers) As curency
    Dim chardate(1 To numcustomers) As String
    Open App.Path & "\cs261as04sp03T" For Input As #1
    For k = 1 To numcustomers
        Input #1, custnumber(k), CorD(k), custbalance(k), chardate(k)
    Next k
    Close #1

However, I get a compiler error "Constant expression required."

Any help is much appreciated,
Question by:JustinBGSU
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Accepted Solution

Brendt Hess earned 100 total points
ID: 8220946
There are many options here, but what I would do is to make an array of User Defined Types:

Type CustRec
   CustNumber As String * 4
   CorD as String * 1
   CustBalance As Currency
   CharDate As String * 7
End Type

Private Records() As CustRec

Private sRec As String
Private sWork() As Strinig

Then, using your code as a base, change to use Line Input, and ReDim dynamically:

   Redim Records(0)
   Open App.Path & "\cs261as04sp03T" For Input As #1

   Line Input #1, sRec      
   Do While Not EOF(1)
      NumCustomers = NumCustomers + 1
      sWork = Split(sRec,",")
      Redim Preserve Records(0 to NumCustomers)
      With Records(NumCustomers)
         .CustNumber = sWork(0)
         .CorD = sWork(1)
         .CustBalance = CCur(sWork(2))
         .CharDate = sWork(3)
      End With
      Line Input #1, sRec      
   Close #1

Then, to sort, use the LSet command:
Dim I as Integer, J as Integer, Ptr as integer

For I = 1 to UBound(Records) - 1
   Ptr = I
   For J = I + 1 To UBound(Records)
       If Records(J).CustNumber < Record(Ptr).CustNumber Then Ptr = J
   Next J
   If I <> Ptr Then
      LSet Records(0) = Records(I)
      LSet Records(I) = Records(Ptr)
      LSet Records(Ptr) = Records(0)
   End If
Next I

Now, the array is sorted from smallest to greatest.

Assisted Solution

Jacamar earned 100 total points
ID: 8221182
ok....use a 2 D array.....since there are only 4 things, this should be easy to do

dim array1() as String

Do while not EOF(1)
inI = inI + 1
Redim Preserve array1(1 to 4, 1 to inI) as string
Input #1, array1(1, inI)
Input #1, array1(2, inI)
Input #1, array1(3, inI)
Input #1, array1(4, inI)

Now, the data is in.  

Next conver the first number to a value.

For i = 1 to inI
array1(1, i) = cint(array1(1,i))
Next i

Now sort.  My next post will have the sort in it.

Expert Comment

ID: 8221206
This is an extrememly fast sort.  Implement as you wish.

Private Sub cmdSort_Click()
Dim i As Integer
Dim BoolSwap As Boolean
Dim inNumVals As Integer
Dim inI() As Integer
inNumVals = 10
ReDim inI(1 To inNumVals) As Integer
For ii = 1 To inNumVals - 1
   BoolSwap = False
   For i = 1 To inNumVals - ii
   If array1(i) > array1(i + 1) Then
       Call Swap(inI(), i)
       BoolSwap = True
   End If
   Next i
   If BoolSwap = False Then ii = inNumVals - 1
Next ii
For i = 1 To inNumVals
Picture1.Print inI(i)
Next i
End Sub

Private Sub Swap(inI() As Integer, i As Integer)
Dim inTemp As Integer
inTemp = inI(i)
inI(i) = inI(i + 1)
inI(i + 1) = inTemp
End Sub

Expert Comment

ID: 8531474
Hi JustinBGSU,
This old question (QID 20565667) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.

Expert Comment

ID: 9440903
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->Split between bhess1 and Jacamar

Please leave any comments here within the next seven days.


Cleanup Volunteer

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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