Subject Re: Incrementing a value OnClick
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 20 Nov 2023 15:55:14 +0200
Newsgroups dbase.getting-started
Attachment(s) increment_a_value.wfm

On 2023/11/20 13:56, Ed wrote:
> Hi,
>
> I'm brand new to dbase and could do with some pointers. I've made a new DB called WEF1 and a tabled called T1. The table has three columns - name (character) and two columns called STR and DEF (both two digit numeric values).
>
> There are two people - "Bob" and "Fred" who both have a couple of random values for STR and DEF.
>
> I've made a form with a browser object and pointed it at T1 and it is all good.
>
> I've put a button on the screen and all I want it to do is increase, say "Bob"s STR by one each time I click it. I've almost no idea how to even begin this.
>
> Any examples or pointers would be appreciated.

Firstly, time spent on working through the dBASE tutorial which you will
find at Ken Mayer's website
http://www.goldenstag.net/dbase/Tutorial/00_Preface.htm   This will
really be time well spent.  You are always welcome to ask questions here
but you'll find you need to ask fewer and fewer questions as you work
through the tutorial.

When you visit Ken's site, grab a copy of the dUFLP (dBASE User Function
Library Project) and install it.  Full instructions on how to install
the dUFLP are on the site.

Use a GRID object rather than a BROWSER object on your form.  The
BROWSER was meant to be used with DML i.e the original commands dating
back to DOS days.  A GRID object is geared to object oriented
programming (OOP) and OODML (Object Oriented Data Manipulation Language)
which is far more powerful.

A little example form is attached.

Mervyn.

Before I could post this Ken's message arrived. I have, therefore, added
his suggestion to the example form.





if file('mb1.dbf')
drop table mb1
endif

if not file('mb1.dbf')
   create table mb1  (name character(15),str numeric(10,0),def numeric(10,0))

   insert into mb1  (name,str,def) values ("Bob",123.00,456.00)
   insert into mb1  (name,str,def) values ("Fred",789.00,123.00)
endif

** END HEADER -- do not remove this line
//
// Generated on 2023-11-20
//
parameter bModal
local f
f = new increment_a_valueForm()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class increment_a_valueForm of FORM
   with (this)
      height = 21.6818
      left = 19.4286
      top = 6.5455
      width = 72.7143
      text = ""
   endwith

   this.WEF11 = new DATABASE()
   this.WEF11.parent = this
   with (this.WEF11)
      left = 55.0
      width = 4.0
      height = 1.0
      databaseName = "WEF1"
      active = true
   endwith

   this.MB11 = new QUERY()
   this.MB11.parent = this
   with (this.MB11)
      left = 56.0
      top = 1.0
      width = 3.0
      height = 1.0
      database = form.wef11
      sql = "select * from MB1.DBF"
      active = true
   endwith

   this.GRID1 = new GRID(this)
   with (this.GRID1)
      dataLink = form.mb11.rowset
      height = 4.5
      left = 5.8571
      top = 3.1364
      width = 60.8571
   endwith

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 1.8636
      left = 15.0
      top = 9.6364
      width = 15.2857
      text = "Increment Bob's STR"
   endwith

   this.PUSHBUTTON2 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON2)
      onClick = class::PUSHBUTTON2_ONCLICK
      height = 1.8636
      left = 42.5714
      top = 9.6364
      width = 15.2857
      text = "Increment Fred's STR"
   endwith

   this.PUSHBUTTON3 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON3)
      onClick = class::PUSHBUTTON3_ONCLICK
      height = 1.8636
      left = 15.0
      top = 12.7727
      width = 15.2857
      text = "Increment Bob's DEF"
   endwith

   this.PUSHBUTTON4 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON4)
      onClick = class::PUSHBUTTON4_ONCLICK
      height = 1.8636
      left = 42.5714
      top = 12.7727
      width = 15.2857
      text = "Increment Fred's DEF"
   endwith

   this.SPINBOX1 = new SPINBOX(this)
   with (this.SPINBOX1)
      onChange = class::SPINBOX1_ONCHANGE
      dataLink = form.mb11.rowset.fields["str"]
      height = 1.0
      left = 17.0
      top = 19.0
      width = 8.0
      rangeMax = 100
      rangeMin = 1
   endwith

   this.TEXTLABEL1 = new TEXTLABEL(this)
   with (this.TEXTLABEL1)
      height = 1.0
      left = 4.5714
      top = 19.0
      width = 12.0
      text = " STR"
   endwith

   this.SPINBOX2 = new SPINBOX(this)
   with (this.SPINBOX2)
      onChange = class::SPINBOX2_ONCHANGE
      dataLink = form.mb11.rowset.fields["def"]
      height = 1.0
      left = 53.4286
      top = 19.0
      width = 8.0
      rangeMax = 100
      rangeMin = 1
   endwith

   this.TEXTLABEL2 = new TEXTLABEL(this)
   with (this.TEXTLABEL2)
      height = 1.0
      left = 39.8571
      top = 19.0
      width = 12.0
      text = "DEF"
   endwith

   this.TEXTLABEL3 = new TEXTLABEL(this)
   with (this.TEXTLABEL3)
      height = 1.0
      left = 9.2857
      top = 15.6364
      width = 51.1429
      text = "Select the correct record in the grid before changing a value."
   endwith

   this.ENTRYFIELD1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD1)
      when = {||false}
      dataLink = form.mb11.rowset.fields["name"]
      height = 1.0
      left = 30.4286
      top = 17.1818
      width = 8.0
   endwith

   this.TEXTLABEL4 = new TEXTLABEL(this)
   with (this.TEXTLABEL4)
      height = 1.0
      left = 18.4286
      top = 17.2727
      width = 12.0
      text = "Name"
   endwith

   this.rowset = this.mb11.rowset

   function PUSHBUTTON1_onClick()
      form.mb11.rowset.applyLocate("name ='Bob'") //Go to the correct record
      form.mb11.rowset.fields['str'].value ++ //Increment the value
      form.mb11.rowset.save() //Save the new value
      return

   function PUSHBUTTON2_onClick()
      form.mb11.rowset.applyLocate("name ='Fred'")
      form.mb11.rowset.fields['str'].value ++
      form.mb11.rowset.save()
      return

   function PUSHBUTTON3_onClick()
      form.mb11.rowset.applyLocate("name ='Bob'")
      form.mb11.rowset.fields['def'].value ++
      form.mb11.rowset.save()
      return

   function PUSHBUTTON4_onClick()
      form.mb11.rowset.applyLocate("name ='Fred'")
      form.mb11.rowset.fields['def'].value ++
      form.mb11.rowset.save()
      return

   function SPINBOX1_onChange()
      form.mb11.rowset.save() //save the new value which will the upate the grid
      return

   function SPINBOX2_onChange()
      form.mb11.rowset.save() //save the new value which will the upate the grid
      return

endclass