Creating a Project

Let's begin by learning how to create a record in the database. We will start by creating a new project in the projects table.

Edit Card Script

Edit the current card script of the program stack. If the To-Do window is frontmost then you can choose Object > Card Script.

Locate uiCreateProject

Locate the uiCreateProject handler in the card script. This is where you will add the logic that creates a record in the projects table of the database.

Update uiCreateProject Command

Replace the uiCreateProject handler in the card script with the RevTalk code below. Make sure and compile the script after pasting in the new code so that the updated script is applied. After you insert the code we will go through the relevant parts.

Copy & Paste The Following Code
command uiCreateProject
  ## Create a SQL Record object for 'projects' table
  put sqlrecord_createObject("projects") into theRecordA
  ## Set name property of object
  sqlrecord_set theRecordA, "name", "New Project"
  ## Create record in the database
  sqlrecord_create theRecordA
  put the result into theError
  if theError is empty then
    ## Refresh list
    lock screen
    ## select new record (uSelectedProjectID is a custom property defined in group script)
    set the uSelectedProjectID of group "ProjectsPeople" to theRecordA["id"]
    ## tell Data Grid to select all text when field editor is opened
    set the dgTemplateFieldEditor["select text"] of group "ProjectsPeople" to true
    ## open field editor so that user can change name.
    dispatch "EditKeyOfIndex" to group "ProjectsPeople" with "name", \
          the dgHilitedIndex of group "ProjectsPeople"
    unlock screen
  end if
  if theError is not empty then
    answer "Error creating project:" && theError & "."
  end if
end uiCreateProject

Using SQL Yoga To Create a Record in the Database

The code to create a new record is fairly straightforward.

1) Create a SQL Record object for the projects table. This object is represented by an array and it contains the keys for each of the fields (or columns) in your database. Each field key in the array contains the default value as defined by the database. Note that since the object is represented using an array it only exists as long as the array variable exists. A SQL Record object does not persist across sessions.

2) Set table field properties. We are going to assign every new project the name "New Project". The user can change the name in the UI if they wish.

3) Tell SQL Yoga to create a record in the database that has the properties of the SQL Record object.

Test Record Creation In The Database

You that you have updated the card script you can test. The Project button already has a script that calls uiCreateProject in the card script. Click on the button to create a record. YOU WON'T GET ANY VISUAL FEEDBACK AT THIS POINT.

Confirm That Record Was Created

To confirm that a record was created you can open up your favorite database manager and look at the records in the projects table (1). You will see that one record exists named New Project (2).


Vaughn Clement

So, I followed the instructions for the To-Do setup and the password script screen was shown. What is required to go forward here?

Trevor DeVore

@Vaughn - That means LiveCode encountered a coding error. Because SQL Yoga is password protected error reporting is a little trickier. I would recommend reading the SQL Yoga manual before you go on.

Specially this article on error reporting:

Add your comment

E-Mail me when someone replies to this comment