text_database
In previous installments, we learnt how to create a Visual GUI in Rebol and how to create a Mini-Text Database, now we’ll combine the two: How to bind a Text Database to a Visual Form. I have much difficulty as newbie to find any tutorial for the absolute beginner, that’s why I wrote this one and hope you will enjoy it.

Start the Console. After loading the text-database with this instruction:


db: load %db.txt

let’s add a Window layout:


counter: 1
window: layout [

    h2 "Account Information"

    text "Please enter your account details here."

    across
    label "First name" tab FN: field db/:counter/1
    return
    label "Last name" tab LN: field db/:counter/2
    return
    label "Email" tab Em: field db/:counter/3
    return
    tab button "Save" [btn-save]
    button "Next" [btn-next]
    return
    tab button "Close" [btn-close]
    button "Previous" [btn-previous]
]

btn-save: func[][
   print "save not yet implemented"
]

btn-next: func[][
   print "next not yet implemented"
]

btn-previous: func[][
   print "previous not yet implemented"
]

btn-close: func[][
   print "close not yet implemented"
]

Change btn-save implementation:


btn-save: func[][
    db/:counter/1: FN/text
    db/:counter/2: LN/text
    db/:counter/3: Em/text
]
view window

Change btn-next and btn-previous implementation:


btn-next: func[][
   if (counter  < (length? db)) [
    counter: counter + 1
    FN/text: db/:counter/1
    LN/text: db/:counter/2
    Em/text: db/:counter/3
    show window
    ]
]

btn-previous: func[][
   if (counter > 1) [
    counter: counter - 1
    FN/text: db/:counter/1
    LN/text: db/:counter/2
    Em/text: db/:counter/3
    show window
    ]
]
view window

Change btn-close implementation:


btn-close: func[][
    Unview
]
view window

Last but not least we also need 2 more buttons btn-add and btn-delete:


btn-add: func[][
    FN/text: ""
    LN/text: ""
    Em/text: ""
    show window
    counter: (length? db) + 1
    append db to-block remold [FN/text  LN/text  Em/text]
]

btn-delete: func[][
    remove db counter
    Switch counter
    [
        1 [
            counter: counter - 1
            FN/text: ""
            LN/text: ""
            Em/text: ""
        ]
    ] /default [
      counter: counter - 1
      FN/text: db/:counter/1
      LN/text: db/:counter/2
      Em/text: db/:counter/3
    ]
    show window
]

window: layout [

    h2 "Account Information"

    text "Please enter your account details here."

    across
    label "First name" tab FN: field db/:counter/1
    return
    label "Last name" tab LN: field db/:counter/2
    return
    label "Email" tab Em: field db/:counter/3
    return
    button "Add" [btn-add]
    button "Save" [btn-save]
    button "Next" [btn-next]
    return
    button "Delete" [btn-delete]
    button "Close" [btn-close]
    button "Previous" [btn-previous]
]

Update July 5th: As kc puts it (see comment at the end) you can replace


append db to-block remold [FN/text  LN/text  Em/text]

by


append/only remold [FN/text  LN/text  Em/text]

This is the whole program:


Rebol[
  title: "Rebol Mini-Text Database with Visual GUI"
  author: "http://reboltutorial.com/blog/rebol-mini-text-database/"
  version: 1.0.0
]

db: [["John" "Doe" "email hidden; JavaScript is required"]["Jane" "Doe" "email hidden; JavaScript is required"]]
counter: 1
window: layout [

    h2 "Account Information"

    text "Please enter your account details here."

    across
    label "First name" tab FN: field db/:counter/1
    return
    label "Last name" tab LN: field db/:counter/2
    return
    label "Email" tab Em: field db/:counter/3
    return
    button "Add" [btn-add]
    button "Save" [btn-save]
    button "Next" [btn-next]
    return
    button "Delete" [btn-delete]
    button "Close" [btn-close]
    button "Previous" [btn-previous]
]

btn-save: func[][
    db/:counter/1: FN/text
    db/:counter/2: LN/text
    db/:counter/3: Em/text
]

btn-next: func[][
   if (counter  < (length? db)) [
    counter: counter + 1
    FN/text: db/:counter/1
    LN/text: db/:counter/2
    Em/text: db/:counter/3
    show window
    ]
]

btn-previous: func[][
   if (counter > 1) [
    counter: counter - 1
    FN/text: db/:counter/1
    LN/text: db/:counter/2
    Em/text: db/:counter/3
    show window
    ]
]

btn-add: func[][
    FN/text: ""
    LN/text: ""
    Em/text: ""
    show window
    counter: (length? db) + 1
    append db to-block remold [FN/text  LN/text  Em/text]
]

btn-delete: func[][
    remove db counter
    Switch counter
    [
        1 [
            counter: counter - 1
            FN/text: ""
            LN/text: ""
            Em/text: ""
        ]
    ] /default [
      counter: counter - 1
      FN/text: db/:counter/1
      LN/text: db/:counter/2
      Em/text: db/:counter/3
    ]
    show window
]

btn-close: func[][
    Unview
]
view/title window "Mini-Text Database"

If you want to add sort or find functions see here.

References:

Bookmark and Share