Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2003
    Brampton, Ontario, Canada

    Unanswered: Arrays confuse me. Please Help

    I am having some trouble understanding arrays and how to Sort them.
    The following code bellow looks for txt files in a specific directory.
    The problem is that the 'DIR' function grabs the filenames randomly
    as explained in the HELP file as Follows;
    "Tip Because file names are retrieved in no particular
    order, you may want to store returned file names in an array,
    and then sort the array."
    First, How would I define and populate an array that would store
    the .txt files from a specific Directory.
    The number of txt files in the directory will vary.
    Second, How would I Sort The Array So that the files are are in the same
    order as the Application.FileSearch.FoundFiles.Count

    The code bellow is just a snippet of code from the Help file that I modified
    But as you can tell I'm beginning to lose the understanding of what is going
    Please, any help is most appreciated.

    With Application.FileSearch
    .LookIn = sFilePath(j)
    .FileName = "*.txt"
    If .Execute() > 0 Then
    MyFile = Dir(sFilePath(j))
    For I = 1 To .FoundFiles.Count
    MyFile = Left(MyFile, Len(MyFile) - 4) 'Removes the .txt
    Application.Loadfromtext Forms, MyFile, .FoundFiles(I)
    MyFile = Dir 'Get the next file in the same directory if there
    is more than one.
    Next I
    End With

  2. #2
    Join Date
    Apr 2004
    outside the rim
    Sorting items in an array is a common "school project" because there aren't any "automatic functions" that will sort an array for you.

    Creating an array is as simple as adding the "dimensions" to the DIM statement. For example:

    Dim strName(1 to 100) as String

    You mentioned that the number of files will vary - you can mess around with dynamic arrays, but that's more complicated. I would think you'd be safe with an array of several hundred. Use an integer to keep track of how many files you end up with.

    Dim strFile(1 to 400) as String
    Dim strTemp as String
    Dim intC as Integer
    Dim strPath as String
    strPath = "C:\My Documents\*.txt"
    intC = 0
    if strTemp <> "" Then
    intC = intC + 1
    strFile(intC) = strTemp
    End If
    strTemp = Dir
    Loop Until strTemp = ""

    This puts the files in an array, with intC telling you how many you have. As for sorting, I like to use unbound recordsets, but Access doesn;t let you create such a thing. Perhaps another user has the code for a "bubble-sort" handy to paste in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts