HOME - - / - - / - - LAZARUS/ DELPHI TUTORIALS
LAZARUS/ DELPHI COURSE - - / - - / - - Other material for programmers

Lazarus and Delphi Course: First steps towards databases... with some other stuff along the way...

Page URL: DataB1.htm

This page is information rich, and a has search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!



You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!

The lines of sample Lazarus/ Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)

This is just one exercise in a series of Lazarus / Delphi exercises. You will probably be best served by doing them in sequence... each assumes some prior knowledge. Material © TK Boyd, sheepdogsoftware.co.uk, 4/05-6/20.

Heavily re-worked, June 2020, to bring it up to date for Windows 10/ Lazarus 2.0.0 Based on earlier Delphi page. Probably still useful to Delphi programmers, too.


In this lesson, you make an application which can access data.
--- You develop alternate versions.

You should learn how to...
--- put commends in your sourcecode

You should learn about....
--- Declaring variables
--- Error management
--- Elegance

Pascal: the language behind both Lazarus and Delphi:
--- The "does not equal" test:<>
--- if... then... else

Lazarus/ Delphi built in functions:
--- UpperCase ====
This lesson will produce a small program which lets you look up data from inside the computer. Even simple database management programs are much, much more than that, and they let you manage you data far more easily than you'll be able to manage it with the program from this lesson. Nevertheless, this lesson is a start towards working with databases.

Start a new application. Put a button, a label, and an edit box on it. Rename form datab1f1.

Save what you've got so far... in a folder set up for JUST this... Start by creating a folder called "datab1" because that's going to be the root of all the important names in this project. (You might want to put that in a "higher" folder called "DT100 examples".) You can add a bit to it, if you like... "-Simple lookup", maybe?

Within THAT folder, make ANOTHER folder also called "datab1". (DON'T "add bits" to the name THIS time.)

During the first save, call the project's lpi file "datab1" ("LPI" comes from Lazarus... um... "Project Information">? Something like that. (It would be datab1.dpr for Delphi.)

Save the unit as datab1u1.pas.

Now we're going to modify what happens when a user clicks the button.

Double-click on the button, and Lazarus will give you a "skeleton" event handler for clicks of that button. Add to the skeleton, to make it what you see below.

Near the ends of most lines, there now appears a //. Everything after the // is ignored by Lazarus/ Delphi when it goes to compile your application. The text after the // is called a comment. Even though they are ignored by Lazarus/ Delphi, they are important parts of the program code. Comments, properly used, help you keep track of things... and that help can be very welcome!

The comments (//1, //2, etc) aren't important yet... you'll see what they are for in a moment.

procedure Tdatab1f1.Button1Click(Sender: TObject);
begin
label1.caption:='Not known';//2
if edit1.text='Madrid' then label1.caption:='Spain';//3
if edit1.text='Rome' then label1.caption:='Italy';//4
if edit1.text='Lima' then label1.caption:='Peru';//5
if edit1.text='Moscow' then label1.caption:='Russia';//6
if edit1.text='New York' then label1.caption:='USA';//7
end;


(You can use copy/ paste to move the following to your Lazarus IDE, instead of editing the lines by hand. When you have a bunch of code like this, it is perfectly acceptable to use copy/paste... as long as you read careful what you are putting the program.)

You should now have a program which, rather crudely, lets you look up what country a given city is in. You type the city name (exactly as it is known to the computer), and click on the button. If the city is known, the country appears in the label.

If it "doesn't work", don't be too downhearted! Maybe it DOES work... if you do the right thing!

Typing "new york" isn't the "right thing". You have to type "New York", with those letters... and only those letters... capitalized. THEN you should get "USA" on the label.

The program has one "frill": If you type an unknown city, the label is filled with "Not known". Look at the program, make sure you understand why this is happening. Filling something, as this program does, with a default value, before going on to try to find some better value is a common enough trick. Your applications should always be able to give sensible responses even when users enter nonsense requests. Anticipating all the mistakes users will make is difficult.

Is this a "good" database program? Of course not. Is it helping you get going with Lazarus/ Delphi? I hope so! Please feel free to change what is being looked up to something that interests you. You could make a little phone directory, or a program to disclose teacher's nicknames, or, or, or.... If your head isn't full of ideas, then either you need to do a bit more of this, or you're not "the right stuff" to be a programmer. Once you get started, you will find yourself full of "I wonder how I could..." ideas.


Now we're going to look at another solution to the problem of what if the user types in a city we don't know. Make the button's OnClick handler...
procedure Tdatab1f1.Button1Click(Sender: TObject);
var sTmp:string;  //<< NEW ! Call it "New 1"
begin
sTmp:='Not known';//modified 2
if edit1.text='Madrid' then sTmp:='Spain'; //modified 3
if edit1.text='Rome' then sTmp:='Italy'; //modified 4
if edit1.text='Lima' then sTmp:='Peru'; //modified 5
if edit1.text='Moscow' then sTmp:='Russia'; //modified 6
if edit1.text='New York' then sTmp:='USA'; //modified 7
if sTmp<>'Not known' then label1.caption:=sTmp; //<< NEW!! "New 2"
end;
Nota Bene: If you enter that into your Lazarus/ Delphi using copy/ paste, you may find, among other things, that "New 2" gets inserted as....
if sTmp&lt;&gt;'Not Found'....
If that happens, you'll have to take the "&lt;&gt;" out, replace it with the "< >" shown. If it happened this time, &lt; and &gt; are going to be a problem throughout and will need replacing each time with < and > respectively. (They will sometimes appear singly.)


There are several things to notice in the code:
_____
The line marked "New 1" is a declaration. It says that we will be using a variable called sTmp, and that it will to be able to store string type data.

_____
At line "modified 2", we put an initial value into sTmp.

.. and then in line "modified 3", and the others like it, we change what is stored in sTmp if we discover that the user has entered the name of a town we know about.

_____
Something to notice about if... then...: The "then" is more a matter of consequence than of timing. The Lazarus/ Delphi "then" is like the "then" in the following: "If you study hard, then you will get good marks on the test." The Lazarus/ Delphi "then" is not like the "then" in the following: "First we have January, then we have February."

_____
Line "new 4" says "If what is stored in sTmp does not equal the string 'Not Found' then..." Notice how you say "does not equal" in Lazarus/ Delphi (and Pascal generally). This is another case of using two symbols together to mean one "something". (You met ":=" earlier.)

_____
Note that we have both "=" and ":=". The equals sign on it's own is used when we are asking if two things are equal, i.e. "if edit1.text = 'Madrid'..." ("if edit1.text equals 'Madrid'...", i.e. if what is in edit1.text is the same as 'Madrid'"). The equals sign is also used just after a colon (":=") in which case the best way to read the two of them togeher is "becomes". We have sTmp:='Spain', which should be read "sTmp becomes 'Spain", or, more fulsomely: "The contents of variable sTmp should become 'Spain'."

_____
A quick word about how to type out things like this procedure. (Of course, Today I hope you just used copy / paste to "write" it into your program. But if you had to type something like it...)

Start by typing....
if edit1.text='' then sTmp:='';
... by hand.

Use copy paste to make enough copies of that. Then go through and write in the other bits by hand. You're not only saving yourself work, you are also giving yourself protection against typos.

_____

If you reverse the order of lines "new 4" and "moved and modified 5", the program will not work properly. Work out why. It took me a moment, and is a good example of the sort of silly little thing which can make a nuisance out of itself which is out of all proportion to its sophistication.

_____
Lines "new 4" and "moved and modified 5" work perfectly well when they are in the right order, but the way they are working are a bit clumsy. Happily, Lazarus/ Delphi provides a solution. You can have...
if sTmp<>'Not Found'
   then label1.caption:=sTmp
   else label1.caption:='Not known';
This brings us to the question of semicolons, which give many beginners headaches. Basically: If in doubt, put one in. One of the rare places where a semicolon will cause problems is before an "else". Even after many years of programming, I will usually write what we saw a moment ago as follows. The only difference is the addition of the little comment.
if sTmp<>'none found'
   then label1.caption:=sTmp //no ; here
   else label1.caption:='Not known';

Please don't think I'm crazy! I did not present the application above as something that you would want to use for database work. It is valuable, I hope, for helping you get a few more programming ideas into your head.
Just before we leave this little application, here's another example of making it work better, regardless of that users do. It will also show you something about making your application elegant.

If a user typed "MADRID" or "madrid" into our current application, it would respond "Not Known".

It could crudely fixed by adding....
if edit1.text='MADRID' then sTmp:='Spain';
if edit1.text='madrid' then sTmp:='Spain';
... but even that would fail if the user typed "MAdrid". (You could argue that the user should learn to type accurately!)

A better answer would be to revise the Madrid test so that the only Madrid line reads....
if uppercase(edit1.text)='MADRID' then sTmp:='Spain';
When the code executes, the computer looks at what is in edit1.text, as it did before. However, before asking if it equals 'MADRID', the computer, because of the "uppercase", converts any letters in the contents of edit1.text to uppercase if they are lowercase. (I.e. makes "capitals" out of "small" letters.). "Uppercase" is built into Lazarus/ Delphi, and is always available to you.

Now, you could go through the program, changing each "if" line the way the Madrid line was changed. And if you did, when the code executed, the contents of edit1.text would be converted to upper case over and over again. It would work... but be inelegant. The following is nicer:
procedure Tdatab1f1.Button1Click(Sender: TObject);
var sTmp,sToMatch:string; //modified 1
begin
sTmp:='Not Found';
sToMatch:=uppercase(edit1.text); //new 2
if sToMatch='MADRID' then sTmp:='Spain'; //modified
if sToMatch='ROME' then sTmp:='Italy'; //modified
if sToMatch='LIMA' then sTmp:='Peru'; //modified
if sToMatch='MOSCOW' then sTmp:='Russia'; //modified
if sToMatch='NEW YORK' then sTmp:='USA'; //modified
if sTmp<>'Not Found'
   then label1.caption:=sTmp
   else label1.caption:='Not known';
end;
Get that working. Figure everything out. Then read on.

The "if" test, and the "else" line are not actually necessary! Make that bit of the program look like...
... if sToMatch='MOSCOW' then sTmp:='Russia'; //modified
if sToMatch='NEW YORK' then sTmp:='USA'; //modified
label1.caption:=sTmp;
end;
Note: You haven't "added" a semicolon after the "label1.caption:=sTmp;"

That semicolon is just the one that was already after the "...else label1.caption:='Not known';". We deleted most of that.. but not the semicolon!







Search just this site without using forms,
Or... again to search just this site, use...

Powered by FreeFind

Site search Web search

The search engine merely looks for the words you type, so....
  *!  Spell them properly   !*
  Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I", "get" and "rich".



I have other sites...
   SheepdogSoftware site.
   My site at Arunet.


Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. they don't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated.
Click here to visit editor's Sheepdog Software freeware, shareware page.
Link to Tutorials main page Link to Lazarus/ Delphi Course index

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "datab1.htm".

Click for W3.org HTML validity test Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Click to check CSS validity


One final suggestion: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .