I’ve created a list box of values which I’d like sorted for display in the list box in ascending order. Any ideas would be appreciated. Thanks
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
How do I sort an Array in VBA
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » How do I sort an Array in VBA
- This topic has 8 replies, 4 voices, and was last updated 24 years, 2 months ago.
Viewing 0 reply threadsAuthorReplies-
WSkmedgyes
AskWoody LoungerMarch 7, 2001 at 5:45 am #517647Hi jp,
There is probably some sort routine in VBA, but I’ll be darned if I can find it. So, make your own sorting routine,
by coding a ‘bubblesort’.This consists of two nested loops, i as the outer loop and j as the inner loop.
For each iteration of the outer loop, the inner loop j goes through all the elements of the array BELOW the current i position. Each time it compares the two items, and if i is greater than j, it reverses the two values.You do this by temporarily storing the i value to a temp variable, let i = j and then let j = tempvar.
I have NOT tested this on anything, but I wrote it up to give you an idea:
Private Sub SortList()
‘Bubble sort
Dim i, j
Dim TempdataFor i = 0 To lstList.ListCount – 1
For j = i + 1 To lstList.ListCount
If lstList.ItemData(i) > lstList.ItemData(j) Then
Tempdata = lstList.ItemData(i)
lstList.ItemData(i) = lstList.ItemData(j)
lstList.ItemData(j) = Tempdata
End If
Next j
Next i
End SubAs I say I didn’t test it (it’s 12:35 am FCOL) but you get the idea. Play with it a bit till you get it right. It’s quite inefficient, but if your list is not overly long, it should work quite well.
-
WScharlotte
AskWoody LoungerMarch 7, 2001 at 5:56 am #517649Nope, there’s no sort function for an array. A bubble sort is the usual way to do it, although the other possibility is to create a recordset instead of an array, sort the recordset, and then create your values list from the recordset instead of from an array. With ADO, you can create recordsets on the fly by using code similar to that used to create a table in code. The difference here is that you don’t need a table and you don’t need to save the recordset. You can just use it and throw it away.
-
WSAndyAinscow
AskWoody Lounger -
WScharlotte
AskWoody LoungerMarch 7, 2001 at 7:57 am #517674Ken Getz has dicussed this in various articles and in the Office 2000 Programmer’s Guide. Here’s some slightly tweaked code that demonstrates building a recordset “on the fly”. It’s slower than using an array, but it gives you functionality that arrays lack, so there’s a trade off. One big advantage is that you can persist the recordset if you want, so you can open it up again later and use it without having to recreate it.
Public Function RecordsetArray() 'based on Access 2000 Developer's Handbook sample code Dim rst As ADODB.Recordset Dim intElement As Integer 'instantiate the recordset Set rst = New ADODB.Recordset With rst 'append two fields/"dimensions" .Fields.Append "ColorID", adSmallInt .Fields.Append "ColorName", adVarChar, 10 'put data in the recordset .Open .AddNew Array("ColorID", "ColorName"), _ Array(1, "Red") .AddNew Array("ColorID", "ColorName"), _ Array(2, "Orange") .AddNew Array("ColorID", "ColorName"), _ Array(3, "Yellow") .AddNew Array("ColorID", "ColorName"), _ Array(4, "Green") .AddNew Array("ColorID", "ColorName"), _ Array(5, "Blue") .AddNew Array("ColorID", "ColorName"), _ Array(6, "Indigo") .AddNew Array("ColorID", "ColorName"), _ Array(7, "Violet") 'write all pending changes - no save required .UpdateBatch 'Dump the recordset to the Immediate Window .MoveFirst Do Until .EOF Debug.Print !ColorID, !ColorName .MoveNext Loop End With rst.Close Set rst = Nothing End Function
Actually, I’m going to attach a zipped database that demonstates the use of this kind of recordset and includes an unbound form based on it so that you can edit, add and remove items from the persisted recordset.
-
WSAndyAinscow
AskWoody Lounger -
WScharlotte
AskWoody LoungerMarch 7, 2001 at 2:30 pm #517721[indent]
now to convert it to C++ and then pass onto a report developed with Crystal reports
[/indent]Not on your life! I don’t do C++ and avoid Crystal Reports. However, the ADO code itself will be virtually identical in whatever language you use it in. Only the language-specific constructs will change (and the names, to protect the innocent
).
-
WSAndyAinscow
AskWoody LoungerMarch 7, 2001 at 4:05 pm #517734You misunderstood me Charlotte. It was meant as a throw away comment. The conversion is my task with your sample code.
I’ve also looked at your sample DB. It’s pretty neat what it does. I wish everyone would put plenty of comments in code (especially if other people will end up with it). -
WScharlotte
AskWoody LoungerMarch 8, 2001 at 1:59 am #517823No, I understood and was answering in kind.
As far as the sample DB goes, I try to comment stuff like that to excess because I know it will fall into the hands of those who are still learning. Since I learned originally by picking apart someone else’s programs, I try to be as kind and helpful as possible with mine.
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Sometimes I wonder about these bots
by
Susan Bradley
1 hour, 59 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
3 hours, 57 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
4 hours, 57 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
4 hours, 23 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
53 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
7 hours, 39 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
7 hours, 41 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
1 hour, 34 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
15 hours, 48 minutes ago -
0Patch, where to begin
by
cassel23
9 hours, 50 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 5 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
17 hours, 12 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 13 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 4 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
16 hours, 5 minutes ago -
Installer program can’t read my registry
by
Peobody
11 hours, 3 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 2 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 10 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 1 hour ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 10 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 11 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day, 8 hours ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 14 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 15 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 20 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 1 hour ago -
Another big Microsoft layoff
by
Charlie
3 days, 1 hour ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
3 hours, 34 minutes ago -
May 2025 updates are out
by
Susan Bradley
5 hours, 16 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 7 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.