Switch to: V9V8V7V6V5

Lesson 1 - "Employees" Database

Step 1: Initialization

New project creation

Start Director. Director automatically creates a new empty project. Save the new project.

Start of work with Valentina

In order to start you should create the global variable Valentina and initialize Valentina with the help of Valentina.Init() function. The most convenient place for this is the handler startmovie().

To do this you should do the following:

  • Open Cast Window if it is not opened.
  • Choose the first cast member and open Script Window for it.
  • Write the following text:
global Valentina
on startmovie
    Valentina = new ( Xtra "Valentina" )
    Valentina.Init( 10 * 1024 * 1024 )
end

Finishing of work with Valentina

In order to finish you should use Valentina.ShutDown() function. The most convenient place for this is the handler startmovie().

To do this you should do the following:

• In the edit window append the following text:

on stopmovie 
    Valentina.Shutdown()
    Valentina = VOID
end

Step 2: Database Creation

At this step you will learn how to create new database, to open and to close database.

In this example we want to create new database on application start if there is no database on the disk or to open the existed one.

Creation of CreateOrOpenDatabase() function

You should create the handler CreateOrOpenDatabase() and global variable gDatabase to open and to create the database on the project start.

To do this you should do the following:

  • Open Cast Window if it is not opened.
  • Choose the second cast member and open Script Window for it.
  • In the edit window write the following text:
global Valentina
global gDatabase

on CreateOrOpenDatabase()
    gDatabase = new (Xtra "VDatabase")
  
    -- We try to open database, if we fail to open then we create it
    gDatabase.Open( the moviePath & "database1.vdb" )
    if Valentina.lastError then
      gDatabase.Create( the moviePath & "database1.vdb", #kDscDatBlbInd )
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
    else
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
    end  if
end

Creation of CloseDatabase() function

In order to close database on finishing the project you should create the CloseDatabase() handler.

To do this you should do the following:

• In the edit window append the following text:

on CloseDatabase()
    gDatabase.Close()
end

Step 3: Table Creation

At this step you will learn how to create database structure.

Creation of CreateStructure() method

It is necessary for you to create new CreateStructure() method where we will create tables and fields.

We want to create the table with three fields: Name, LName, BirthDate.

To do this you should do the following:

  • Open Cast Window if it is not opened.
  • Choose the third cast member and open Script Window for it.
  • Write down the following text in the code editor window for this method:
global Valentina
global gDatabase

on CreateStructure() 
    tblPerson = gDatabase.CreateTable( "Person" )
    tblPerson.CreateVarCharField( "Name",  1022, #fIndexed )
    tblPerson.CreateVarCharField( "LName",  1022, #fIndexed )
    tblPerson.CreateDateField( "BirthDate",  #fIndexed )
end

Method CreateStructure() Call in CreateOrOpenDatabase()

You should call CreateStructure() method in CreateOrOpenDatabase() event in order to create the structure of database on it's creation.

To do this you should do the following:

  • Choose the second cast member and open Script Window for it.
  • Append the following text after gDatabase.Create() call:
CreateStructure()

After that CreateOrOpenDatabase() method the text will look like:

global Valentina
global gDatabase

on CreateOrOpenDatabase()
    gDatabase = new (Xtra "VDatabase")
  
    -- We try to open the database, if we fail to open then we create it
    gDatabase.Open( the moviePath & "database1.vdb" )
  
    if Valentina.lastError then
      gDatabase.Create( the moviePath & "database1.vdb", #kDscDatBlbInd )
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
       
      CreateStructure()  
    else
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]
    end  if
end 

on CloseDatabase()
    gDatabase.Close()
end

Step 4: Design of Window

At this step you will learn how to create graphic elements to control the database. As a result of the actions execution mentioned below we will get the window of such kind:

Creation of Edit Fields

You should create edit fields for displaying and editing of table field values. Each table field should have corresponded edit field. To do this you should do the following:

  • Create three Field controls and place them as it is shown in the picture. Name them: efName, efLName, efBirthDate.
  • Create three Text controls and place them as it is shown in the picture. Set the text Name, LName, BirthDate correspondingly for each element.


Creation of Record Modification Buttons

We need three buttons for adding, updating and deleting records in order to have the ability to change records.

To do this you should do the following:

  • Create three buttons.
  • Set the button names Add, Delete, Update correspondingly.


Creation of Navigation Buttons

In order to have the ability for record navigation we need four buttons for the movement to the first, last, previous or next record.

To do this you should do the following:

  • Create four buttons.
  • Set the button names |«, <, >, »| correspondingly.


lesson1_step4_p1.jpg

Step 5: Add Logic to Controls

At this step you will learn how to program logic for controls in order to add, update, delete records and also to navigate in records.

Pay attention that we have only mDatabase property. That's why in every following function we will ask for a Person table pointer.

Implementation of Action() for Add Button

You should write the action which will be executed on pressing Add button.

To do this you should do the following:

  • Using contextual menu for Add button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase
  
    tblPerson = gDatabase.Table( "Person" )
    
    tblPerson.SetBlank()
    CollectPanes()
    tblPerson.AddRecord()
end

Here we prepare a record buffer using SetBlank() function. We call CollectPanes() method which copies values from edit fields to table fields. We add the record to the table.

Implementation of Action() for Update Button

You should write the action which will be executed on pressing Update button.

To do this you should do the following:

  • Using contextual menu for Update button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase
  
    tblPerson = gDatabase.Table( "Person" )
  
    CollectPanes()
    tblPerson.UpdateRecord()
 end

Here we call the CollectPanes() method, which copies values from edit fields to table fields. We update the record of the table.

Implementation of Action() for Delete Button

You want to write the action which will be executed on pressing Delete button. To do this you should do the following:

  • Using contextual menu for Delete button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase
  
    tblPerson = gDatabase.Table( "Person" )
  
    tblPerson.DeleteRecord()
    PopulatePanes()
end

CollectPanes Method Implementation

We create CollectPanes() method which copies values from edit fields to table fields.

To do this you should do the following:

  • Choose the third cast member and open Script Window for it.
  • Append the following text after gDatabase.Create() call:
on CollectPanes()
    tblPerson = gDatabase.Table( "Person" )
  
    tblPerson.Field("Name").value = the text of member "efName" 
    tblPerson.Field("LName").value = the text of member "efLName"
    tblPerson.Field("BirthDate").value = the text of member "efBirthDate"
end

PopulateFields Method Implementation

We create PopulateFields() method which copies values from table fields to edit field. This method is the reverse one to the CollectPanes() method.

To do this you should do the following:

  • Choose the third cast member and open Script Window for it.
  • Append the following text for this method in the code editor window:
on PopulatePanes()
    tblPerson = gDatabase.Table( "Person" )

    put tblPerson.Field("Name").value into field "efName" 
    put tblPerson.Field("LName").value into field "efLName"
    put tblPerson.Field("BirthDate").value into field "efBirthDate"
end

Implementation for |« Button

You should write the action which will be executed on pressing |« button. To do this you should do the following:

  • Using contextual menu for |« button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase

    tblPerson = gDatabase.Table( "Person" )
    tblPerson.FirstRecord()
    PopulatePanes()
end

Implementation for < Button

You should write the action which will be executed on pressing < button. To do this you should do the following:

  • Using contextual menu for < button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase

    tblPerson = gDatabase.Table( "Person" )
    tblPerson.PrevRecord()
    PopulatePanes()
end

Implementation for > Button

You should write the action which will be executed on pressing > button. To do this you should do the following:

  • Using contextual menu for > button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase

    tblPerson = gDatabase.Table( "Person" )
    tblPerson.NextRecord()
    PopulatePanes()
end

Implementation for »| Button

You should write the action which will be executed on pressing »| button. To do this you should do the following:

  • Using contextual menu for |» button open the Script Window.
  • Write the following text down:
on mouseUp me
    global Valentina
    global gDatabase

    tblPerson = gDatabase.Table( "Person" )
    tblPerson.LastRecord()
    PopulatePanes()
end

Move to the first record on database opening

On database opening you should move to the first record and show it.

To do this you should do the following:

  • Choose the second cast member and open Script Window for it.
  • In the code editor window append the following text in the “if” block of the CreateOrOpenDatabase() function:
else
    gDatabase.Table("Person").FirstRecord()
    PopulatePanes()

After this the CreateOrOpenDatabase() function will look like this:

on CreateOrOpenDatabase()
    gDatabase = new (Xtra "VDatabase")
  
    -- We try to open database, if we fail to open then we create it
    gDatabase.Open( the moviePath & "database1.vdb" )
  
    if Valentina.lastError then
      gDatabase.Create( the moviePath & "database1.vdb", #kDscDatBlbInd )
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]

      CreateStructure()  
    else
      gDatabase.dateTimeFormat = [ #kYMD, "-", ":", 10 ]

      gDatabase.Table("Person").FirstRecord()
      PopulatePanes()
    end  if
end