Suffix Help

JMZ

New member
Local time
Today, 15:13
Joined
Nov 20, 2020
Messages
25
I have a report that show what place a player finished at, this text box will show 1, 2, 3 and I would like it to show 1st, 2nd, 3rd.
Does anyone have an idea on how to.
 
I would make a quick table
NumberID ' numeric
NumberDesc 'text

1 1st
2 2nd
3 3rd
4 4th
....

Then in your query link your field to NumberID and display NumberDesc.
 
There are also functions that do this, but the above solution should take a few minutes and faster then finding code which would be less efficient anyways. You can do an iif or Switch in a query too.

The example is very similar
 
Or the choose function

choose(num,”1st”,”2nd”,”3rd”,”4th”…..)
 
CJ
Can you explain how, I'm just getting into programing.
 
Just as CJ_London has posted.
In the control source put

Code:
=choose(num,”1st”,”2nd”,”3rd”)
where num is the name of the field holding your 1, 2 or 3 value
 
It wants me to enter paramenter value "1st"
 
Show what you have written.
Code:
tt=1
? choose(tt,"1st","2nd","3rd")
1st
tt=2
? choose(tt,"1st","2nd","3rd")
2nd
tt=3
? choose(tt,"1st","2nd","3rd")
3rd
 
Code:
=Choose([placed],[”1st”],[”2nd”],[”3rd”])
 
It wants me to enter paramenter value "1st"
One of the most important things we have to learn about getting help in a technical forum, is that only you, the person asking the question, can actually see the tables, forms, code etc. in your application.

Others can make educated guesses about what might be going on, but without being able to see directly, it is always just a guess.

The solution is quite direct and easy to implement.
  • Show the forum what you see.
  • Tell the forum what you expect to happen.
  • Tell the forum what actually does happen.
 
This function should work for all numbers.
Code:
Public Function GetOrdinal(TheNumber As Variant) As String
  Dim suffix As String
  If Not IsNull(TheNumber) Then
    suffix = "th"
    If TheNumber Mod 100 >= 11 And TheNumber Mod 100 <= 13 Then
      suffix = "th"
    ElseIf TheNumber Mod 10 = 1 Then
      suffix = "st"
    ElseIf TheNumber Mod 10 = 2 Then
      suffix = "nd"
    ElseIf TheNumber Mod 10 = 3 Then
     suffix = "rd"
    End If
    GetOrdinal = CStr(TheNumber) & suffix
  End If
End Function

you call it like = GetOrdinal([placed])
I demoed a helper table too.

With the helper table you can do the second column too which you cannot write a simple function for.
ordinal.png


That function can be rewritten and even done inline.

 

Attachments

Last edited:
MajP
It worked like a charm. I will learn more from this code, thank you.
 
GPGeorge
I did copy what Gasman posted, when I placed it in the control source of the text box the code was change automatically to what you just posted. Quit being so quick to judge.
 
@JMZ
You have smart quotes or typographer quotes around your strings. This is probably because you copied this out of Word or some other document. You cannot type that directly into Access AFAIK. Because of that, Access assumed it was an invalid string and automatically put the [] as if it was a field. You cannot use smart quotes in sql or vba only straight quotes.


I tried with the smart quotes and it failed, but Gasman's code worked with straight quotes.
 
Last edited:
GPGeorge
I did copy what Gasman posted, when I placed it in the control source of the text box the code was change automatically to what you just posted. Quit being so quick to judge.
Not what I posted? :(

Edit: If you have to copy, paste,modify and copy again, then do the initial paste to notepad.
 
Last edited:
IF the OP pastes
Code:
“1st”
not
Code:
 "1st"
it will automatically do this
Code:
[“1st”]
and will not run.
 
I did not know that I copied from here and pasted it in notepad and change num to placed, copied and pasted it in the control source where access changed it, so I didn't question it. My apologies GPGeorge for being rude.
 
Well done for the apology.
It takes courage to admit when you are wrong. 👍
However, if you used notepad, I am puzzled as to how that occurred. 🤔

Ah, I see why now, you copied what I copied from @CJ_London post in post #5, and that had the smart quotes, so I was responsible for that, my apologies for that. :(

Note to self, always use code tags for posting code, but inspect that which came from non tagged code.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom