Subject Re: field name error
From Mervyn Bick <invalid@invalid.invalid>
Date Sat, 17 Feb 2024 12:19:56 +0200
Newsgroups dbase.getting-started
Attachment(s) create_n_table.wfm

On 2024/02/16 02:57, trevor wrote:
> Hi,
> using dBase plus 2.21 b1755
> BDE 5.2.0.2
> on Windows 10.
>
> Using table designer  I get error message "field name error" no matter
> what name I type (i.e not reserved words such as date) and unable to
> continue.
>
>
> Using the wizard is ok but although able to change type or length but
> get error if changing name.Also cannot ammend existing tables field
> names.
>
> I can use sql 'create table' with no problem.

When I first started with OODML all those years ago there was help
aplenty in the newsgroups but I found I got so much from little working
turnkey examples posted by Marko Mihorko that could be run immediately.
Working code that could basically be cut and pasted with very few
changes suited this card carrying member of the "Monkey see, monkey do"
school of programming down to the ground.

When I eventually reached the stage that I could offer help to others in
the newsgroups I decided that I too would create turnkey examples.
Writing the necessary code by hand to create and populate a table became
a chore so I wrote a little program to stream out code to create the
required table and insert some data.

A stripped down version of the program is attached. Instead of streaming
out code for use in an example it actually uses the "create table" code
to create the table. You can uncomment the print statement if you want
to see the code generated.

You are welcome to use the program but be aware that it was never meant
to be used by others.   There is no dropdown list of field types, there
is no error checking and I never did get the  program to create a Double
type field so in the end I removed the option  and included F for float
which is the SQL equivalent.  I had already use L fr a logical type
field so I left out the Long type and included I for Integer.

The program definitely has warts but it should be better than writing
"create table ..." code by hand.

Mervyn.




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

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

** END HEADER -- do not remove this line
//
// Generated on 2024-02-17
//
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)
      onOpen = class::FORM_ONOPEN
      height = 25.2727
      left = 21.5714
      top = 0.2273
      width = 79.5714
      text = ""
   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)
      onKey = class::ENTRYFIELD1_ONKEY
      height = 1.0
      left = 22.0
      top = 1.3636
      width = 18.4286
      value = " "
   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.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
      height = 15.5909
      left = 6.5714
      top = 7.9091
      width = 66.7143
   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.rowset = this.mbtemptable1.rowset

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

   function PUSHBUTTON1_onClick
      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
      return

   function convert_type
        local cNewtype
        do case
           case form.rowset.fields["field_type"].value.toUpperCase() = " "
                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 form.rowset.fields["field_type"].value.toUpperCase() = "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 form.rowset.fields["field_type"].value.toUpperCase() = "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 form.rowset.fields["field_type"].value.toUpperCase() = "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 form.rowset.fields["field_type"].value.toUppercase() = "I"
                cNewType ="integer"
            case form.rowset.fields["field_type"].value.toUpperCase() = "A"
               cNewType = "autoinc"
            case form.rowset.fields["field_type"].value.toUpperCase() = "D"
               cNewType = "date"
            case form.rowset.fields["field_type"].value.toUpperCase() = "T"
               cNewType = "timestamp"
            case form.rowset.fields["field_type"].value.toUpperCase() = "L"
               cNewtype = "boolean"
            case form.rowset.fields["field_type"].value.toUpperCase() = "M"
               cNewtype = "blob(0,1)"
            case form.rowset.fields["field_type"].value.toUpperCase() = "B"
               cNewtype = "blob(0,2)"
            case form.rowset.fields["field_type"].value.toUpperCase() = "O"
               cNewtype = "blob(0,4)"
         endcase
         return cNewType

   function form_onOpen()
      
      return

endclass