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, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
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
-
Does Your State Reveal Who’s Been Hacked?
by
Nibbled To Death By Ducks
3 hours, 9 minutes ago -
A one-year extension to Windows 10 โ almost free!
by
Susan Bradley
20 minutes ago -
Windows Configuration Update (KB5062324) โ June 2025
by
Alex5723
3 hours, 5 minutes ago -
A federal judge sides with Anthropic in lawsuit over training AI
by
Alex5723
8 hours, 2 minutes ago -
Name of MS Word Formatting Feature
by
John Baum
9 hours, 10 minutes ago -
InControl Failure?
by
Casey H
15 hours, 7 minutes ago -
Microsoft : Free 1 year support for Windows 10 after EOL
by
Alex5723
8 minutes ago -
MS-DEFCON 3: Businesses must tread carefully
by
Susan Bradley
2 hours, 39 minutes ago -
McLaren Health Care says data breach impacts 743,000 patients
by
Nibbled To Death By Ducks
1 day, 7 hours ago -
WhatsApp banned on House staffers’ devices
by
Alex5723
1 day, 2 hours ago -
Is your device eligible?
by
Susan Bradley
1 day, 10 hours ago -
Windows 11 Insider Preview build 26200.5661 released to DEV
by
joep517
1 day, 16 hours ago -
Windows 11 Insider Preview build 26120.4452 (24H2) released to BETA
by
joep517
1 day, 16 hours ago -
Hello Windows…My Problem is Windows Hello…
by
rdleib
1 day, 17 hours ago -
New Canon Printer Wants Data Sent
by
Win7and10
1 day, 17 hours ago -
I set up passkeys for my Microsoft account
by
Lance Whitney
3 hours, 54 minutes ago -
AI is for everyone
by
Peter Deegan
1 day, 17 hours ago -
Terabyte update 2025
by
Will Fastie
1 day, 11 hours ago -
Migrating from Windows 10 to Windows 11
by
Susan Bradley
14 hours, 16 minutes ago -
Lost sound after the upgrade to 24H2?
by
Susan Bradley
11 hours, 41 minutes ago -
How to move 10GB of data in C:\ProgramData\Package Cache ?
by
Alex5723
20 hours, 2 minutes ago -
Plugged in 24-7
by
CWBillow
2 days, 2 hours ago -
Netflix, Apple, BofA websites hijacked with fake help-desk numbers
by
Nibbled To Death By Ducks
3 days, 5 hours ago -
Have Copilot there but not taking over the screen in Word
by
CWBillow
3 days, 2 hours ago -
Windows 11 blocks Chrome 137.0.7151.68, 137.0.7151.69
by
Alex5723
4 days, 20 hours ago -
Are Macs immune?
by
Susan Bradley
1 day, 12 hours ago -
HP Envy and the Function keys
by
CWBillow
4 days, 4 hours ago -
Microsoft : Removal of unwanted drivers from Windows Update
by
Alex5723
1 day, 21 hours ago -
MacOS 26 beta 1 dropped support for Firewire 400/800
by
Alex5723
5 days, 8 hours ago -
Unable to update to version 22h2
by
04om
2 days, 16 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.