Académique Documents
Professionnel Documents
Culture Documents
Self-training
Windows®/Mac OS®
The software and the manual are copyrighted and may not be reproduced in whole or in part except for the
personal licensee’s use and solely in accordance with the contractual terms. This includes copying the elec-
tronic media, archiving, or using the software in any manner other than that provided for in the Software
license Agreement.
4D, 4D Draw, 4D Write, 4D View, 4th Dimension®, 4D Server and the 4th Dimension and 4D logos are reg-
istered trademarks of 4D SAS.
Windows, Windows XP, Windows Vista and Microsoft are registered trademarks of Microsoft Corporation.
Apple, Macintosh, QuickTime, and Mac OS are trademarks or registered trademarks of Apple Computer Inc.
Mac2Win Software is a product of Altura Software, Inc.
ICU Copyright © 1995-2008 International Business Machines Corporation and others. All rights reserved.
This product includes software developed by the Apache Software Foundation (http://www.apache.org/).
4D includes cryptographic software written by Eric Young (eay@cryptsoft.com)
4D includes software written by Tim Hudson (tjh@cryptsoft.com).
Spellchecker © Copyright SYNAPSE Développement, Toulouse, France, 1994-2008.
ACROBAT © Copyright 1987-2008, Secret Commercial Adobe Systems Inc. All rights reserved. ACROBAT is a
registered trademark of Adobe Systems Inc.
All other referenced trade names are trademarks, registered trademarks, or copyrights of their respective
holders.
Contents
Welcome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Fundamentals . . . . . . . . . . . . . . . . . . . . . . . 11
Sample Presentation Overview . . . . . . . . . . . . . . . . . 13
Chapter 4 Menubar . . . . . . . . . . . . . . . . . . . . 35
Getting it Done . . . . . . . . . . . . . . . . . . . . . . . . . 35
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4D v11 - Self-training 3
Contents
Chapter 9 Relations . . . . . . . . . . . . . . . . . . . . . 61
Getting it Done . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Chapter 11 Importing . . . . . . . . . . . . . . . . . . . . 73
Getting it Done . . . . . . . . . . . . . . . . . . . . . . . . . 73
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4 4D v11 - Self-training
Contents
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4D v11 - Self-training 5
Contents
6 4D v11 - Self-training
Contents
4D v11 - Self-training 7
Contents
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Going Further . . . . . . . . . . . . . . . . . . . . . . . . . 262
8 4D v11 - Self-training
PRESENTATION
Welcome
I’m Prof4D, your personal assistant; I’ll be your guide throughout our
time together during this guide.
What do you say we take a quick tour of some of the things you can do
with 4D? Of course, I won’t get in to everything, but by the time we
get to the end of this guide, you’ll know everything you need to know
to create working databases that are both ergonomic and efficient!
1) Introduction table
This table that introduces the chapter contains:
- the goal(s) for the chapter
- the estimated amount of time needed to complete the
exercise
- the list of elements used in the exercise
2) Getting it Done
3) Exercises
Additional exercises that add to the lesson and make sure that
you’ve mastered the main concepts.
The keys for these exercises are found in the Key chapter at the
end of the guide.
4D v11 - Self-training 9
Welcome
PRESENTATION
4) Comments
General commentary related to the chapter. If you wish to
learn even more about a given topic, the “Going further”
section provides additional information or sources to study.
You can also navigate the guide using the chapter themes.
Theme
I’ll be there to help you every step of the way... after all, I need to work
a bit, too! :-)
In the interest of efficiency, at the start of each chapter, you can either
use the database that you completed in the previous chapter or the
sample database that is provided. That way, we can work using
identical databases. The sample databases are located in the Selftraining
Database folder.
Your goal (mine too!) is to get right to the essential information and
understand 4D’s global architecture without entering in to too much
detail. If we were to do that, this would be an encyclopedia and not a
quickstart guide!
At the end of this guide, you’ll find the exercise keys as well as useful
information to complete your training.
10 4D v11 - Self-training
The Fundamentals
PRESENTATION
THE FUNDAMENTALS
Let’s starting discovering 4D right now!
4D v11 - Self-training 11
Welcome
PRESENTATION
On the other hand, you application must be able to offer the following
functionalities:
■ Add, Edit, Delete
■ Query, Order by
■ Print
■ Import, Export
■ Send e-mail
■ Access the Web (Web publishing)
■ Exchange data (XML, Web Services, etc.) and communicate (TCP,
FTP, etc.)
■ Interact with other databases (ODBC, Oracle, MySQL, etc.)
■ ...
■ Store data
■ Some programming
We will touch upon these three points through the different lessons.
12 4D v11 - Self-training
Sample Presentation Overview
PRESENTATION
SAMPLE PRESENTATION OVERVIEW
Throughout this guide, we will strive to create a database that responds
to specific needs (such as the kinds you will likely come across at some
point as a 4D developer).
The following steps, made up of different lessons, will help you find
the solution.
Remember: This guide was not designed to cover every aspect and
feature of 4D, nor is it an analysis guide (Merise, UML, etc.). We’re
trying to stay pragmatic and efficient.
4D v11 - Self-training 13
Welcome
PRESENTATION
Here’s one last piece of information from the client - this layout
designed by their graphic artists after several meetings. We have icons
that were created according to our thumbnails (we’ll see the detail a
little further).
14 4D v11 - Self-training
GETTING STARTED
1 Taking your First Steps
with 4D
• Understanding 4D
Objective
• Discovering the interface and architecture
Estimated time 10 minutes
4D Standard Edition
What we’ll use Navigating menus
Using a sample database
GETTING IT DONE
You just downloaded and installed your software and now you want to
get up and running as fast as possible.
If you haven’t yet registered your product (or if you don’t have a
product number), you can click “I would like to evaluate 4D”. This
4D v11 - Self-training 15
GETTING STARTED
allows you to enter your product number at a later time (you can find
out more about this in the 4D Installation Guide).
The first four menus from the left on the menu bar (File, Edit, Mode,
Run) let you manage every aspect of development.
The two menus to the right (Records and Tools) let you quickly test
your application.
Two additional menus appear to the right of these menus depending
on where you’re working in 4D (form, method, tools, etc.).
You’ll frequently use the 4D toolbar when working with your project.
Like with the menus, the buttons to the left of the Find feature are for
development purposes while the buttons to the right are for usage
(working with data).
16 4D v11 - Self-training
GETTING STARTED
Getting it Done
Like I already mentioned, the sample that we’ll use throughout this
guide concerns setting up an application that will manage an asset
management park. Whew! We lucked out — we won’t have to worry
about handling the bills! :)
Before we learn how to do that, let’s look at what our project will look
like once our work is complete. You’ll love how easy it is to get started
- not to mention all the feature!
2 In the toolbar, click the Open menu and select
MyITManager_36_key.4dbase" in the "Selftraining Database" folder.
Once opened, the database is displayed and shows you the contents of
the Technicians table. Each and every table can contain billions of
records... are you ready to have a go?
Let’s take a look at the features that we’ll use in the database, starting
with the main navigation screen.
Each of these tables are displayed as a list. You simply need to double-
click a list row to display the detail screen in the same window (this is
the detailed content of a single record.)
When you double-click a list row, the DETAIL record appears along
with navigation buttons.
4D v11 - Self-training 17
GETTING STARTED
You can click the title of list headers; the header also allows you to
perform a search on or sort column data.
You can re-open this database at any time to find a feature
(expected or requested) in the exercises.
TIP
18 4D v11 - Self-training
GETTING STARTED
Exercise
EXERCISE
Spend some time browsing within the database to discover the primary
features (help bubbles, entry aids, parameters, preferences, Web access,
etc.)
4D v11 - Self-training 19
GETTING STARTED
COMMENTS
This database mirrors what you will get at the end of this guide. Of
course, you can always add more features to your database (for
example, ergonomics, programming, etc.) to create a professional
application.
I recommend that you check out the other sample databases as well as
the website www.4d.com to discover the many features and solutions
imaginable that were put created by ingenious developers just like us!
...and for those who need specific features or commands that aren’t
available in 4D or in plug-ins, you can use the SDK Plugin (free!) that
allows you to create projects in C++ in the wink of an eye!
20 4D v11 - Self-training
INTERFACE
2 Forms and Buttons
Objective Setup and test the first interface elements (buttons) on a form
Estimated time 10 minutes
What we’ll use Forms, Buttons, Object properties
GETTING IT DONE
To get you quickly up to speed, let’s check out the navigational layout.
4D v11 - Self-training 21
Forms and Buttons
INTERFACE
See, we’re going to stick with a simple structure that is amply sufficient
for getting to know 4D.
In the toolbar:
1 Click Explorer .
2 Click Forms.
3 Select “Project Forms” and click the button at the bottom of the
Explorer.
4 Enter “Navigation” in the Form Name box.
5 Click OK.
22 4D v11 - Self-training
Getting it Done
INTERFACE
An empty form appears:
The tools on the left allow adding objects to the form (buttons,
variables, fields, arrays, texts, plug-ins, etc.)
The tools at the top of the form let you work with objects (group,
align, move) and to manage display options within the form (zoom,
pages, views, etc.)
The triangle to the right of a tool indicates that several options are
available for the tool.
List.
4D v11 - Self-training 23
Forms and Buttons
INTERFACE
To test your creation, click the green triangle in the upper left of the
form window. 4D displays the form exactly how the user will see it.
You’ll notice that the window has automatically set its size to that of
the form. Resizing is done automatically and will help you save time
during both development and maintenance (of course, this feature can
be disabled if desired).
8 Click Finished to go back to the Design environment.
24 4D v11 - Self-training
Getting it Done
INTERFACE
2 Select “Object Method”:
4D v11 - Self-training 25
Forms and Buttons
INTERFACE
As you can see, getting started with creating forms is easy! By now, you
should be completely independent and ready to tackle some exercises!
:=)
EXERCISE
Let’s create a second button designed to display the list of “Help
Requests”.
Then finish the form so that it looks like the following example:
26 4D v11 - Self-training
Comments
INTERFACE
COMMENTS
When you copy a button (copy-paste), you are also copying its set of
properties as well as the associated method. Note: Only the object
name is different because an object name is unique to each form.
I generally recommend that you polish the initial object and then copy
it so that you only have to change a few necessary parameters.
4D v11 - Self-training 27
Forms and Buttons
INTERFACE
28 4D v11 - Self-training
PROGRAMMING
3 Startup Display
GETTING IT DONE
Before we move forward, let’s take a look at the basic syntax rules and
primary operators used in 4D.
4D v11 - Self-training 29
PROGRAMMING
Startup Display
|
This character is
accessible using:
OR • Shift-\ on If((it_rains) | (it_snows))
Windows
• Alt-Shift-L on
Mac OS
# used only during
EXCEPT
searches
Add a field or variable
Assign := vName:="Napoléon"
value
Addition + vTotal:=vSubtotal+vTax
Subtraction - vRevenues:=vSalaries-vWitholdings-vTax
Operators for Multiplication * vTotal:=vNumHours*vHourlyRate
numbers Division / vLength:=vSurface/vWidth
Exponential ^ vKiloByte:=2^10
Parentheses () vTotal:=(5+3*(2-7)-((4*5)/(2+4)))
Concatenation + vName:="Mickey"+" "+"Mouse"
Multiplication * vTest:="x"*4 (we get "xxxx")
Operators for strings
String index [[ ]] (Windows) and vName[[1]]:=Uppercase(vName [[1]])
≤ ≥ (Mac OS)
VNumDays:=!06/05/2007! -
Operators for dates Addition + or -
!22/04/2007!
30 4D v11 - Self-training
PROGRAMMING
Getting it Done
The first row creates a container the size of the Navigation form, taking
in to consideration the properties we defined.
The third line is executed when you shut the window by clicking
Finished. It shuts the container.
4D v11 - Self-training 31
PROGRAMMING
Startup Display
32 4D v11 - Self-training
PROGRAMMING
Getting it Done
To test it:
1 Select “Open Recent Databases” in the File menu.
2 Select the name of your database.
The database closes and then re-opens. You must see the form in a
multi-user window.
Great job! This step is completed, we will now carry on and improve
the look and feel of our form... it certainly needs it! :=)
4D v11 - Self-training 33
PROGRAMMING
Startup Display
EXERCISE
Set up a system that allows you to display the message “See you later”
when you close a database.
Tip: This exercise requires the use of the “On Exit” database method.
COMMENTS
A project method can be called by any other method (object, form,
database, project, trigger, plug-in, menu rows, etc.). A method can call
itself, in which case it becomes a recursive method.
34 4D v11 - Self-training
INTERFACE
4 Menubar
Even though the interface is really geared towards clicking buttons, the
menu bars are still an efficient tool that your users are used to using.
In 4D, you can create up to 32,000 menu bars. You can name any
menu bar so that its easily located.
GETTING IT DONE
Now that the set works, we will create a menu bar and call this method
from a menu row.
1 Select Tool Box from the toolbar.
4D v11 - Self-training 35
Menubar
INTERFACE
2 Select Menus.
This window first displays the list of all menu bars; when you click one
of the menu bars, its set of menus appears. You can then display each
row for that menu. The right side if the screen displays information
concerning the selected elements.
3 Rename Bar #1 as “Navigation” (by double-clicking its name in the
list).
36 4D v11 - Self-training
Getting it Done
INTERFACE
4 Display the contextual button by right-clicking the Navigation bar.
Choose “Attach a menu to the menu bar Navigation” then “New
menu”.
To associated this new menu with the Navigation bar, you can also do
the following:
1 Click the Navigation bar.
2 In the right-hand column containing the current menus for the bar,
display the contextual menu (right-click).
3 Select “Attach a menu to the Navigation bar”.
4 Click the Tools menu, it is automatically attached.
To test this new way of accessing items in your project, select Test
Application in the Run menu. You will see your menu appear and can
test the efficiency of the row you created.
4D v11 - Self-training 37
Menubar
INTERFACE
EXERCISE
In the next lessons, you can add a row of menus in the Tools menu for
each specific feature.
COMMENTS
You can enable/disable menu rows depending on the user’s actions.
Perhaps you noticed that the standard menus call a resource called
:xliff:name. This indicates that the menu title comes from the
parameters file named CommonEN.xlf located in the
/Contents/Resources/English.lproj folder.
This allows you to better understand the interaction that can exist
between a resource file (an XML file, in this case) and your database.
This is particularly relevant when creating multilingual applications.
38 4D v11 - Self-training
INTERFACE
5 Page 0 and Page 1
GETTING IT DONE
4D forms allow multiple pages per form (up to 32,767 to be exact!),
which allows us to spread information across a form either by theme or
function and be able to access a specific page according to our needs.
You can browse pages using tabs, buttons, etc.
For example, all the buttons to the left and at the top in the final
version of the sample database are found on page 0.
To browse from page to page, or to add another page, you can use the
left and right buttons in the tools at the top of the form window or use
4D v11 - Self-training 39
Page 0 and Page 1
INTERFACE
the pop-up menu between the two arrows. This allows you to access
existing pages.
To move objects from one page to another, simply cut the object and
copy it on to the desired page.
1 Move the set of buttons on your form to page 0.
The page indicator becomes:
We will now replace all of these buttons with a more, let’s say “system”
look and feel. Don’t worry, these buttons are completely functional,
they’re just not aesthetically up to par for a rather modern-looking
interface.
2 Double-click Finish to display its properties.
Let’s take a moment to give the button a variable name that’s slightly
more exciting. For example, let’s say “B_End”.
3 Click Button in the “Type” property (first in the list) and select Picture
Button.
40 4D v11 - Self-training
Getting it Done
INTERFACE
The button changes to:
If you take a look at the other properties, you’ll see that just below the
name, the picture related properties (the source and reference number)
are available:
At this point, there aren’t any pictures in the Picture Library. We are
unable to indicate the number of picture name.
4D v11 - Self-training 41
Page 0 and Page 1
INTERFACE
5 Set a name for the picture (its number and size are automatically set,
but can be modified at this point). You should also indicate that the
picture is composed of four pictures (1 column on 4 rows):
42 4D v11 - Self-training
Getting it Done
INTERFACE
6 Memorize the picture number and size (48x48); click OK.
Your first picture is imported. You can now shut the Tool Box and go
back to your form and indicate the source and the picture number that
the button should use.
■ Switch when Roll Over, Switch back when Released, and Use Last
Frame when Disabled:
4D v11 - Self-training 43
Page 0 and Page 1
INTERFACE
ADVICE
pictures, etc.) in your 4D database can be used in any new
database via transfer. You won’t need to recreate pictures,
buttons, and other elements for each new database.
EXERCISE
■ Repeat these procedures for each button on your form.
■ Add functional buttons (find, order by, etc.) like in the provided sam-
ple.
It gathers the different ways of accessing the left-hand tables and the
functional buttons (add, print, delete, etc.) at the top of the form.
44 4D v11 - Self-training
Comments
INTERFACE
Don’t add any programming to these latest buttons, we will do it when
they are used.
Note: All the buttons in the left hand colum (from technicians to
parameters) remain are 1 row x 1 column. The checkboxes to select are:
All buttons across the top (from Finish to Delete) are cut in groups of 4
rows x 1 column and use the following settings:
■ Create text for these buttons. They will be set later on to take the selec-
ted language into account.
COMMENTS
It can be tempting (and why not, it’s usually a lot faster) to paste a
picture in several forms rather than to include it in the Picture Library.
Keep in mind that any change to a picture in the library is instantly
made to the entire database and only absorbs the size of the picture a
single time.
4D v11 - Self-training 45
Page 0 and Page 1
INTERFACE
For example, a picture file is 8kb and we’re going to use 10 pictures of
the same size. That brings us to 80kb, and if we use it in 50 forms,
we’re quickly at 4MB that we really don’t need to add to the structure.
Now that you understand how pages work, you can start to have a
think about basic programming. Be sure to take in to account the
page on which the user is working when adapting, for example,
Going further
menus, as we saw in the previous chapter.In the following chapters,
we’ll take this one step further and discuss the concept of inheriting
forms.
46 4D v11 - Self-training
INTERFACE
6 Navigation and Form
Pages
GETTING IT DONE
We now have a database that is aesthetically correct!
To create pages:
1 Display the “Navigation” Project form.
2 Select “Add Page” in the Form menu.
4D v11 - Self-training 47
Navigation and Form Pages
INTERFACE
4 Repeat the operation so that your form has 7 pages (8 total if you’re
counting page 0).
You can also click on the right arrow in the navigation
panel .
TIP
Now we need to indicate for each button which form page to display.
1 Open the method for the Technicians button.
2 Delete its content.
3 Enter the following statement:
GOTO PAGE(1)
48 4D v11 - Self-training
Exercise
INTERFACE
EXERCISE
■ Repeat this procedure for each of the buttons by modifying the page
number.
■ To verify that pages have changed, add a different text or drawing (or a
different location) on each page.
COMMENTS
Instead of managing the same line of code in each button, you can call
a method and pass the page number to display as a parameter. This is a
lot easier to maintain and also allows conditioning the page change
depending on the selected criteria.
You can also manage more pages than visible buttons and keep certain
information “hidden” so that it is only available under certain
conditions.
4D v11 - Self-training 49
Navigation and Form Pages
INTERFACE
50 4D v11 - Self-training
DESIGN
7 Tables and Fields
GETTING IT DONE
To keep data from one session for another, or to share data among
several users, we can store it in:
■ a table
■ a text file
■ an XML file
■ resources
■ somewhere else (another database)
Let’s take a look at storing data in a table. To do that, we’ll need to:
1. Create the table
2. Create fields
3. Create at least one entry/visualization form
❿ To create a table:
1 Click the Structure icon .
4D v11 - Self-training 51
Tables and Fields
DESIGN
❿ To add fields, there are several ways of going about it. Let’s take the
quickest way:
1 Double-click the first row of the table.
2 Enter the name of the first field (don’t change the type, we’ll come
back to this later.)
3 Press the Enter key, a new field is created.
52 4D v11 - Self-training
Getting it Done
DESIGN
4 Repeat as necessary for all the fields to create.
■ You can come back to each field and select the desire type to the right
of the field name.
■ You can also double-click fields to display the Inspector and change its
properties in detail.
The help tip is displayed when the user mouses over the field. The
comments allow specifying any information that you feel is useful
(ideas, field information, etc.)
4D v11 - Self-training 53
Tables and Fields
DESIGN
In the following chapter, we’ll create forms and start data input.
EXERCISE
Create the “Material” and “Help Request” tables in the same way using
the below model:
COMMENTS
To modify the type for multiple fields:
■ Select multiple contiguous (using the Shift key) or several non-con-
tiguous fields (using the Ctrl key in Windows or Cmd for MacOS)
fields
■ Changing the type of one of these fields => all selected fields are
modified
To move a field:
■ Hold the Alt key (the cursor changes in to a hand)
■ Drag the field to its new location
54 4D v11 - Self-training
Comments
DESIGN
There are several solutions available to you for adding tables and fields:
■ Right-click (within the structure to add a table, in the table to add a
field)
■ File -> New menu
■ The New button in the toolbar
■ The Add button in the structure window
■ Data import
■ Getting an existing database
text files (txt, csv, dbf, etc.) I recommend that you consult
4D documentation for more information (importing data).
4D v11 - Self-training 55
Tables and Fields
DESIGN
56 4D v11 - Self-training
USAGE
8 Entering and Modifying
Records
We’re now going to add input forms in the Navigation form created in
the previous chapters.
GETTING IT DONE
For this first input, we will use standard forms automatically created by
4D:
1 Choose “Display the current table” in the Records menu.
4D asks if you want to create forms.
2 Click “Yes for All”.
4D v11 - Self-training 57
Entering and Modifying Records
USAGE
■ the OUTPUT form, which presents the list of records for the selec-
tion.
3 Choose the TECHNICIANS table in the list of tables.
To go from one field to another, you can use the Tab key.
58 4D v11 - Self-training
Getting it Done
USAGE
To add a value to the picture field, you have three options:
■ Copy a picture and paste it in the field.
■ Drag the picture from another application or another system win-
dow.
■ Use the import feature in the picture’s contextual menu.
6 To save the file, click the OK button.
4D asks, by default, if you want to create a new record.
7 To stop and return to the list, click the cancel button.
Did you notice during these inputs that a number of elements can be
improved (default values, input filters for dates, list of suggested
values, etc.)?
We’ll discuss these three options coming up in the next chapter. We’ll
first start with setting up relations.
4D v11 - Self-training 59
Entering and Modifying Records
USAGE
EXERCISE
Finish any remaining input in the TECHNICIANS table from the list of
provided information.
COMMENTS
You have four options at your disposal for creating a new record:
■ Clicking the Add button => the input is done in the list.
■ Choose New Record in List in the Records menu => input is also done
in the list.
■ Choose New record in the Records menu => input is done through the
input form (detailed content of a single record.)
■ Double-clicking an empty row to display a new record in the input
form.
60 4D v11 - Self-training
STRUCTURE
9 Relations
GETTING IT DONE
4D is a relational database. The relation between tables is created using
relations between the FOREIGN KEY and the PRIMARY KEY.
Tracing a relation is simple: Click a field (Foreign Key = start) and drag
it to another field (Primary key = finish).
4D v11 - Self-training 61
Relations
STRUCTURE
Let’s talk for a little bit about billing - I know you’ll love that! :=)) You
can trace relations:
■ from the Invoice rows table to Invoices (X invoice rows for 1
invoice)
■ from the Invoice table to Customers (X invoices for 1 customer)
■ from the Invoice rows table to Products (X invoice rows for 1 prod-
uct)
You can associate as many roles as you want to a film, and even
represent the different roles a single actor might play in the same film
(for example, Eddy Murphy in the Nutty Professor).
Now that you’ve mastered relations, in what direction will you trace
the relation between “Technicians” and “Help requests”? Or between
“Help requests” and “Materials”?
62 4D v11 - Self-training
Getting it Done
STRUCTURE
These are basically the questions you should be asking yourself:
■ Can a single technical perform several interventions?
■ Can an intervention concern multiple technicians?
This subject could be debated for hours and hours using many
arguments and examples. This is where careful analysis and working in
the field with users really becomes important. In this exercise, we’ll
make the following arbitrary choices:
■ an intervention is handled by a single technician,
■ an intervention only concerns a single piece of material,
■ a piece of material will have an associated set of peripherals, soft-
ware, and networks.
4D v11 - Self-training 63
Relations
STRUCTURE
To create a relation:
2 Trace the relation between the “ID” field in the HELP REQUESTS
table and the “ID” field in the TECHNICIANS table.
64 4D v11 - Self-training
Getting it Done
STRUCTURE
This allows managing a portion of referential integrity; we’ll get back
to this point a little later on.
We’ll come back and discuss the other Inspector parameters for
relations a little later.
4D v11 - Self-training 65
Relations
STRUCTURE
EXERCISE
Create relations between other tables and apply settings. Don’t hesitate
to add fields if any are missing.
See key on page 231. Note that in the key, we moved the fields so
that the structure is linear.
COMMENTS
To complete relation automation, you can use the integrity manager in
the relation inspector. It allows you to set how 4D should behave
when deleting a table record containing the primary key:
■ Leave related many intact: This implies management is done pro-
grammatically; 4D performs no integrity verification.
■ Delete related many: If you delete a Technician you’re deleting all
dependant records in table X (starting point for the relation). In our
case, this would be all help requests (interventions).
■ Cannot delete if related many: Even though records for table X are
related to the primary key, we’re not allowed to delete the record. In
other words, we can’t get rid of a technician who hasn’t performed
an intervention.
66 4D v11 - Self-training
USAGE
10 Input and Deletion
• Inputting data
Objective
• Deleting data
Estimated time 10 minutes
What we’ll use Managing data mode, using relations, deletion
GETTING IT DONE
In previous chapters, we created records for Technicians as well as a
few records for Software.
4D v11 - Self-training 67
Input and Deletion
USAGE
This control is due to the checkbox “Prompt if related one does not
exist” that we selected previously. It lets you manage database integrity
during input and avoiding the presence of orphaned files.
In case the code doesn’t work, click Try Again and re-enter an existing
code.
We can still use the relation to simply input. After all, we don’t know
the identifier of the records in each of the tables. In that case, simply
type the first letter(s) of the identifier using the @ character to display a
list of all records starting with that letter.
You can also start entry using the @ symbol; for example, you can type
@m. This will display any technician with the letter ‘m’ in their name.
When the results are returned on your screen, the text that is in
68 4D v11 - Self-training
Getting it Done
USAGE
parentheses is the “Wildcard choice” field selected in the relation
properties.
With relations, you can use the at sign (@) anywhere in the
foreign key to displays primary key records. For example:
TIP
Deleting records also depends on the settings for the relation. In this
case, the One to many relation is taken into account.
If the one to many relation is manual 4D doesn’t perform any check and deletes it.
If the one to many relation is automatic 4D acts depending on the integrity check parameters.
If Leave Related Many Intact is
4D doesn’t perform any check and deletes it.
selected
If Delete Related Many is
4D deletes the technician and his services.
selected
If Cannot Delete if Related 4D refuses to delete the Technician record and doesn’t delete
Many is selected any services performed by the technician.
4D v11 - Self-training 69
Input and Deletion
USAGE
Learning how to set parameters for links depends on how they are to
be used. Let’s take a look at the software -> material relation. Should
we delete the Software records when a piece of material is used (sale,
damaged, end of lease, etc.)?
In theory, software is purchased separately from a computer (with the
exception of OEM software), and it can be re-attributed to another
piece of equipment. For that reason, deleting software when deleting a
piece of material seems inappropriate.
❿ To delete a record:
1 Double-click the Technician to delete.
The technician is displayed in an input form.
2 Click the trash bin to delete the record.
EXERCISE
Set parameters for all relations using the following information:
Cascading records:
■ Create a piece of software and assign it a piece of material that
hasn’t been recorded yet => creation of a Material record
■ Create a help request for a non-existing piece of material (it’s not
yet in the database) => creation of a Material record. This is often
the case when a piece of material is installed for the first time.
■ Set a non-existing Technician for the Help request record => cre-
ation of a Technician record.
■ Validate each of these records (they are held in memory).
70 4D v11 - Self-training
Comments
USAGE
Did you notice how we were able to create records in four tables and
ensure the integrity of data without having to program? This is an
important lesson when working with 10 cascading tables.
If you select the Cannot delete if related many... checkbox for certain
relations, you must start by deleting records in Many table (relation
starting point) and work your way upward.
On the other hand, if all relations have the Delete related many option
selected, deleting a piece of material deletes the records in all related
tables (Help requests, Software, etc.)
COMMENTS
There’s a debate raging about whether to enable or disable automatic
relations. As you can imagine, there are several varying schools of
thoughts on the topic. The more choice you have, the more questions
we tend to ask! :=))
4D v11 - Self-training 71
Input and Deletion
USAGE
72 4D v11 - Self-training
USAGE
11 Importing
• Import data
Objective
• Automatically create tables
Estimated time 10 minutes
What we’ll use Data manipulation, importing with or without creating tables
GETTING IT DONE
To save time, and to prepare for getting previously inputted
information, we will learn about importing data and automatically
creating tables from a tabbed file.
❿ To import data:
1 In the File menu, select “Import” and then “From File...”
2 Select the Technicians.txt file in the “Selftraining Database” folder,
“Import_Files” subfolder.
The Setting dialog box appears.
3 Select the Technicians table in the list.
4 Match each field with the column to which it will be imported.
5 Click Import.
The data is added to any previously existing records.
4D v11 - Self-training 73
Importing
USAGE
The import feature also allows creating tables directly during import.
This function is critical when transferring data from other databases or,
as is often the case, from data stored in a spreadsheet.
If the file that you are importing contains column headers (which I
recommend using), go on to the next step.
5 Click the second settings tab.
74 4D v11 - Self-training
Exercise
USAGE
6 Select the checkbox “Column Titles as Field Name”.
Afterwards, whether you selected this option or not, you can define
the type of fields:
7 Click each of the column headers and indicate the desired field type.
8 Click Import.
Once the import is complete, 4D asks you if you want to create forms
for the new table. Not a bad idea, if I say so myself! Go ahead and click
Yes.
EXERCISE
Import all of the files available in the Import_files subfolder in to each
table.
COMMENTS
■ When importing Boolean values (true-false in 4D), you can request
automatic conversion by going to the last tab in the Import dialog box
and selecting “yes;no”, “internal;external”, “man;woman”, etc. 4D
automatically converts the imported data (the first value as True and
the second as False).
4D v11 - Self-training 75
Importing
USAGE
■ You can also offer your users access to the Import or Export dialog
boxes by using the commands:
IMPORT DATA("")
EXPORT DATA("")
Using the Import dialog box, you can also import your data using a
form. This form contains the fields that should receive the data,
they must be arranged in the same order as the import file
columns. You can then program the form method and object
Going further
methods to perform automatic checks, reformatting, automatic
population of calculated fields, etc. This is exactly like if the user
entered the data in the form. You can also created programmed
imports using the Open document and GET PACKET commands.
76 4D v11 - Self-training
INTERFACE
12 Output forms
GETTING IT DONE
Now that inputting no longer hold any secrets from you, we’ll use the
Form creation wizard to define forms that we’ll integrate with the
navigation interface.
These forms will be list presentations like you saw in the sample
database at the beginning of this guide.
4D v11 - Self-training 77
Output forms
INTERFACE
2 Select New Form using Form Wizard from the options menu:
78 4D v11 - Self-training
Getting it Done
INTERFACE
8 Delete the field(s) that you do not wish to display by selecting them
and clicking .
4D v11 - Self-training 79
Output forms
INTERFACE
80 4D v11 - Self-training
Getting it Done
INTERFACE
You may notice the presence of yellow markers:
These markers note the position of each area of the form. For example,
objects place between the header and body are displayed once for each
selection record while those placed between the top of the form and
the header are displayed once they are at the top of the form.
4D v11 - Self-training 81
Output forms
INTERFACE
The form that you dragged automatically integrates into the displays
form, similar to the example below:
By default, the form can be resized freely. For display aesthetics and
usability, it’s better to force the width and height.
8 Display the form properties (right-click the background of the form
and not on an object.)
9 Set the form as shown below:
82 4D v11 - Self-training
Exercise
INTERFACE
You now know how to display the fields of different tables. However,
for the moment, no record appears even though you imported data
into each table in the previous chapter.
EXERCISE
For each table, create the display form as a list form and then integrate
it into the Navigation form.
Also thinkabout copying the blue pointer, indicating which page you
are on.
COMMENTS
If you want to modify the template you created, you can do so as
follows:
■ Create a new form using the template to modify
■ Click the Advanced button to make the desired changes.
Go up to where your new form is created - the last dialog box asks you
to either create a new template or to modify the template that you just
used. Choose this second option.
4D v11 - Self-training 83
Output forms
INTERFACE
84 4D v11 - Self-training
USAGE
13 Query and Order by
• Query and sort records using criteria from one or more tables
Objective
• Finish the use of automatic relations
Estimated time 10 minutes
“Manipulating data” environment, Query and Order by editor,
What we’ll use
programming
GETTING IT DONE
There are a series of buttons at the top of the Navigation form that we
added in the previous chapter. We’re now going to program them
taking into consideration the displayed list, in other words, the current
page with which we’re working.
We must use the QUERY command that uses the name of the relevant
table as a parameter. That’s all well and good, but which table is
displayed at any given moment?
4D v11 - Self-training 85
Query and Order by
USAGE
Note: The “Current form page” function returns the number of the
displayed page.
1 Save the method (File -> Save object method...)
2 Execute your form to see if the button works.
3 Put your mouse over the Technicians list.
4 Click Find.
The following Query screen appears. The fields match up with the
Technicians table... Great job! We’re on the right track!
❿ To perform a Query:
1 Click a field then a comparator, then type the value to query.
86 4D v11 - Self-training
Getting it Done
USAGE
The query argument gradually finishes:
To use multiple query criteria, you must set parameters in the first line
of the argument and then click Add. You can carry on and modify the
next rows of the argument using any of the available conjunctions:
You can also perform a query and then perform a second query using
the results of the first selection:
1 Start the second query in the same way you performed the first query.
2 Start the query by clicking Query in Selection.
Order By The second goal of this chapter is to learn how to order by (or sort) a
selection. Like with the query button, we need to adapt the order by to
work with the displayed page. You can start exactly like we did with
the query button.
1 Display the Navigation form.
4D v11 - Self-training 87
Query and Order by
USAGE
88 4D v11 - Self-training
Exercise
USAGE
EXERCISE
Modify the code for the two buttons so that they work for all lists in
the Navigation form.
COMMENTS
As you undoubtedly have guessed by now, you can optimize the
procedure for creating these methods. In the course of the next few
chapters, we will learn how to use generic programming, passing
parameters between methods, and the use of pointers to make
reference to tables, fields, etc.
4D v11 - Self-training 89
Query and Order by
USAGE
90 4D v11 - Self-training
USAGE
14 Quick Reports and
Exports
The interest of using a database isn’t in your ability to enter data, but
rather your ability to get to data.
Getting data is, for now, done using pre-defined forms on screen (via
the programmer). In a number of cases, your users would want to
adapt data extraction to specific needs that aren’t available in a
functional specification or need to be very precise.
GETTING IT DONE
The Quick Report editor allows presenting data from the current
selection, in other words, the result of a previously performed query1.
1. The final Boolean settings for the command allows you to bypass the pre-
vious query.
4D v11 - Self-training 91
Quick Reports and Exports
USAGE
2 3 4
92 4D v11 - Self-training
Getting it Done
USAGE
■ 2- The bottom area that allows choosing the table, performing a
query, set the quick report type or launch the wizard (the magi-
cian’s hat on the right)
■ 3- The column of fields of the selected table
■ 4- The column of requested order by procedures.
If you’re working with Mac OS, the preview displays as a PDF in the
system tool Preview.
4D v11 - Self-training 93
Quick Reports and Exports
USAGE
In the row for the subtotal, you can add summary calculations by
right-clicking the intersection of the column and the subtotal row.
94 4D v11 - Self-training
Getting it Done
USAGE
To distinguish between the detail lines (each record) and summary
lines, you can apply styles, such as bold or italics, and apply spaces
between the summary and subsequent lines:
4D v11 - Self-training 95
Quick Reports and Exports
USAGE
96 4D v11 - Self-training
Getting it Done
USAGE
You can also display only synthetic lines by “hiding” the body (the
detail of each record):
The hidden body is materialized by a gray grid over the entire row:
4D v11 - Self-training 97
Quick Reports and Exports
USAGE
Very nice, but it doesn’t say a whole lot! :=)) That’s because we didn’t
tell the Quick Reports editor that we want to know what these
synthetic values represent. To find out, simply click the # character in
one of the synthetic row Checkbox.
This behavior is valid regardless of what subtotal line you add these
markers. The result calculated or displayed for each subtotal is
evaluated by the Quick Reports editor depending on the grouping level
being processed.
To save it to disk as a text file, select Disk File. A system dialog box
appears allowing you to choose a name and location for your file.
98 4D v11 - Self-training
Exercise
USAGE
You can also perform an export using the Export editor.
TIP
summarized as a list or cross-table or saved as HTML with
formatting, you will need to use the Quick Reports editor.
EXERCISE
Try to create the quick reports that follow:
■ List of help requests per technician
■ Number of help requests per technician
■ Number of help requests by request type and technician
■ Number of help requests by object and technician
COMMENTS
It is important to fully understand and work with all the possibilities
that the report editor affords you. You’ll become much more
productive and avoid pulling your hair out over things that 4D already
does so well. Also, you can totally program generation of quick reports
starting from reports that you’ve already set - just like we did in this
chapter - and then generating the corresponding source code (using
the Finalizing step of the wizard). In fact, the quick report generating is
a plug-in that is already integrated into 4D. It has its own
programming language.
4D v11 - Self-training 99
Quick Reports and Exports
USAGE
GETTING IT DONE
The set of 4D language commands is at our fingertips in several editors
(a limited version is available to end-users when they access standard
editors.)
This feature allows refining queries, order by, quick reports, but it also
applies formulas to the record selection of the current table. For
example, if you want to pass a field entirely in capital letters, lowercase
letters, to concatenate character strings or to separate them (if these
processes weren’t taken care of before importing them or if you forms
don’t specify conversions and automatic checks.)
Let’s use an example: in the file that you imported into the Users table,
I purposely forgot to use capital letters in the first letters of first and
last names. Some are capital, lower case, or even both! Also, telephone
numbers aren’t all formatted in the same manner; some have spaces,
some do not, some use dashes, some don’t. The number zero wasn’t
imported due to a spreadsheet that was improperly formatted during
export. We need to take care of this misrepresented data and badly
formatted content.
4D works with the idea of the current selection, which is a list of records
whose numbers are stored in memory and act as a starting point for
each process (except in a few rare cases.)
The order for executing processes can be broken down like this:
1 Select (create a selection of records that suit your needs).
2 Act (one or more actions).
The first step is to find the records, either using query (QUERY
command) or by selecting a set of records in the table (ALL RECORDS
command).
USAGE
3 Select “Apply Formula” in the same menu.
This screen allows writing the formula in the line at the bottom, using
the content of the three columns. Simply drag or double-click a field
name, operator, or a command to add it to the formula line.
4 Enter the following formula:
[HELP_REQUESTS]Object:=Uppercase([HELP_REQUESTS]Object)
TIP
what you want it to do.
The query by formula works the same way, using the same formula
editor; the only difference is that allocation (:=) disappears because it is
incompatible with the query context.
The last step: sort by formula. This allows using the Formula editor in
the same way.
USAGE
The most informative example is generally the dictionary order by,
which, by definition, is sorted in alphabetical order and doesn’t
require a formula. However, what if it is the dictionary for the game
Scrabble™ ? First, you must sort names by length, by number of
characters (1 character words, then 2, then 3, etc.) then within these
groups you sort by alphabetical order.
In this case, you must use the order by formula for the first sort.
In our example, we can perform the order by for first names; for
example, if we want to see who has the shortest first name (ascending
sort) or the longest (descending sort).
In the Order by editor, click Add Formula at the bottom left and then
type:
We then get the list of users sorted by length of first name (in
ascending order) and then alphabetically within each length.
EXERCISE
Exercise 1: Re-write the first name with the first letter in capitals and
the rest of the name in lower case.
USAGE
COMMENTS
You can also apply a method to the selection. If you must apply several
formulas to a single selection, you can write the formula to apply
inside a method:
Then, enter the name of this method in the formula line to apply:
Formulas can also be used in the columns of the Quick Reports editor,
whose features were discussed in earlier chapters. This feature provides
you with a number of combinations (almost an unlimited number), to
create your quick reports or your exports.
GETTING IT DONE
So far, I’ve tried to familiarize you with simple procedures... let’s keep
it up! :=))
INTERFACE
8 Display the Property list.
You’ll immediately see appear the content of the parent form in the
background.
If you modify the location of the button in the parent form then save
it (without closing it), you’ll see the change made on the open children
form(s). It’s also made to children forms that aren’t open, too, but you
just can’t see it. :=))
Import details:
■ Theoretically, there is no limit to inheritance; a parent can have a
parent, who has a parent, but be careful - you know how families
can be, especially among generations! The more you inherit, the
house (as well as your memory) starts to quickly fill up!
■ A parent form est a completely different form. It can contain several
pages and have a page 0. When using inheritance, only pages 0 and
1 of the parent are visible (inheritable).
Now that you know all about inheritance, you can set one up!
Create at least one inherited form for your list forms, another
for your input forms and another for your information or
TIP
settings forms. Your imagination and user necessity will take
care of the rest...
EXERCISE
Transfer the set of buttons of list forms on a form and then make it
inheritable.
COMMENTS
In certain cases, you’ll need a parent form object to be invisible or
inaccessible depending on the context or relevant table.
If we’re talking about a button, you can disable it using the DISABLE
BUTTON command. If we’re talking about an object to hide, use the
SET VISIBLE command.
We’ll see a case a little later in this guide where we’ll optimize
programming using generic methods and pointers.
GETTING IT DONE
In 4D, any form can be printed. There is no conceptual difference
between a report (printed) and a form (displayed on-screen).
In certain cases, you can have a single form for both entry and
printing. In other cases, especially when dealing with different sizes
and features, entry and printing are done using different forms.
It’s not uncommon to have several forms for a single table (you can
create up to 32,000!). You can set which form to use either
programmatically or using interface tools in the Design environment.
2 In “Form type”, choose the type that best suits your needs:
or
4 Click Advanced...
5 Click Options to set print settings.
6 Click OK.
7 Click Edit (do not save the module for now).
INTERFACE
The form appears with the selected objects. At this point, the form can
be modified as necessary.
Note: Black lines divide the form in to quadrants, indicating the size of
the print area based on the defined print settings.
EXERCISE
Set up List print forms and Detailed forms in each of the tables.
In a print form, you can also include a subform. For example, you will
create a form that allows printing the technician record as well as the
set of interventions.
INTERFACE
COMMENTS
Be careful! Inheritance is strictly for display and not printing. This
option is disabled when working with printing.
Take advantage in this chapter to study all the possiblities that the
Form creation wizard provides, especially the part accessible using the
Advanced button.
GETTING IT DONE
As a developer, you certainly know that ergonomics, aesthetics and
setting up the user interface are the most time-consuming aspects of
your development.
We’ve already used several ways to help you save time. Now I want to
give you the keys to optimizing your application, especially
concerning selecting, moving, and navigating your forms.
INTERFACE
Alt+click an object or Right-click an object +
INTERFACE
Access tools using T = Text tool
shortcuts. Touching the F(ield) = Field or variable
following keys at any time L = ListBox, scroll areas
provokes... P = Popup menus, combo box
B = Button
R = Radio button
C = Checkbox
I = Thermometers, dials,
S(quare) = Rectangle, line, …
D = Separator, tab
X = plug-in
There’s also a separate tool: the order entry . It draws the order of
object of order focus on the screen when the user passes from one field
to another using the Tab key.
You can change this order by clicking and dragging a field towards
another... but that can quickly grow tiresome. The easiest way to do
this is to:
■ position your objects,
■ draw a selection rectangle around a group of objects (or all objects,
if necessary).
4D redefines the input order starting with the highest object and then
from left to right and top to bottom.
If we re-organize the form, the initial order is kept, but not very
ergonomic:
INTERFACE
A more logical input order is displayed:
If you prefer a column input, draw a rectangle on the two left fields,
then another on the two right fields.
EXERCISE
Set up shortcuts, especially:
■ moving and enlarging objects
■ selecting with Alt
COMMENTS
Shortcuts are an investment. Losing time to learn how to use them
helps you save a lot of time in the long run.
Saving time and seeking ways to optimize are also possible by drag-
and-drop between two databases, or from the object library to a form.
You’ll also save yourself from performing a number of procedures!
GETTING IT DONE
Each object has properties. Some are unique to that object, others are
part of all objects (object name, coordinates, height, width, resizing,
etc.)
When you select several objects, only shared properties appears. Any
change to one of these properties affects the set of selected objects.
Form properties:
INTERFACE
Text area properties:
Let’s try an exercise right now: when working with forms, you create or
duplicate several objects (fields or variables) and want to deselect the
set of events because none require executing the method.
INTERFACE
Keep pressing the Ctrl/Command key and then click a single event:
EXERCISE
In the “Navigation” project form, make it so that all navigation
buttons (at the top) only have the “On click” event selected.
■ Select the Find button then also select “On load”.
■ Save the form then execute it... Question 1: what happens?
COMMENTS
By default (for ease of maintenance and optimization purposes), I
recommend deselecting the set of object and form events, and then to
only select those you need and those associated with programming. In
fact, if you organized your programming with tests on the events you
use, the method is still loaded and executed each time a checked event
every time it occurs, even if you don’t want to use the event... very
tiring!
Forms also have properties and methods (form method). You may
be tempted to program in any one of these methods (object or
form). But which to choose?
In any case, what matters is that the code is executed when
necessary. So, any method will suffice to start. At first, we won’t
Going further optimize at all costs. You should still keep the order that methods
are executed when an event occurs:
1) Method objects are executed first (in the order of object depth =
levels of map = input order)
2) Once all object methods are executed, the form method takes
over.
GETTING IT DONE
To start, we will put the number of records in the MATERIALS table
from a search inside a variable.
In all other cases (order by, print, previous, etc.), the number of records
doesn’t vary, so there is no reason to recalculate.
Since we want to have this result for each of the displayed tables, we’ll
place the variable on page 0.
1 Click the Field/Variable object:
■ Non-enterable:
The other properties don’t concern us for the time being. Then, we’ll
put in place a calculation method in the All button as follows:
EXERCISE
Finalize this code on the Query and All buttons, taking into account
the table (in other words, the page) on which you’re working.
COMMENTS
For learning purposes, we’ll keep the methods in objects for the
moment. Later, we’ll move them into generic methods to facilitate
maintenance and getting started with new functionalities.
Going further Object methods, form methods, trigger, and database methods
GETTING IT DONE
Throughout the previous lessons, we regularly used variables
(vRecNum...). Let’s now take a look at what they’re made of, how they
work, and, depending on how they are defined, the limits of their
usage.
Variables Overview
The last comment concerning the need for information coming from
the legal department introduces the concept of interprocess
communication. In fact, it’s possible in 4D to order reading or writing
variables of one process to another (and even from a client machine to
the server.)
Now that you understand the purpose of variables, we can learn how
they are used.
Variables Overview
EXERCISE
In a project method named “ARRAYS”, create 3 arrays of 26 elements:
■ The first contains letters of the alphabet.
■ The second contains dates starting from today.
■ The third contains values from 1 to 26.
COMMENTS
As with any language, variables are an integral part of 4D. They can be
used and be abused.
Be careful though, the very nature of certain variables means that they
cannot be viewed in a form (two-dimensional arrays, BLOBs, etc.)
Generic programming
Pointers
Going further
Two-dimensional arrays
BLOBs
GETTING IT DONE
Even though we’re perfect programmers who never make mistakes :=))
it’s still a good idea to “check up” on our code every now and then to
make sure everything is working as it should.
To display the debugger, you can use any one of the five following
ways:
■ Insert the TRACE command in the code (and NO TRACE to stop it):
This is the most frequent way that you’ll display the debugger (unless
you are tracing the server machine.).
■ Add a break point: this is a red dot that you add by clicking in the row
number column where you want 4D to go in to Trace mode:
■ When using your program (in the Application environment), use the
following keyboard shortcuts to start Trace mode:
■ Windows: Alt + Shift + right-click
■ Macintosh: Command + Alt + Ctrl + click
Once you’ve used one about the above choices, 4D displays the
following window, at the execution of the next line of code.
These five options are voluntary options for going to Trace mode, but
4D certainly has its say in the matter! When it notices a syntax error in
your code, the following message is displayed, which also allows you
to start Trace mode:
5
4
I numbered the areas in the order that you typically use them:
EXERCISE
Place a break point in the “On Startup” database method and then re-
start. Watch what happens during this process.
COMMENTS
The two ways most frequently used to go to Trace mode are:
■ the break point
■ the keyboard shortcuts
Using the Debugger, you can perform other tasks and functions,
such as:
• modifying field data and variables being executed
• executing code (careful, this can be risky!... but very helpful :=)
• displaying table and field numbers
Going further
• deleting all expressions
• choosing information that must appear in each area of the editor
• adding temporary break points, conditional break points, etc.
Most of the above features can be performed using the contextual
menu (right-click).
GETTING IT DONE
In the first lessons in programming, we saw how to set parameters for
buttons depending on the selected page.
You can call the method as follows for the “Add” button:
The code isn’t yet optimized, but it’s already greatly improved because
we will concentrate the set of actions in a single method, which
simplifies reading, and, by extension, maintenance.
EXERCISE
Exercise 1: Set up the behavior for the query button and order by
button. Don’t do it for the other buttons because in the following
chapter, we will see how to make the set more “generic” using
pointers.
Exercise 2: Also set up page changing from the buttons on the left side
of the form. Create a method called NAVIGATION_TABLES to which
you can pass the page number as a parameter.
Then, you can take into account an eventual action depending on the
page and put in place the list query on page 8.
COMMENTS
Programming is truly a subjective work of art. After all, doesn’t the
legal aspect of software cover both the program “author” and “author
rights”?
The manner in which these exercises are put in place shows you one
way of doing things. Depending on your habits, development
standards in your company, you’ll find different ways of organizing
things, a nomenclature system of fields and variables, an order for
settings parameters, etc.
If you find a particular manner of doing things that feels right to you,
go for it and compare the advantages and disadvantages of each
solution. It’s also a great way to increase your experience.
Use and abuse generic programming. The next step to think about
is using pointers, which is a complement to what we’ve just
learned.
Generic programming also allows you to reduce the size of your
code by concentrating an entire series of functions in a single
method to which you will pass several text parameters.
To truly get started working with generic methods, keep this in
Going further mind:
- As soon as I write similar lines of code for the second time... there
is certainly a generic solution to consider.
- (Later) when you hesitate about creating a generic method: create
it, you’ll never regret it.
- Not everything can be made generic. Sometimes, trying to make
something too generic can lead to a decrease in productivity (loss
of legibility, difficulty in maintenance and upkeep).
GETTING IT DONE
As we saw in the previous exercise, setting options for each action
(query, order by, etc.) in the NAVIGATION_FUNCTIONS method can
quickly become fastidious. So, we need to set the action towards the
table corresponding to the selected page.
In that case, it’s easier to indicate this just one time at the beginning of
the table to process. This directive is stored in a variable than used in
commands.
Pointers
This simple notion works well in 4D and allows replacing drawers with
tables, fields, or variables.
In the rest of the method, we will use the $Table pointer in a phrase
like “I will query in the table that you showed me”; this bold text is
translated as follows:
As you can see, using a pointer is pretty easy. The advantage of its use
is to make programming even more generic and adaptable.
Let’s take the example from the previous chapter in which you had to
indicate the table to process for each process depending on the page of
the form. We can now write the method as follows:
EXERCISE
Exercise 1: Finish programming this method by replacing the [table]
in the commands by a pointer towards the table.
You should touch up your input forms a bit. Each of these buttons are
available as a graphics file in the import file.
Put in place a generic method with pointers that allows navigating
filling in the set of functions.
Pointers
COMMENTS
At first, you might have a little trouble with pointers, but you’ll
quickly learn that not only are they very easy to set up, but they’re also
very powerful!
I’m going to guess that the answer is probably ‘no’ to both. Similarly,
4D also respects these same principals. This means that different
methods will be executed only if certain pre-defined events occur. Our
mission is to define the situations and behaviors, a little as if we tell a
child to only cross the street if the light is green and to only open the
door to people he or she recognizes.
GETTING IT DONE
Let’s first learn to tell the difference between “form” events from
“database” events.
Form events only kick in if a form is used (on screen or when printing),
when the interface is manipulated by a user (click, drag-and-drop,
mouse-over, menu selection, keyboard stroke, etc.)
Database events only concern the four actions for data: creation,
modification, deletion, and loading. These four events are managed
by the data engine. This is important because in client/server, the data
engine turns on the server.You will never see a database event created
on your client machine. You cannot trace it on a client machine either.
Events
We’ll come back to database events later. First, let’s have a little fun
creating a “special” button.
1 Draw a standard button on page 7 of the Navigation form:
All that’s left to do is to view the form in use, display the preferences
page and try to click the button.
So, how did it go? Did it work? If the button falls off the screen, shut
the form and start again, the button will re-appear in its initial
location.
Only the third point really interests us in this exercise! Come on, be
serious! :=)
Let’s now detail the primary form events that you will use in the short-
term while giving an example of its use for each. Objects don’t all have
the same events; it’s impossible, for example, to check “On Data
Change” for a button object because you can’t “input” its content (its
title).
Events
This list helps you understand events. I’d like to recommend you
consult 4D documentation for a complete description of events.
There’s one important detail that new users always ask: “Do I need to
check form events or object events?” The response is simple:
everything depends on where you are going to create your method.
You’re probably going to say “of course, but why should I code in the
form method instead of the object method or vice-versa?”
example, you can create one that write the following code
for you when typing #$evt” in your method:
$evt:=Form event
Case of
: ($evt=On data change)
: ($evt=On load)
End case
EXERCISE
Organize the Help Requests page as shown in the screenshot below.
You’ll notice a ruler from 0 to 100 at the bottom right of the screen
(drawn using ). Its purpose is to visually and quantitatively
indicate the progress of the service from 0% to 100%:
Events
Next, and this is your mission (if you choose to accept it): the value of
this ruler must match the value of the field when you open a Help
request record. Then, if we update the rule by sliding from left to write,
you must update the field with the ruler value.
COMMENTS
I recommend that you start off by putting your methods in your
objects; you’ll have more flexibility for updating. Then, start by using
generic method calls and finally, when everything works properly, see
what you can move from your object methods to your form method.
Take a good look at how events work because they will allow you to
precisely understand exactly when to execute your programming.
You'll also a number of ideas for powerful first steps by using events:
Going further
on drag, on drop, on selection change, the three “on mouse”
events and, of course, the oldies but goodies, like on clicked, on
double clicked, on long click, etc.
GETTING IT DONE
We’ve already discussed the concept and use of single-value variables.
An array is a multi-valued variable that we can read or write each of its
elements. Like a simple variable, an array must be declared and then
applied a value before being used.
Types Integer, Long integer, Numeric, Identical to variable types except Time
Alpha, Text, Boolean, Date, Time, and BLOB
Picture, BLOB, pointers
As you can see, there are a number of similarities between the two.
Since you already know quite a bit about arrays, all that’s left is to try
and create our first array in the Input form of the HELP_REQUESTS
table.
To do that, we must:
To make the first steps easier, we will put all of the code in the object
defined in step number 2. That allows us to revise the event idea.
1 Select the popup/drop-down list object then draw it to right of
the OBJECT field.
2 Name it TabObjectRequest.
In the pop-up method, we should plan on two events (make sure that
they are both selected):
■ On load: we will initialize the array and apply values just before the
form appears on the screen.
■ On click: when the user selects a value, we transfer its choice in the
Object field.
You undoubtedly noticed that the name of the array is sometimes used
with braces { }, sometimes by itself (rows 11 and 12). In this case, it’s a
variable (long integer) automatically created by 4D. This variable,
associated to the array, serves as an array index (row number). It’s
through this variable that we can learn which row the user selected or
force selection of a specific row in the pop-up menu.
This is what you’ll often see this concise syntax written in database
examples or in other developers’ applications:
You’ll also find, though more concise and a lot more generic, this
syntax that uses the Self command (pointer towards the object whose
method is being executed):
Regardless of the syntax used, the behavior is the same. You must then
input the following:
and once you’ve selected a value, this value is transferred to the Object
field:
Here’s another example with a new array that allows entering the time
the service starts by using a 12-hour window (6:00 AM to 6:00 PM) in
sets of 30 minutes.
To create the pop-up/drop down list, you can do as detailed above and
then create the method. For example, you could write:
However, you’ll see in the next database that I’ve set up the time arrays
in a slightly different way. A time array (interprocess) is initialized
when the database is opened (INIT_ARRAYS method called by the
Initializations method). To use within the context of services, copy the
array when the object method is loaded, which optimized
programming and prevents you from having to recalculate the array
each time, especially when used in several locations (start time, end
time, etc.)
Here’s the third and final example: setting up a tab. A tab is an unique
object with several titles (values). In 4D, its an example of an object
that can represent an array.
Note that the upper part is identical. We’ll put in on page 0. The tab
helps us to browse, regardless of the displayed page, so we’ll need to
put it on page 0. We’ll now take a look at the content of the 2nd page.
7 Create the second page of your form (leave it empty for now, we’ll
fill it up next).
You can test browsing and moving from page to page with your tab.
This works, yet we didn’t have to code the tab’s behavior when clicking
it anywhere! You were probably just thinking that, weren’t you? :=)
No, Harry Potter didn’t use his magical powers on our application. :-)
Quite simply, 4D defines the standard action for a tab as “Go to page”
by default:
Of course, we can always disable this standard action and manage tab
behavior programmatically!
EXERCISE
Create a popup/drop down list that indicates the time the service is
completed.
COMMENTS
As you’ve learned, arrays become useful quite quickly; in fact, they
quickly become a necessity!
In 4D, an array only contains items of the same type. You cannot have
an array with an Alpha element, a Date element and then a third Time
element. In that case, you can use an array of pointers that could point
to variables of different types.
4D lets you add plug-ins very easily. Plug-ins are provided by both 4D
SAS, other developers, or even yourself!
GETTING IT DONE
Let’s start by installing the plug-in.
1 Quit your application.
2 Create a folder named “Plugins” at the same level as your structure file
(on Mac OS, put the new folder in the Content:plugins package).
3 Drag the 4D Write plug-in to the Plugins folder.
4 Re-launch your application (if you don’t have a license, a message
appears indicating that you can use it for up to 30 minutes).
Your plug-in is now active, so you can close the window. For more
information on using 4D Write, as well as its features and
programming with it, check out the documentation.
I told you there was nothing easier than installed a plug-in! :=) Now,
we will associate a 4D Write area with a field, which provides us with a
word processor that we can use directly in our input form.
PLUG-INS
Your form will look as follows:
We now need to set parameters for this area (it’s actually a variable) so
that it contains the 4D Write plug-in:
5 In the “Type” property, scroll through the list and choose 4D Write:
At this point, we have a word processor area that can be used in the
form. If we look at the message (in red) displayed at the top of the area,
we understand that to associate (automatically) the content of this area
to a table field, we should have a field named “Report_” (the “_” is
important here):
And that’s all she wrote! Now, every time we create or modify a report
in a HELP_REQUESTS record, 4D automatically saves it in the Report_
field and released when the record is opened. All that and zero
programming!
PLUG-INS
You can test it by adding and modifying Help Requests records:
EXERCISE
Perform the same installation with the 4D View (spreadsheet) plug-in
by creating a third page in the Input form of the HELP_REQUESTS
table.
COMMENTS
Some plug-ins are available with 4D by default (4D Internet
commands, 4D Pack, etc.) I recommend that you install them for your
project. These days, it’s rare that you develop an application without
having to take into consideration FTP flows, sending/receiving e-mail
or programmed startup of other applications.
It’s also possible to develop your own plug-ins using the “Plug-in
SDK”. This tool lets you set your input points, input and output
variables, as well as numerous parameters linked to generating code.
You can then generate your project. The library call code is
automatically generated. All that’s left is to write your specific code.
GETTING IT DONE
In our Input form in the Help Requests table, we want to include a
send e-mail button. This e-mail will be sent to the technician in charge
of the intervention.
To do that, we need to add this button and the input e-mail interface.
Then, we need to send the e-mail (this will be the goal of the next
lesson.)
1 Create the input e-mail form using the project form.
Let’s make the areas auto-adjustable. Their width and/or height will
adapt to the size of the window in case of resizing.
2 Select all variables and then modify the “Horizontal sizing” property.
3 Then, and only for the vContent, add also “Grow” in the “Vertical
sizing” property:
With these Size properties, you start to see the inner workings of 4D
whose objects adapt to your desires, while sticking to the size of the
form.
5 Add a button to send the e-mail (we will program it in the next
lesson.)
You can add it to the picture library. It is the “Mail” file in the
“Images_png” folder. Then drag the button from the library to your
form.
The dialog box is ready. We can create the call button for this dialog
box at the bottom of the Help Requests INPUT form:
The method of the button that allows displaying the dialog box is as
follows:
Let’s test the dialog box’s behavior. You should get this screen:
If you resize the window, all of the fields should enlarge and the send
e-mail button should move, as well as the associated text.
All that’s left for us to do is transfer the useful information in the Help
requests table’s fields to the form variables (e-mail recipient, subject,
and content). Here is one way of adapting the call e-mail dialog box
button:
EXERCISE
4D uses the ALERT command to display messages. You’ll quickly
notice that it is often necessary to update messages with additional
information. Keeping in mind future application, you will put in place
a customizable form that will replace the ALERT command and display
more personalized messages.
Then, the dialog box is called using a method that you must create.
This method, DIALOG_ALERT, receives three text parameters:
■ the title of the window,
■ the alert message
■ a complement (if necessary)
COMMENTS
Keep in mind that the alert dialog box is a completely separate form. It
can contain several pages, a tab, buttons, arrays, and anything else you
deem necessary. You could even suggest automatic sending of e-mail
to customer service with information about the user, the computer,
the date, the time, the error message, etc. or, simply, the record with
this information in a log file that customer service can access.
You can also set the form so that it displays or hides a given button,
adds a clickable URL that allows accessing online help, select the
language in which you display messages, set messages from a table
selected by the user, etc. Once you get the hang of it, the sky is the
limit.
GETTING IT DONE
To start, we need the 4D Internet Commands plug-in commands.
Install it like you did with 4D Write and 4D View then re-start your
database.
1 Open the MAIL project form and then display the method for the send
e-mail button (in the upper right).
You’ll notice that this command doesn’t allow adding carbon copies,
blind carbon copies, and/or documents. However, it is very practical
and quickly set up.
PLUG-INS
attachment and doesn’t take into account blind carbon copies or the
reply-to:
Put in place this send e-mail method (you can copy the text in the
Send_Email method in the adjusted database).
TIP
don’t set parameters for it, the 4D Internet Commands
plug-in cannot send e-mails and an error message is
returned.
EXERCISE
Put in place a method that allows sending an e-mail to an e-mail
address defined when an error message is displayed (exercise related to
the previous lesson).
COMMENTS
With 4D Internet Commands, you can create a full-service e-mail
application. However, that’s not our goal here. What is of interest to us
is that all the functions necessary for working with the Internet (e-
mail, FTP, SMTP, HTTP, TCP/IP, etc.) are available in 4D.
4D allows you to manage both groups and users; it also allows limiting
access to specific objects or actions:
■ Tables: load (see), create, edit, delete
■ Executing methods
■ Displaying forms
■ Selecting menu items
■ Accessing plug-ins
■ Accessing the Design environment
GETTING IT DONE
In any 4D database, there are at least two users:
■ the Designer, who has full read/write privileges
■ the Administrator, who is usually a client user that administrates
the database and sets permissions.
Simply click Designer then “Edit” to set a password for the account.
ATTENTION: The password accepts both upper and lower case letters.
Like with any password, guard it with your life because you cannot get
it back if you forget it!
You can then add users. Careful, users created by the Designer cannot
be modified by the Administrator and vice-versa. That’s to be expected
since one is supposed to be responsible for developers while the other
the client-side administrator. It’s important that they not interfere
with permissions.
SECURITY
To create groups, click and then .
Enter the name of the group then check the users who should belong
to the group, as well as plug-in access permissions.
A group can be a member of another group. If that’s the case, the child
group inherits permissions from the parent group.
Once you create your groups and users, and assign users to groups, you
can indicate how you want to use your groups.
The point is to indicate which group has the right to perform a given
procedure. For a method, you can indicate which group is owner (in
other words, which group is allowed to modify it in the design
environment) and which group of users has access to the method and
can execute it:
Use can use the Current user and User in group commands
programmatically to intervene in access to a specific part of your
application.
SECURITY
EXERCISE
Create several users (it is possible to not set a password for them right
away during testing). When you will restart your database, you should
get a dialog box such as the following:
Check out the documentation for the syntax of the Current user and
User in group commands and then prohibit access to pages 7 and 8 of
the NAVIGATION form to all users not in the “SETTINGS” group. You
must also create this group and give it users (including Administrator).
COMMENTS
Users and groups are stored in the 4D structure. This behavior implies
several precautions when updating the product site because any
changes made by the administrator (setting a user in a group, etc.) risks
being overwritten by your new structure (the one where you made
modifications).
In fact, triggers are started during ENGINE events and no longer form
events as we’ve seen up through now. By calling an ENGINE event,
there’s an important precision to note: triggers are executed on the
database engine.
Triggers
GETTING IT DONE
Like form events, database events must be checked so that 4D gives
control to the trigger to execute. We can enable any database event in
the Table inspector, as you can see in the following screen:
You’ll find the “Edit” button at the bottom right, which allows
creating the method to create when the checked events occur. The
method is generally structured in the following manner:
Unlike a form method that is only executed during a display (or print),
a trigger is executed regardless of how you access records (display in a
form, programming, import/export, ODBC, etc.) The trigger has the
advantage of being a mandatory passage point.
EXERCISE
Add a Duration field in the Help Requests table and make it so that it
automatically calculates when the end of the service is noted.
COMMENTS
Generally speaking, get used to creating project methods that take care
of your processes, calculations, etc. and call these “generic” methods
from specific methods such as triggers. We saw how to pass
parameters, so take advantage! It’s a lot easier to maintain and you
save a considerable amount of time developing as well as bug fixing.
Your code will be a lot more concise, reliable, and readable.
Triggers
• What’s a selection?
• How to put a selection together and browse from record to
Objective
record
• Interaction between selections and arrays
Estimated time 45 minutes
What we’ll use Method editor, Arrays, Form editor, ListBox, Object properties
Now that the interface and its primary features are in place, we’re now
going to work with records and see what 4D has in store for us.
Before we do anything else, let’s talk about the concept of the current
selection. This is one of the fundamentals of 4D that is a little different
than the usual concepts, especially SQL. We’ll talk about the SQL part
of 4D later in the lessons.
Let’s take a look at an example: if I want to print all services for the
month of May 2007, I first need to query (I create a current selection)
then I print these records by using, for example, an output form
containing several fields from the services table (Date, Object, etc.)
Current Selection
CURRENT SELECTION
(1 per table and per process)
ORDER ...
BY
For Repeat
DELETE EXPORT ... ...
SELECTION End for Until
GETTING IT DONE
As indicated in the above schema, the QUERY command is one of the
ways to obtain a current selection.
Note: We will use a second method to obtain the same result and will
re-do this exercise including SQL commands in 4D so that you see the
different possibilities.
Before you begin: You must determine the desired period. In this
exercise, we’ll limit ourselves to the current year from January 1 to
December 31. Then, you can improve the system to present a choice of
periods to the user.
1 Create a method named STATISTICS that integrates two variables for
the period of dates:
Current Selection
3 Make a loop on the current selection and take each record one after
the other:
4 For each technician, find his or her services (1 current selection in the
Help requests table):
5 Query the help requests for the desired period in this selection:
But wait, did you declare your array? Let’s do it before the loop.
Current Selection
9 Choose the tool and draw it in the lower right hand side of
the form:
The List Box allows inputting data, the order by and moving rows and
columns, the display of alternate colors. It can be synchronized with
arrays like we do here or with the fields of the table’s current selection.
Knowing that the List Box synchronizes its columns, it takes the
smallest number of rows of the arrays that comprise it. This is
important because you will have a situation where you have filled
arrays and an empty List Box because a single array is empty.
Current Selection
11 Click Header1 and change its name to “Technician”. Do the same for
the second header and call it “Help Request Number”:
12 Click the first column (below the header) and give it the name of the
technicians array as a variable name (TechniciansStatsArray). Do the
same for the second column and the array for the number of help
requests (HelpRequestsStatsArray).
And there you go! You just created a simple statistics module!
EXERCISE
Modify the programming of this exercise so that it takes into account a
third column corresponding to the average time per service. In this
case, you only need to use completed services (where an end time is
set).
COMMENTS
To optimize several rows, from the start we could have created an array
starting from the selection of technicians with the DISTINCT VALUES
command, then put a loop on the array.
Current Selection
• Keep a selection
Objective
• Perform crosses (intersection, union. etc.)
Estimated time 20 minutes
Method editor, programming, and use of temporary selections and
What we’ll use
sets
GETTING IT DONE
Don’t worry, setting up sets is a lot easier than the explanation! :=)
To compose a set, simply use the CREATE SET command in the current
selection. Let’s take the case of services started before 09:00:00.
In this command, we set the name of the relevant table as the first
parameter, then the name for the set.
We are going to create a second set for all help questions in progress
(advancements < 100%).
I know that you code cleanly and like to take care of the amount of
memory available, so when your sets are no longer needed, you can
erase them:
Named selections Now that you understand how sets are used, we will learn the second
solution for keeping a selection: named selections.
The idea behind using them it simple; simply use (at least) the two
following commands:
■ COPY NAMED SELECTION where you want to create a named selec-
tion from your current selection.
■ USE NAMED SELECTION where you want to regenerate your current
selection from the named selection.
The table below sums up the all of possibilities for sets and temporary
selections:
EXERCISE
To improve the interface provided for your users (their happiness has
no price), you want to set up the following system: starting from a
selection of records, they must be able to add new records. When a
user completes additions, he or she must find the increased initial list
of newly created records.
Put your programming on the add button and use the following
commands (among others):
■ Create set
■ Add to set
■ Union
■ Use set
COMMENTS
As you might have guessed, we can only compare sets of a single table.
Be careful! The behavior of a set implies during a limited time and
eventually using semaphores (see 4D documentation for more informa-
tion). In fact, a set makes a bit correspond to the physical position of
each table record. In case of deleting and then adding records, the
former content of a physical record could be replaced by the new con-
tent that is no longer in line with what the set is supposed to represent.
Be methodical in the use of sets, which remain an efficient and fast
way to compare selections.
To keep a selection, there’s a third solution that involves
using an array that contains your identification using the
TIP
Now that you understand how sets are used, look in the 4D
Going further
documentation to find out about other commands used with sets.
Process
GETTING IT DONE
Let’s say that you want to access the statistic module that we set up in
a previous lesson. You want it to remain active while you do
something else.
To do that, we will start the method in a process and allow the user to
refresh its statistics by clicking a button that we’ll add in the form.
Let’s start the last step, which means preparing the interface.
During the exercise of the chapter 32, the following changes have been
made to the database:
■ You created a button calling the method that fills arrays:
Now, we must create the process; in other words, we must create the
execution environment for this method:
Process
The method calls itself (it create a second instance of the method in
the new process) and passes itself a parameter:
As such, you can continue to work while having the statistics window
at your fingertips.
To trace the execution of a process (when you execute the
New Process line), use the “step-by-step new process”
TIP
EXERCISE
Create a process that displays the time and elapsed time since the new
process started. This exercise will give you the basics of setting up a
time counter.
COMMENTS
This simple example allows you to consider quick evolutions in your
programming. You can conceptualize your processes, test them in
several lines of code, and set them for independent execution in a
process.
There are numerous examples of using processes. Here’s one more: you
have updates to perform on an important database; however, these
updates can be staggered by several seconds or even minutes without
having an impact on your company’s performance.
You can plan on having a table where you save a to-do list of tasks to
perform. For each of these tasks, you specify a date, a time, and
eventually a priority level and a description of the task to perform
(what? on what table? what record? etc.)
Process
Let’s again work with the statistics example to create the same array
from the SQL commands.
GETTING IT DONE
Create a new method that you’ll call SQL_STATISTICS.
We can break down the step (filling statistic arrays) in two parts:
■ getting the list of technicians
■ for each technician, calculate statistics
SQL
Here is the part of the method that allows creating the first array:
Note the syntax that indicates that 4D transferred the result to the
arrays. We use the variable name proceeded by the “:” symbol (colon).
The rest of the method (sizing arrays) is structured overall in the same
way:
Now, we’ll again use SQL commands to apply values to the arrays:
SQL
EXERCISE
Exercise 1: To complete the statistic array, you will create a summary
array presenting the number of records contained in the set of tables.
The results (numbers) may vary from the corrected report, depending
on the number of records you created.
COMMENTS
The two languages bring both flexibility and possibilities. If you start a
new development project, you can decide to access the database
engine solely using SQL and keep the 4D language for other purposes
(interface, managing user context, etc.).
Going further Discover the four ways of using SQL in your development projects
SQL
Let’s start by tackling the second question: even though we can access
any byte of the BLOB, in most cases we’ll use what we call FIFO (First
In First Out). This means that you recuperate the content of the BLOB
in the order in which you put it into the BLOB.
To make a simple analogy, let’s take a bookshelf where you put away
books from left to right. You start by putting away a small book on the
left and then a large book next to the first one, and then another big
book and then a picture of your mother-in-law and a knick-knack from
last summer’s vacations, etc. When its time to dust the bookshelf, you
don’t think about how many inches from the left side a book is
located. You take them in the order they are and you adjust your gaze
(and hands) depending on the size of the last object picked.
It’s the same thing in a BLOB, especially because you know what you
put away in the BLOB. If you put away an integer, it’s 2 bytes. A long
integer is 4 bytes, while a date is 6 bytes long. For text, you indicate
how you want it put away and how is expressed in the length of the
BLOBS
text (C string, Pascal string, etc.) During re-read, you will re-take the
information in the same order and put them away in variables that are
adapted to the expected content (typed correctly). As such, reading the
BLOB is done in a coherent manner.
Now that you feel better, but still doubtful, let’s put the simplicity of
BLOBs to the test!
GETTING IT DONE
Let’s use a simple example, say, storing variables. We are going to
apply values to three variables, then store them in a BLOB and then
recuperate them in three other variables of the same type. We will
perform checks on how the BLOB and variables are filled using Trace
mode as necessary.
1 Create the BLOB_MANAGEMENT method as follows:
BLOBS
At the end of the third addition, the BLOB contains 53 bytes. Now, we
can extract the data from this BLOB and transfer it to output variables:
Once the first two rows are executed, you can see that variable
recuperation is in progress.
You can see that using BLOBs is really very simple, even if this example
is a very limited sample of how to use one.
When you fill a BLOB, you can use the * character instead
of the $PositionInBlob variable that we used. 4D
TIP
EXERCISE
Let’s now imagine that you have a number of parameters in your
database that must be taken into account for each your user (or each
site where your application is installed): the startup screen, the
background color, the font and size of characters, etc. It’s really just
user preferences.
To keep them, there are several solutions (create a table, a text file, an
XML file, etc.). In our case, we will keep these preferences in a BLOB,
which is stored in a file on your disk (the read and write commands for
BLOBs are BLOB TO DOCUMENT and DOCUMENT TO BLOB).
COMMENTS
Now that you master BLOBs, you are going to be able to use them in
the different scenarios mentioned at the beginning of this chapter.
You can also refine their behavior by using other read/write commands
for variables in BLOBs. However, be sure to take byteswapping into
account (the position of the heavy byte is different between Macintosh
and Windows). For example, if you store a long integer on Windows
and recuperate it on Macintosh, the bytes order is reversed. Talk about
surprises! Think about indicating the desired order when putting away
using the “ByteOrder” parameter.
To save place and/or transfer time, you may also want to consider
compressing your BLOBs. Watch out, 4D only compresses BLOBs if
Going further their size is greater than 255 bytes. When decompressing BLOBs,
think about testing to see if the BLOB is compressed. Otherwise,
the decompression attempt will generate an error.
BLOBS
KEY
Key
This section gathered all of the “keys” for the exercises in this manual.
Each exercise is identified by the name of the lesson and the page
number of the exercise.
You can modify the button name by clicking twice on its title (be
careful, I didn’t say double-click!). Then, enter the title and then
validate the change by pressing Enter on the keypad.
Position the buttons: when you drag buttons, a magnetic grid (with
pointed red lines) indicates the position relative to other objects, as
well as the optimal distance compared to other objects or window
sides. This feature adapts to the Design environment.
On the following screen, you can see the code needed to make it
happen:
■ Help Requests
■ Materials
KEY
EXERCISE: “ENTERING AND MODIFYING RECORDS”, PAGE 60
You must obtain the following result:
Then click Tab: 4D automatically writes the below text and positions
the cursor in the right place:
Select the appropriate command and press Tab: the text writes itself.
KEY
EXERCISE: “QUICK REPORTS AND EXPORTS”, PAGE 99
■ List of help requests per technician:
KEY
■ Number of help requests by request type and technician:
KEY
EXERCISE: “QUERY AND ORDER BY USING FORMULAS, APPLYING
A FORMULA”, PAGE 106
Exercise 1: You have two options:
■ The easiest way to start:
■ you first pass a formula that passes all first names in lowercase:
Note: On MacOS, you can use the same string index [[n]], il will
convert to ≤n≥ on its own (see the following example).
■ Slightly more complicated method, but you’ll get it in no time! Write a
single formula:
Windows:
MacOS:
On the other input forms, you simply need to delete the buttons and
set the inheritance properties.
KEY
All button:
KEY
EXERCISE: “BASIC PROGRAMMING (WITHOUT POINTERS)”,
PAGE 149
KEY
TIP
pointer that will contain a pointer to the table
[technicians] on the first line, a pointer to the [services]
table on the second line, etc.
Note that we de-point Self because its a pointer. We should write Self-
> when we want to address the pointed object.
KEY
It is important to take into consideration the order of
method execution during debugging. If you put code in the
TIP
Of course, you didn’t forget to add a line to the array that manages the
tab so that you can access the third page:
KEY
Finally, you created the plug-in area in the form, set its type (4D View)
and named it “Numbering” so that you get:
Set the method for the button so that it takes into account message
information and sends it to an address that you’ve set:
KEY
EXERCISE: “PASSWORDS AND GROUPS”, PAGE 191
Here is a way to program limited access (add this code to the
TABLES_NAVIGATION method):
I also added the password “A” to the Designer so that 4D displays the
users window at startup. In the following sample databases, I removed
the password.
KEY
We can optimize this trigger by creating a method called
REQUEST_MANAGEMENT to which we pass the Duration parameter:
table name
Note 1: The third array that you create is a “Long integer”. The “Time”
array doesn’t exist in 4D. Instead, it uses long integer arrays. Think of
indicating it in the properties of the third column of your ListBox.
KEY
To combine multiple queries, simply end each (except the
last one) with the two characters ;* and to relate queries
TIP
... and once you decide to code “cleanly”, let’s clear the memory once
we no longer need the sets:
3 In the form properties, select the events that you use in the method.
4 Write the process creation method:
By executing this method, you should get the time counter on screen
be able to continue working on something else.
KEY
The star at the end of the NEW PROCESS command allows
you to avoid recreating a process if a process of the same
TIP
You can then set up a button that allows stopping the elapsed time
calculation, add an enterable text to indicate what the elapsed time
means and, for example, when closing the counter, save the elapsed
time and the reason in a table. You’ll also have a trace of the passed
time.
To start your time counter, you can use a button, a menu item, an
automatic launch when a form opens, etc.
Once the two arrays are filled, you can integrate them into a form and
present them in a dialog box.
For example, you can integrate these arrays in your Navigation form
on an existing page (for example, Preferences) or create a new
“Statistics” page that gathers the set of statistics that you created.
KEY
Exercise 2: Create a form like this one:
During execution, you can write your SQL request in the form and
execute it:
Once you understand these concepts, you can improve your system by
making the list of tables, the list of fields, SQL commands, etc.
available for your users.
At close, like this (I used the project method named Close_Message that
is called in the On Exit database method):
KEY
As you probably noticed, no access path is set in the method. In this
case, 4D stores the file directly in the folder of the structure (your .4DB
database).
Well, here we are at the end of our lesson plan! Even though there are
still more themes to discover, I think you’ve learned enough to get
started with your own development projects in the proper conditions.
Thanks a lot for having paid such close attention during these lessons
Here’s a small list of feature that we didn’t touch on - just to whet your
appetite!
■ Managing pictures
■ Compiling and generating applications
■ Web publishing
■ Web Services
■ XML
■ Enumerations
■ Styles
■ Help bubbles
■ Help bubbles as variables
Going Further
Here are a few links and information sources that can help you to
complete your 4D training:
■ 4D Technical tips: http://www.4d.com/support/techtips.html
■ 4D documentation: http://www.4d.com/support/docs.html