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, 3 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
-
Small desktops
by
Susan Bradley
2 hours, 6 minutes ago -
Totally disable Bitlocker
by
CWBillow
3 hours, 4 minutes ago -
Totally disable Bitlocker
by
CWBillow
4 hours, 43 minutes ago -
Windows 11 ad from Campaign Manager in Windows 10 (Awaiting moderation)
by
Jim McKenna
8 hours, 12 minutes ago -
Phishers extract Millions from HMRC accounts..
by
Microfix
16 hours, 51 minutes ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
18 hours, 43 minutes ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
21 hours, 52 minutes ago -
Mystical Desktop
by
CWBillow
22 hours, 5 minutes ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
3 hours, 23 minutes ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
12 hours, 16 minutes ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
1 day, 13 hours ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
1 day, 16 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
1 day, 14 hours ago -
What is wrong with simple approach?
by
WSSpoke36
1 day, 6 hours ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
2 days, 1 hour ago -
Location, location, location
by
Susan Bradley
16 hours, 4 minutes ago -
Cannot get a task to run a restore point
by
CWBillow
2 days, 3 hours ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
1 day, 17 hours ago -
June 2025 Office non-Security Updates
by
PKCano
2 days, 13 hours ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
16 hours, 24 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
2 days, 13 hours ago -
How start headers and page numbers on page 3?
by
Davidhs
3 days ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
2 days, 2 hours ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
3 days, 8 hours ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
3 days, 8 hours ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
2 days, 22 hours ago -
Firefox 139
by
Charlie
2 days, 15 hours ago -
Who knows what?
by
Will Fastie
1 day, 17 hours ago -
My top ten underappreciated features in Office
by
Peter Deegan
3 days, 9 hours ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
1 day, 3 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.