Frequently asked questions - Base
How can I format a field as uppercase/lowercase?
This is how to make the entry of a field "Name" all uppercase and make only the first letter of the field "Firstname" uppercase.
There are two different solutions: using a masked field or a macro.
Whichever solution is chosen, it is useful to define the relevant fields in the table holding the data with the format "Text [VARCHAR_IGNORECASE]". The searches or selections from queries will be easier because they will be case insensitive.
Using a field mask
In the entry form, choose the "Masked field" control. This control is an entry field, which has two particular properties relevant to this task.
|Entry mask||Put the character codes, not the data to be entered: one code = one character
The codes determine what the field will accept at that position.
|Field validation||Yes: Only the characters specified as valid will be accepted
No: Invalid characters will be removed on leaving the field
To convert the field "Name" to uppercase automatically use the code "X":
- All characters are allowed (this is not limited to letters because it is possible to enter hyphens or apostrophes
- Automatic conversion from lowercase to uppercase
- The permitted number of characters is indicated by the number of the X characters
- Field validation: Yes
To capitalise just the first letter of the field "Firstname", use the code "A" followed by "x":
- The code "A" allows only the capital letters A-Z to be entered (hyphens and apostrophes aren't allowed at the beginning of a name): if a lowercase letter is entered it is automatically converted to uppercase.
- With "x" all characters are allowed.
Using a macro
It is possible to associate a macro with the event "Before update" of text fields. This event is triggered before the content of the changed control is written to the database.
We can change everything that has been entered with the uppercase/lowercase conversion.
Below is the code for two Python procedures "PysCapitaliseAll" and "PysCapitaliseOne" which are associated with the entry fields "Name" and "Firstname" respectively.
The procedures take as a parameter the contents of the control in question. They change the text contained in the text field.
- PysCapitaliseAll changes everything to uppercase.
- PysCapitaliseOne changes only the first character to uppercase.
option explicit sub PysCapitaliseOne(PysEvent) PysEvent.Source.text = Ucase(left(PysEvent.Source.text, 1)) & Lcase(mid(PysEvent.Source.text, 2)) end sub sub PysCapitaliseAll(PysEvent) PysEvent.Source.text = Ucase(PysEvent.Source.text) end sub
- File:FR.FAQ BASE 134 Capitaliser.odb Download an example database
- Format codes https://help.libreoffice.org/latest/en-US/text/sbasic/shared/01170101.html#hd_id3155379 Table of format codes