Subject Re: create_n_table Form (field error)
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 19 Feb 2024 09:45:50 +0200
Newsgroups dbase.getting-started
Attachment(s) create_n_table.wfm

On 2024/02/19 07:04, trevor wrote:
> Mervyn,
>
> Thanks very much, got the form to work with a few tweeks, reinstalled
> queery and datalink and works fine. With alter table and changing field
> type in table designer, if needed, a great work around solution for the
> problem.

I'm glad you got it working.  Attached is a more civilized version.  A
dropdown list for the field types which still allows a valid single
character to be input manually.  A check for an existing table and the
ability to delete it if required.

Mervyn.





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

if not file('mbtemptable.dbf')
   create table mbtemptable (name character(20),field_type character(13),field_spec character(10))
endif

** END HEADER -- do not remove this line
//
// Generated on 2024-02-19
//
parameter bModal
local f
f = new create_n_tableForm()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class create_n_tableForm of FORM
   with (this)
      canClose = class::FORM_CANCLOSE
      height = 25.2727
      left = 13.8571
      top = -0.1818
      width = 78.7143
      text = "Create New Table"
   endwith

   this.MBTEMPTABLE1 = new QUERY(this)
   with (this.MBTEMPTABLE1)
      width = 10.0
      height = 1.0
      sql = 'select * from "mbtemptable.DBF"'
      active = true
   endwith

   this.ENTRYFIELD1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD1)
      onLostFocus = class::ENTRYFIELD1_ONLOSTFOCUS
      onKey = class::ENTRYFIELD1_ONKEY
      height = 1.0
      left = 22.0
      top = 1.3636
      width = 18.4286
      value = " "
   endwith

   this.TEXTLABEL1 = new TEXTLABEL(this)
   with (this.TEXTLABEL1)
      height = 1.0
      left = 8.7143
      top = 1.3636
      width = 12.0
      text = "Name of table"
   endwith

   this.GRID1 = new GRID(this)
   with (this.GRID1)
      dataLink = form.mbtemptable1.rowset
      columns["COLUMN1"] = new GRIDCOLUMN(form.GRID1)
      with (columns["COLUMN1"])
         dataLink = form.mbtemptable1.rowset.fields["name"]
         editorType = 1        // EntryField
         width = 28.5714
      endwith
      columns["COLUMN2"] = new GRIDCOLUMN(form.GRID1)
      with (columns["COLUMN2"])
         dataLink = form.mbtemptable1.rowset.fields["field_type"]
         editorType = 4        // ComboBox
         width = 16.5714
      endwith
      columns["COLUMN3"] = new GRIDCOLUMN(form.GRID1)
      with (columns["COLUMN3"])
         dataLink = form.mbtemptable1.rowset.fields["field_spec"]
         editorType = 1        // EntryField
         width = 14.2857
      endwith
      with (columns["COLUMN1"].headingControl)
         value = "name"
      endwith

      with (columns["COLUMN2"].editorControl)
         style = 1        // DropDownList
         dataSource = 'array {"Autoincrement","Binary","Character","Date","Float","Integer","Logical","Memo","Numeric","OLE","Timestamp"}'
      endwith

      with (columns["COLUMN2"].headingControl)
         value = "field_type"
      endwith

      with (columns["COLUMN3"].headingControl)
         value = "field_spec"
      endwith

      height = 15.5909
      left = 6.5714
      top = 7.9091
      width = 64.1429
   endwith

   this.PUSHBUTTON2 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON2)
      onClick = class::PUSHBUTTON2_ONCLICK
      height = 1.0909
      left = 6.2857
      top = 23.9545
      width = 15.2857
      text = "Create table"
   endwith

   this.TEXTLABEL4 = new TEXTLABEL(this)
   with (this.TEXTLABEL4)
      height = 1.0
      left = 9.0
      top = 3.6818
      width = 12.0
      text = "Default char"
   endwith

   this.TEXTLABEL5 = new TEXTLABEL(this)
   with (this.TEXTLABEL5)
      height = 1.0
      left = 35.4286
      top = 3.6818
      width = 12.0
      text = "Default num"
   endwith

   this.ENTRYFIELD4 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD4)
      height = 1.0
      left = 22.0
      top = 3.6818
      width = 8.0
      value = "15"
   endwith

   this.ENTRYFIELD5 = new ENTRYFIELD(this)
   with (this.ENTRYFIELD5)
      height = 1.0
      left = 48.2857
      top = 3.6818
      width = 8.0
      value = "10,2"
   endwith

   this.TEXTLABEL2 = new TEXTLABEL(this)
   with (this.TEXTLABEL2)
      height = 1.0
      left = 39.5714
      top = 5.6364
      width = 25.8571
      text = "Default field type is Character"
   endwith

   this.PUSHBUTTON1 = new PUSHBUTTON(this)
   with (this.PUSHBUTTON1)
      onClick = class::PUSHBUTTON1_ONCLICK
      height = 1.0909
      left = 21.5714
      top = 5.6364
      width = 15.2857
      text = "Continue"
   endwith

   this.TEXT1 = new TEXT(this)
   with (this.TEXT1)
      height = 3.0
      left = 44.5714
      top = 0.4545
      width = 29.8571
      text = 'Press Tab to move focus to entryfields to change defaults or press Enter or left-click on "Continue" button to procede.'
   endwith

   this.rowset = this.mbtemptable1.rowset

   function ENTRYFIELD1_onKey(nChar, nPosition,bShift,bControl)
      if nChar = 13
         form.pushbutton1_onclick()
      elseif nChar = 9
         form.entryfield4.setfocus()        
      endif      
      return

   function ENTRYFIELD1_onLostFocus()
      if empty(this.value)
         msgbox('Table name required.','Error')
         inkey(0.01)
         this.setfocus()
      endif  
      return

   function PUSHBUTTON1_onClick
      cTable = form.entryfield1.value.toLowerCase()
      if not '.dbf'$cTable
         cTable = trim(cTable)+'.dbf'
      endif  
      f = new file()
      if f.exists(cTable)
         if msgbox( cTable+' exists.  Delete it?','Warning',4+16) = 6
            cmd = 'drop table '+substr(cTable,1,at('.',cTable)-1)
            &cmd
            class::ready_for_fields()
         else
            form.entryfield1.setfocus()
         endif
      else
         class::ready_for_fields()      
      endif

   function form_canClose()
      form.entryfield1.onLostFocus = null  //Allow to close with entryfield1 blank
      return true

   function ready_for_fields
      use mbtemptable
      append blank
      use
      if type("form.mbtemptable1") # "U"
         form.mbtemptable1.active = false
         form.mbtemptable1.active = true
      endif
      form.grid1.setfocus()
      return

   function PUSHBUTTON2_onClick
      if "."$form.entryfield1.value
        form.entryfield1.value = left(form.entryfield1.value,at(".",form.entryfield1.value)-1)
      endif
      if empty(form.entryfield1.value)
      form.entryfield1.value = "mbtesttable"
      endif
      cStr=  "   create table "+form.entryfield1.value+" ("
      form.rowset.first()
      cStr += trim(form.rowset.fields["name"].value)
      cStr += " "+class::convert_type()
      form.rowset.next()
      do while not form.rowset.endofset
         cStr += ","
         cStr += trim(form.rowset.fields["name"].value)
         cStr += " "+class::convert_type()
         form.rowset.next()
      enddo
      cStr+= ')'
//      ?cStr
      &cStr
      form.mbtemptable1.active = false
      drop table mbtemptable
      create table mbtemptable (name character(20),field_type character(13),field_spec character(10))
      form.mbtemptable1.active = true
      form.grid1.dataLink = form.mbtemptable1.rowset
      form.entryfield1.value = ''
      form.entryfield1.setfocus()
      return

   function convert_type
        local cNewtype
        do case
           case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = " "
                cNewType = "character("
                if not empty(form.rowset.fields["field_spec"].value)
                   cNewType += trim(form.rowset.fields["field_spec"].value)+")"
                else
                   cNewType += form.entryfield4.value + ")"
                endif
           case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "C"
                cNewType = "character("
                if not empty(form.rowset.fields["field_spec"].value)
                   cNewType += trim(form.rowset.fields["field_spec"].value)+")"
                else
                   cNewType += form.entryfield4.value + ")"
                endif
           case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "N"
                cNewType = "numeric("
                if not empty(form.rowset.fields["field_spec"].value)
                   cNewType += trim(form.rowset.fields["field_spec"].value)+")"
                else
                   cNewType += form.entryfield5.value + ")"
                endif
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "F"
                 cNewType = "float("
                 if not empty(form.rowset.fields["field_spec"].value)
                   cNewType += trim(form.rowset.fields["field_spec"].value)+")"
                else
                   cNewType += form.entryfield4.value + ")"
                endif
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "I"
                cNewType ="integer"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "A"
               cNewType = "autoinc"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "D"
               cNewType = "date"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "T"
               cNewType = "timestamp"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "L"
               cNewtype = "boolean"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "M"
               cNewtype = "blob(0,1)"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "B"
               cNewtype = "blob(0,2)"
            case left(form.rowset.fields["field_type"].value.toUpperCase(),1) = "O"
               cNewtype = "blob(0,4)"
         endcase
         return cNewType

endclass