To resolve this unexpected sort order, you can sort an
array, and then assign the array to the combo box or the list box. To use this
method, follow the steps in Method 1.
If you were assigning database
objects to your combo box or list box when you encountered this behavior,
another method is to assign the appropriate system table to the
RowSource property of the combo box or the list box in sorted order. Be
advised, however, that although this method works in Microsoft Access 2000, it
is not guaranteed to work with future versions of Access because system tables
may change. To use this method, follow the steps in Method 2.
Method 1: Assigning Sorted Array to a Combo Box or a List Box
- Open the sample database Northwind.
- In the Database window, click Forms, and then click New.
- In the New Form dialog box, click Design View, and then click OK.
- Add a combo box control to the form.
- Right-click the combo box, click Properties, and then set the following properties for the combo box:
Name: cmbReports
RowSourceType: fncGetReports
LimitToList: Yes
Left: 2"
Top: 0.5"
Width: 2"
- Right-click the label, click Properties, and then set the following properties for the label:
Name: lblReports
Caption: Reports:
Left: 1"
Top: 0.5"
Width: 1"
FontWeight: Bold
TextAlign: Right
- On the View menu, click Code, and then copy or paste the following code:
Option Compare Database
Option Explicit
Dim strArray() As String
Function fncGetReports(Ctrl As Control, varID As Variant, _
varRow As Variant, varCol As Variant, varCode As Variant) _
As Variant
'Assign all report names to the combo box.
Dim proj As CurrentProject
Set proj = Application.CurrentProject
Select Case varCode
Case acLBInitialize
fncGetReports = True
Case acLBOpen
fncGetReports = Timer
Case acLBGetRowCount
fncGetReports = proj.AllReports.Count
Case acLBGetColumnCount
fncGetReports = 1
Case acLBGetColumnWidth
fncGetReports = -1
Case acLBGetValue
fncGetReports = strArray(varRow + 1)
End Select
End Function
Sub subAddItem(MyList() As String, MyItem As String)
ReDim Preserve MyList(UBound(MyList) + 1)
MyList(UBound(MyList)) = MyItem
End Sub
Private Sub Form_Load()
Dim obj As AccessObject
Dim proj As CurrentProject
ReDim strArray(0)
Set proj = Application.CurrentProject
'Search for open AccessObject objects in AllReports collection.
For Each obj In proj.AllReports
subAddItem strArray, obj.Name
Next obj
subSort strArray
End Sub
Sub subSort(MyList() As String)
Dim intRet As Integer, intCompare As Integer, intLoopTimes As Integer
Dim strTemp As String
For intLoopTimes = 1 To UBound(MyList)
For intCompare = LBound(MyList) To UBound(MyList) - 1
intRet = StrComp(MyList(intCompare), MyList(intCompare + 1), _
vbTextCompare)
If intRet = 1 Then 'Current string is greater than previous.
strTemp = MyList(intCompare)
MyList(intCompare) = MyList(intCompare + 1)
MyList(intCompare + 1) = strTemp
End If
Next
Next
End Sub
- On the File menu, click Close and Return to Microsoft
Access.
- On the File menu, click Save, type frmListReports in the Form Name box, and then click OK.
- On the View menu, click Form View. Note that the reports are listed in alphabetical
order.
Method 2: Assigning Sorted System Table to Combo or List Box
Although this method will work with Microsoft Access 2000, it is
not a highly recommended method, as the structure of system tables may change
within future versions of Access.
- Open the sample database Northwind.
- In the Database Window, click Forms, and then click New.
- In the New Form dialog box, click Design View, and then click OK.
- Add a combo box control on the form.
- Right-click the combo box, click Properties, and then set the following properties for the combo box:
Name: cmbReports
RowSourceType: Table/Query
LimitToList: Yes
Left: 2"
Top: 0.5"
Width: 2"
- Assign the following SQL statement to the RowSource property:
SELECT Name, Type FROM MSysObjects WHERE Type=-32764 ORDER BY Name;
- Right-click the label, click Properties, and then set the following properties for the label:
Name: lblReports
Caption: Reports:
Left: 1"
Top: 0.5"
Width: 1"
FontWeight: Bold
TextAlign: Right
- On the File menu, click Save, type frmListReports in the Form Name box, and then click OK.
- On the View menu, click Form View. Note that the reports are listed in alphabetical
order.