A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Sorting fractional values



 
 
Thread Tools Display Modes
  #1  
Old October 28th, 2004, 04:27 PM
ehunter
external usenet poster
 
Posts: n/a
Default Sorting fractional values

I have a table with a field containing fractional numbers with the TEXT data
type. I need to sort the field from smallest to largest values. However, when
sorted, the values end up 1-1/2, 10-1/2, 2-1/2, etc. instead of 1-1/2, 2-1/2,
10-1/2, etc. What can I do to sort correctly?
Thanks.
--
Ed
  #2  
Old October 29th, 2004, 11:26 AM
Brett Collings [429338]
external usenet poster
 
Posts: n/a
Default

On Thu, 28 Oct 2004 08:27:05 -0700, "ehunter"
wrote:

I have a table with a field containing fractional numbers with the TEXT data
type. I need to sort the field from smallest to largest values. However, when
sorted, the values end up 1-1/2, 10-1/2, 2-1/2, etc. instead of 1-1/2, 2-1/2,
10-1/2, etc. What can I do to sort correctly?
Thanks.



I don't know Ed, that's a real messy one. Text sorts differently to
numbers and I'm not sure there's anything to change that. With text
you get, as you've found out
1
10
101
2

I love that we are all metric and everything is in decimals. I hope
someone in the, US where you are used to fractional data, can help


Cheers,
Brett

----------------------------------------------
Be adventurous, be bold, be careful, be a star
----------------------------------------------
Brett Collings
Business & Systems Analyst
Management Information Systems Developer
  #3  
Old October 29th, 2004, 03:12 PM
PC Datasheet
external usenet poster
 
Posts: n/a
Default

Sort on the expression: Val([textfieldname])


--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications

www.pcdatasheet.com


"ehunter" wrote in message
...
I have a table with a field containing fractional numbers with the TEXT

data
type. I need to sort the field from smallest to largest values. However,

when
sorted, the values end up 1-1/2, 10-1/2, 2-1/2, etc. instead of 1-1/2,

2-1/2,
10-1/2, etc. What can I do to sort correctly?
Thanks.
--
Ed



  #4  
Old October 29th, 2004, 05:08 PM
John Vinson
external usenet poster
 
Posts: n/a
Default

On Fri, 29 Oct 2004 14:12:07 GMT, "PC Datasheet"
wrote:

Sort on the expression: Val([textfieldname])


Eh? Won't work correctly: from the debug window -

?val("1 3/4")
13

Not that simple. You'll need some VBA code (which I'm sure is out
there somewhere) to parse the text string, find the slash, evaluate
the fraction and combine it with the integer portion.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
  #5  
Old October 29th, 2004, 07:31 PM
Gregory Paret
external usenet poster
 
Posts: n/a
Default

John Vinson wrote:
On Fri, 29 Oct 2004 14:12:07 GMT, "PC Datasheet"
wrote:
Sort on the expression: Val([textfieldname])

Eh? Won't work correctly: from the debug window -

?val("1 3/4")
13

Not that simple. You'll need some VBA code (which I'm sure is out
there somewhere) to parse the text string, find the slash, evaluate
the fraction and combine it with the integer portion.


Something like this works for most well-formed strings:

Function ParseFraction(ByVal str As String) As Double
Dim i, j, k
str = Trim(str)
i = InStr(2, str, "-")
If i = 0 Then i = InStr(str, " ")
If i And i Len(str) Then
j = Val(Left(str, i))
k = Eval(Mid(str, i + 1)) * Sgn(j)
ElseIf IsNumeric(str) Then
j = Val(str)
Else
k = Eval(str)
End If
ParseFraction = j + k
End Function

Then you can add an expression field like
"NumField: ParseFraction([TextField])
and sort on it.

-Greg.
  #6  
Old October 30th, 2004, 01:16 AM
ehunter
external usenet poster
 
Posts: n/a
Default

Greg,
Thanks for the responses, guys. Greg, the code works but changes the values
to decimal form. Is there possibly a function in Access that converts
decimals into lowest common denominator fractions that could be inserted
either into the procedure or the query to change the query results back into
fractional form?

"Gregory Paret" wrote:

John Vinson wrote:
On Fri, 29 Oct 2004 14:12:07 GMT, "PC Datasheet"
wrote:
Sort on the expression: Val([textfieldname])

Eh? Won't work correctly: from the debug window -

?val("1 3/4")
13

Not that simple. You'll need some VBA code (which I'm sure is out
there somewhere) to parse the text string, find the slash, evaluate
the fraction and combine it with the integer portion.


Something like this works for most well-formed strings:

Function ParseFraction(ByVal str As String) As Double
Dim i, j, k
str = Trim(str)
i = InStr(2, str, "-")
If i = 0 Then i = InStr(str, " ")
If i And i Len(str) Then
j = Val(Left(str, i))
k = Eval(Mid(str, i + 1)) * Sgn(j)
ElseIf IsNumeric(str) Then
j = Val(str)
Else
k = Eval(str)
End If
ParseFraction = j + k
End Function

Then you can add an expression field like
"NumField: ParseFraction([TextField])
and sort on it.

-Greg.

  #7  
Old October 30th, 2004, 01:32 AM
John Vinson
external usenet poster
 
Posts: n/a
Default

On Fri, 29 Oct 2004 17:16:01 -0700, "ehunter"
wrote:

Greg,
Thanks for the responses, guys. Greg, the code works but changes the values
to decimal form. Is there possibly a function in Access that converts
decimals into lowest common denominator fractions that could be inserted
either into the procedure or the query to change the query results back into
fractional form?


stealing Gregory's code with gratitude, and attribution

I'd suggest using a calculated field for the sorting (and for range
searching, if you need to do so), and keep the text field as is.


John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
  #8  
Old October 30th, 2004, 03:13 AM
ehunter
external usenet poster
 
Posts: n/a
Default

Many thanks, sir. The query works as desired.

"John Vinson" wrote:

On Fri, 29 Oct 2004 17:16:01 -0700, "ehunter"
wrote:

Greg,
Thanks for the responses, guys. Greg, the code works but changes the values
to decimal form. Is there possibly a function in Access that converts
decimals into lowest common denominator fractions that could be inserted
either into the procedure or the query to change the query results back into
fractional form?


stealing Gregory's code with gratitude, and attribution

I'd suggest using a calculated field for the sorting (and for range
searching, if you need to do so), and keep the text field as is.


John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps

  #9  
Old November 29th, 2004, 04:54 PM
Gregory Paret
external usenet poster
 
Posts: n/a
Default

Gregory Paret wrote:
John Vinson wrote:

On Fri, 29 Oct 2004 14:12:07 GMT, "PC Datasheet"
wrote:

Sort on the expression: Val([textfieldname])

Eh? Won't work correctly: from the debug window -

?val("1 3/4")
13
Not that simple. You'll need some VBA code (which I'm sure is out
there somewhere) to parse the text string, find the slash, evaluate
the fraction and combine it with the integer portion.



Something like this works for most well-formed strings:

[Buggy code snipped]

Then you can add an expression field like
"NumField: ParseFraction([TextField])
and sort on it.


The code I previously posted wouldn't work for strings like "0-11/16", so I've
updated it to:

'
' Parse a string like 1-3/4 into a number 1.75
' The '-' between the whole number and the fraction can be ' ' instead.
' N.B.: Can do interesting things with oddly formed input!
' Author: Greg Paret
'
Function ParseFraction(ByVal str As String) As Double
Dim i, j, k
str = Trim(str)
i = InStr(2, str, "-")
If i = 0 Then i = InStr(str, " ")
If i And i Len(str) Then
j = Val(Left(str, i))
k = Eval(Mid(str, i + 1)) * IIf(j 0, -1, 1)
ElseIf IsNumeric(str) Then
j = Val(str)
Else
k = Eval(str)
End If
ParseFraction = j + k
End Function

I hope the original didn't cause too much hair loss.

-Greg.
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting hke General Discussion 1 September 23rd, 2004 02:40 PM
need to calculate intermediate values Chris Alexander Worksheet Functions 3 August 18th, 2004 02:14 PM
ignoring zero values in excel charts Whirlwind Charts and Charting 3 September 24th, 2003 04:03 PM


All times are GMT +1. The time now is 05:25 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.