Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / July 2008



Tip: Looking for answers? Try searching our database.

Sorting an address

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 14 Jul 2008 14:05 GMT
Hi all,

Please forgive me if I am asking this question in the wrong area.

In my app I am connecting to an Access database that contains a table with
an address field.

I am trying to sort the table by using "SELECT * FROM members ORDER BY ADDR"
but the result sorts the street address as text (obviously), giving me house
number 100 before house number 2.

How can I produce a sort that will sort the address by the number portion
correctly?

Thanks in advance,

George
Norm Cook - 17 Jul 2008 13:20 GMT
> Hi all,
>
[quoted text clipped - 11 lines]
> How can I produce a sort that will sort the address by the number portion
> correctly?

I don't know of an SQL solution, i.e. a "smart sort", but sorting numbers
represented as strings is a common problem.  The most common
solution is to left justify the numbers so they sort properly.

For example the series 1,2,3,10,11 (as strings) will sort as 1,10,11,2,3.
But
if the numbers are changed to __1, __2, __3, _10, _11 (where the underscore
is a space, they will sort properly.

A short VB example with a sorted listbox might be:

Private Sub Form_Load()
Dim i As Long
For i = 0 To 100
 List1.AddItem LJust(i, 3)
Next
End Sub
Private Function LJust(ByVal Item As String, ByVal Length As Long) As String
LJust = Right$(Space$(Length) & Item, Length)
'or
'LJust = Format$(Item, String$(Length, "@"))
End Function

In short, you are going to have to reformat/edit your ADDR field
so it will sort properly

Here is a function that attempts to left justify the numbers in an
address.

Private Function LJustAddr(ByVal Addr As String)
'assumes the number comes first, followed by a space
Dim NewNum As String
Dim OldNum As String
OldNum = Split(Addr)(0)
NewNum = LJust(OldNum, 5) '5 digit house number
LJustAddr = Replace$(Addr, OldNum, NewNum)
End Function

Air code to edit your field (DAO)
For i = 1 to RS.RecordCount
RS.Edit
RS.Fields("ADDR").Value = LJustAddr(RS.Fields("ADDR").Value)
RS.Update
RS.MoveNext
Next
George - 17 Jul 2008 14:16 GMT
Thank you...I'll work from that.

George
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.