Subject Re: How do I set up a Form for a single record
From Mervyn Bick <invalid@invalid.invalid>
Date Sat, 22 Oct 2016 10:10:38 +0200
Newsgroups dbase.getting-started
Attachment(s) default.wfm

On 21-Oct-16 5:02 PM, Joe LeBlanc wrote:
> I'm trying to do a form to hold default values in a program.
> In another life, I would just do a form single record.
>
> Nothing like that is available in dBASE so I did regular form with 1
> blank record in the table and put 2 buttons on the form.
>
> One with "OK" that will save the record and close the form.
> The second one says "Cancel" which will just close the form without
> saving any data.
>
> For the OK button, how do I save the record and close the form.
> Something like ThisForm.close() or ????
>
> For the Cancel button, I need to simply close the fork without saving
> anything.
>
> When I design the form, I have just 1 record.
>
> The 2 buttons will either save or cancel.
>
> Also, There shouldn't be any system record menu when this form is
> active.I can't find anywhere where there is a method or property of the
> form to disable menu items.


If you create the form as SDI ie with the mdi property set false and set
the sysmenu property false the form will not have the normal minimise,
maximise and close buttons.  You must, therefore make your own provision
for closing the form.

To close the form you will need to include form.close() in the event
handlers for your two pushbuttons.  In the Save button's event handler
include form.rowset.save() and in the other one include
form.rowset.abandon()

To make sure you only ever have one record use the form's onOpen event
handler to check the number of rows in the form's rowset.  If it is 0 ie
the first time the form is used then form.rowset.beginAppend() which
will add a blank record.  If the count is 1 and the rowset's autoEdit
property is set false then form.rowset.beginEdit() which will allow you
to edit the existing values.  This is not necessary if the rowset's
autoEdit property is true (its default which means the rowset is already
in edit mode) but I've included it in the attached example as it does no
harm and gives added clarity.

In the example the code to create the required table is in the header
section of the form where it is safe from being altered by the form
designer.  You can, of course, create the table elsewhere in your
application if you wish.

Instead of using a table to hold default values you could consider using
a .ini file.  Code to manage an INI file (ini.cc) is available in the
dUFLP (dBASE Users Function Library Project).  If you don't already have
the dUFLP you can get it from Ken Mayer's website where you will also
find instructions for installing it.

http://www.goldenstag.net/dbase/index.htm#duflp


Mervyn.





if not file('default_values.dbf')
   create table default_values  (id autoinc,data1 character(15),data2 character(15))
endif
** END HEADER -- do not remove this line
//
// Generated on 2016/10/22
//
parameter bModal
local f
f = new defaultForm()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class defaultForm of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      height = 22.1818
      left = 63.0
      top = 10.1818
      width = 106.5714
      text = ""
      mdi = false
      sysMenu = false
   endwith

   this.DEFAULT_VALUES1 = new QUERY(this)
   with (this.DEFAULT_VALUES1)
      left = 5.0
      top = 1.0
      sql = 'select * from "default_values.DBF"'
      active = true
   endwith

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 1.0909
      left = 31.0
      top = 18.0
      width = 15.2857
      text = "Save"
   endwith

   this.PUSHBUTTON2 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON2)
      onClick = class::PUSHBUTTON2_ONCLICK
      height = 1.0909
      left = 60.0
      top = 18.0
      width = 15.2857
      text = "Don't save"
   endwith

   this.ENTRYFIELD1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD1)
      dataLink = form.default_values1.rowset.fields["data1"]
      height = 1.0
      left = 8.0
      top = 7.0
      width = 8.0
   endwith

   this.ENTRYFIELD2 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD2)
      dataLink = form.default_values1.rowset.fields["data2"]
      height = 1.0
      left = 34.0
      top = 7.0
      width = 8.0
   endwith

   this.TEXTLABEL1 = new TEXTLABEL(this)
   with (this.TEXTLABEL1)
      height = 1.0
      left = 8.0
      top = 5.0
      width = 12.0
      text = "data1"
   endwith

   this.TEXTLABEL2 = new TEXTLABEL(this)
   with (this.TEXTLABEL2)
      height = 1.0
      left = 34.0
      top = 5.0
      width = 12.0
      text = "data2"
   endwith

   this.rowset = this.default_values1.rowset

   function PUSHBUTTON1_onClick()
      form.rowset.save()
      form.close()
      return

   function PUSHBUTTON2_onClick()
      form.rowset.abandon()
      form.close()
      return

   function form_onOpen()
      if form.rowset.count() = 0
         form.rowset.beginAppend()
      else
         form.rowset.beginEdit()
      endif        
      return

endclass