Help with Arrays needed (1 Viewer)

C

conbrad

Guest
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
on.
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
Extension
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
 

dcx693

Registered User.
Local time
Yesterday, 21:20
Joined
Apr 30, 2003
Messages
3,265
This is not an uncommon problem. Access has no built-in way of sorting arrays, believe it or not. You could write your own sort routine, but perhaps someone has already posted one on the forums.

You define an array of string variables just as you would an ordinary string variable, just include () after the name of the variable. Something like:
Dim strFileName() as String

Within the loop of your Filesearch, assign each found filename to the strFileName() array like this:
strFileName(I)=.FileName

An alternative to sorting the array would be to create a new table, create a field in that table, then populate that table with the filenames. Then it's simply a matter of opening the stored table as a sorted recordset using DAO or ADO.

If that proves too slow, you don't even need to store the results in a table. You can do the entire operation in memory and open the temporary recordset as another sorted recordset, then write the sorted recordset back over the original array. Here's a link to some sample code: Sort Values from different fields but same row in a query
 

EMP

Registered User.
Local time
Today, 02:20
Joined
May 10, 2003
Messages
574
Some more info is needed.

What is sFilePath(j)?
If what you want is only one directory, you can simply put in the directory e.g.
.LookIn = "C:\My Documents"

And what do you want the code to do with
Application.Loadfromtext Forms, MyFile, .FoundFiles(I)?
 

Tim K.

Registered User.
Local time
Today, 02:20
Joined
Aug 1, 2002
Messages
242
FileSearch already has a SortOrder property. Check your similar post in the other forum for more details.

EMP, LoadFromText is one of several undocumented procedures hiddenly provided by Access. It enables you to create a form from a text file that was created by SaveAsText procedure.

To learn more about the 2 hidden features, press Ctrl + G>click Object Browser (or press F2)>right click on the Object Browser pane>select Show Hidden Members command>type the 2 features in Search Text box for their syntax.

:)
 

EMP

Registered User.
Local time
Today, 02:20
Joined
May 10, 2003
Messages
574
Thanks,Tim. Didn't notice their existence in the Application object. Thanks again.
 

Users who are viewing this thread

Top Bottom