Online Book Reader

Home Category

Access Cookbook - Ken Getz [157]

By Root 1989 0
at the dividing value, which certainly isn't less than itself:

Do While varArray(i) < varTestVal

i = i + 1

Loop

The sort starts from the right, walking backward through the array until it finds an item that isn't greater than the dividing value. This search is guaranteed to stop at the dividing value, which certainly isn't more than itself:

Do While varArray(j) > varTestVal

j = j - 1

Loop

If the position from Step 3 is less than or equal to the position found in Step 4, the sort swaps the elements at the two positions, then increments the pointer for Step 3 and decrements the pointer for Step 4:

If i <= j Then

SwapElements varArray( ), i, j

i = i + 1

j = j - 1

End If

The sort repeats Steps 3 through 5 until the pointer from Step 3 is greater than the pointer from Step 4 (i > j). At this point, every item to the left of the dividing element is less than or equal to it, and everything to the right is greater than or equal to it.

Choosing the smaller partition first, the sort repeats all these steps on each of the subsets to either side of the dividing value, until Step 1 indicates that it's done:

If j <= intMid Then

QuickSort varArray( ), intLeft, j

QuickSort varArray( ), i, intRight

Else

QuickSort varArray( ), i, intRight

QuickSort varArray( ), intLeft, j

End If

There are probably sort algorithms that are simpler than the quicksort algorithm, but for arrays that aren't already sorted, quicksort's speed is hard to beat. (For presorted arrays, it doesn't do as well as some other sorts. But most arrays don't come to the QuickSort subroutine in order.) As it is, the QuickSort subroutine is capable of handling only single-column arrays. If you need to sort multicolumn arrays, you'll need to either modify the code to handle those cases or move the data into a table and let Access do the sorting for you.

See Also


See the next solution for an example of using QuickSort.

7.8. Fill a List Box with a List of Files


Problem


You need to present your users with a sorted list of files with a specific filename extension in a particular directory. You found the Dir function, but you can't find a way to get this information into a list box. Is there a way to do this?

Solution


This problem provides the perfect opportunity to use the past three solutions. It involves creating a list-filling callback function, passing arrays as parameters, and sorting an array. In addition, you'll fill that array with a list of files matching a particular criterion, using the Dir function.

Load the form frmTestFillDirList from 07-08.MDB. Enter a file specification into the text box (for example, c:\*.exe). Once you leave the text box (by pressing either Tab or Return), the code attached to the AfterUpdate event will force the list box to requery. When that happens, the list box will fill in with the matching filenames. Figure 7-12 shows the results of a search for c:\\*.*.

Figure 7-12. frmTestFillDirList, searching for *.* in the C:\ folder

To include this functionality in your own applications, follow these steps:

On a form, create a text box and a list box, with properties set as shown in Table 7-6.

Table 7-6. Property settings for the controls on the directory list form

Control

Property

Setting

Text box

Name

txtFileSpec

AfterUpdate

[Event Procedure]

List box

Name

lstDirList

RowSourceType

FillList

AfterUpdate

[Event Procedure]

Enter the following code in the text box's AfterUpdate event procedure. (See the Preface for more information on creating event procedures.) This code forces the list box to requery itself when you enter a value in the text box, and then move to some other control:

Sub txtFileSpec_AfterUpdate ( )

Me.lstDirList.Requery

End Sub

Enter the following code in the list box's AfterUpdate event. This is sample code that pops up a message box indicating which file you chose:

Sub lstDirList_AfterUpdate ( )

MsgBox "You chose: " & Me.lstDirList.Value

End Sub

Enter the following code into a global module so that it

Return Main Page Previous Page Next Page

®Online Book Reader