Solved Is there another way to sort ascending and descending numbers? (1 Viewer)

Matin_Murad

Member
Local time
Today, 14:10
Joined
Jul 1, 2020
Messages
37
I want ascending: 1, 2, 3, 4, 10, 11, 101, 121, 133 without using queries
 

Attachments

  • sort.accdb
    608 KB · Views: 69

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:10
Joined
Jul 9, 2003
Messages
16,282
Well if you didn't have 10 and 101 in there, you could use text and sort that.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:10
Joined
Feb 19, 2013
Messages
16,618
you don't need to use queries to sort - user can use the right click menu on a table, query or form or you can use the form order by and order by on load properties . Reports ignore any sorting done in a recordsource ahd you have to use the group by/sort by functionality provided by reports.

As far as your sample db is concerned, change the datatype from text to number
 

Matin_Murad

Member
Local time
Today, 14:10
Joined
Jul 1, 2020
Messages
37
I don't want to go to a table and then right-click sort and sort
And I do not want to change the data type to a number. There are letters with numbers
I want to sort data with a button and a click
 

plog

Banishment Pending
Local time
Today, 16:10
Joined
May 11, 2011
Messages
11,646
There are letters with numbers

You can't give us sample data and not include pertinent examples. Can you provide a better sample set (with more representative data) and demonstrate how you want it to sort?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:10
Joined
Jul 9, 2003
Messages
16,282
You can't give us sample data and not include pertinent examples. Can you provide a better sample set (with more representative data) and demonstrate how you want it to sort?

Ditto.
 

Matin_Murad

Member
Local time
Today, 14:10
Joined
Jul 1, 2020
Messages
37
I want to arrange numbers inside the text field (numbers from small to large)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,533
With your current example (no letters and numbers mixed) you could try
Code:
Private Sub ASC_Click()
    Me.OrderBy = "val([number]) ASC"
    Me.OrderByOn = True

End Sub

Private Sub Desc_Click()
     Me.OrderBy = "val([number]) Desc"
    Me.OrderByOn = True

End Sub
As said this could get much more complicated with a mix of letters and numbers.
 

Matin_Murad

Member
Local time
Today, 14:10
Joined
Jul 1, 2020
Messages
37
Me.RecordSource = "SELECT tabl1.number, * FROM tabl1 ORDER BY IIf([number] Is Null,0,Val([number])) ASC;"
 

Matin_Murad

Member
Local time
Today, 14:10
Joined
Jul 1, 2020
Messages
37
مع مثالك الحالي (لا توجد أحرف وأرقام مختلطة) يمكنك المحاولة
[كود] Private Sub ASC_Click ()
Me.OrderBy = "val ([number]) ASC"
Me.OrderByOn = صحيح

End Sub

تفاصيل فرعية خاصة ()
Me.OrderBy = "val ([العدد]) تنازلي"
Me.OrderByOn = صحيح

End Sub [/ CODE]
كما قيل ، قد يصبح الأمر أكثر تعقيدًا مع مزيج من الأحرف والأرقام.
This is what is required sir... Thank you
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:10
Joined
Feb 19, 2002
Messages
43,301
Please tell us why you don't want to use a query since that is what every one of us would suggest because your data is in a recordset and queries are how you deal with recordsets. Think about what an in memory process using arrays would entail. Then think about how it would be displayed since it can't be displayed using the rows of the form. Start by looking up Bubble sort which is probably the simplest version of in memory sorting.

VBA — Bubble Sort. A bubble sort is a technique to order… | by breakCorporate | Medium
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,533
Please tell us why you don't want to use a query since that is what every one of us would suggest because your data is in a recordset and queries are how you deal with recordsets. Think about what an in memory process using arrays would entail. Then think about how it would be displayed since it can't be displayed using the rows of the form. Start by looking up Bubble sort which is probably the simplest version of in memory sorting
Are you looking at the correct thread? The OP wanted to use the Sort by property, there is no mention or suggestions of arrays.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:10
Joined
Feb 19, 2002
Messages
43,301
What he said was:
I want ascending: 1, 2, 3, 4, 10, 11, 101, 121, 133 without using queries
And his example showed the data in a text box.

I did forget to mention that the only way to sort this set of data is to sort on a zero filled copy of the fields. Otherwise the result ends up as1,10, 11, 101, 121, 133, 2, 3, 4 or to use the Val() function to make the values numeric.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:10
Joined
Feb 19, 2013
Messages
16,618
in post #4 OP said

There are letters with numbers

so
Me.OrderBy = "val ([number]) ASC"

is not going to work unless all the numbers in every record are at the beginning of the value.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,533
is not going to work unless all the numbers in every record are at the beginning of the value.
Yep, I think I made that overly clear in thread 9.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:10
Joined
Feb 19, 2002
Messages
43,301
Well the thread has been marked complete with no clue as to the actual solution.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:10
Joined
May 21, 2018
Messages
8,533
Well the thread has been marked complete with no clue as to the actual solution
Huh? We know exactly the solution used. In thread 11, the OP post the solution used and even explicitly says so.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:10
Joined
May 7, 2009
Messages
19,245
And I do not want to change the data type to a number. There are letters with numbers
so maybe he is expecting to have letters there.
for now, with all records in string of numbers format, it will work.
what will the future holds?

mr.murad, have a consistent coding.
you also need to have a rule on what can be
entered on that field and validate all entries against your rules.
 

Users who are viewing this thread

Top Bottom