Hi, Readers.
I recently received a question, is there any way to get the next or previous letter in the alphabet in AL.
For example, if the input was equal to ‘c’, they want to output ‘b’ and ‘d’.
In this post, I would like to share two easy way about this, hope it will help.
1. Using ASCII
As you might know, we can do this easily by using ASCII in SQL Query.


PS: ASCII Code for Letters
Letter | ASCII Code | Letter | ASCII Code |
a | 97 | A | 65 |
b | 98 | B | 66 |
c | 99 | C | 67 |
d | 100 | D | 68 |
e | 101 | E | 69 |
f | 102 | F | 70 |
g | 103 | G | 71 |
h | 104 | H | 72 |
i | 105 | I | 73 |
j | 106 | J | 74 |
k | 107 | K | 75 |
l | 108 | L | 76 |
m | 109 | M | 77 |
n | 110 | N | 78 |
o | 111 | O | 79 |
p | 112 | P | 80 |
q | 113 | Q | 81 |
r | 114 | R | 82 |
s | 115 | S | 83 |
t | 116 | T | 84 |
u | 117 | U | 85 |
v | 118 | V | 86 |
w | 119 | W | 87 |
x | 120 | X | 88 |
y | 121 | Y | 89 |
z | 122 | Z | 90 |
Can we do the same thing in AL?
Yes, we can use the CHAR Data Type to get the ordinal value.
Let’s see two examples:
1. From a letter to get ASCII Code.


2. From ASCII Code to get a letter.


Okay, now you can easily get the next or previous letter. (Please note that you may also get symbol)

Test Video:
Source Code:
page 50100 "Get next or previous letter"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(Input)
{
field(InputLetter; InputLetter)
{
Caption = 'Input Letter';
ApplicationArea = All;
trigger OnValidate()
var
InputLetterASCII: Integer;
begin
InputLetterASCII := InputLetter;
PerviousLetter := ' ';
NextLetter := ' ';
PerviousLetter := InputLetterASCII - 1;
Nextletter := InputLetterASCII + 1;
end;
}
}
group(Result)
{
field(PerviousLetter; PerviousLetter)
{
Caption = 'Pervious Letter';
ApplicationArea = All;
Editable = false;
}
field(NextLetter; NextLetter)
{
Caption = 'Next Letter';
ApplicationArea = All;
Editable = false;
}
}
}
}
var
InputLetter: Char;
NextLetter: Char;
PerviousLetter: Char;
}
2. Using List Data Type
List Data Type: Represents a strongly typed list of ordered objects that can be accessed by index. Contrary to the Array data type, a List is unbounded, such that its dimension does not need to be specified upon declaration.
So you can pre-store the letters to a List and then get the letter at the specified index.
For example:

Note: List.Get Method will raise an error if the index is outside the valid range.
Test Video:
Source Code:
page 50100 "Get next or previous letter"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(Input)
{
field(InputLetter; InputLetter)
{
Caption = 'Input Letter';
ApplicationArea = All;
trigger OnValidate()
var
Index: Integer;
AlphabetValueSet: Text;
AlphabetList: List of [Text];
begin
PerviousLetter := '';
NextLetter := '';
AlphabetValueSet := 'a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z';
AlphabetList := AlphabetValueSet.Split(' ');
if not AlphabetList.Contains(InputLetter) then
Error('Please enter a single letter');
Index := AlphabetList.IndexOf(InputLetter);
if InputLetter <> 'a' then
PerviousLetter := AlphabetList.Get(Index - 1);
if InputLetter <> 'Z' then
Nextletter := AlphabetList.Get(Index + 1);
end;
}
}
group(Result)
{
field(PerviousLetter; PerviousLetter)
{
Caption = 'Pervious Letter';
ApplicationArea = All;
Editable = false;
}
field(NextLetter; NextLetter)
{
Caption = 'Next Letter';
ApplicationArea = All;
Editable = false;
}
}
}
}
var
InputLetter: Text;
NextLetter: Text;
PerviousLetter: Text;
}
END
Hope this will help.
Thanks for reading.
ZHU
コメント