` Printed Icetips Article

Icetips Article



Par2: Adding field labels to an export file
1999-07-13 -- Jim Katz
 
I would do something like this, assuming NEWFile is the output file and
MyFile is the file to be exported:

Clear(New:Record)
Loop I# = 1 To MyFile{PROP:Fields}
   Execute I#
      NEW:Field1 = MyFile{PROP:Label,I#}
      New:Field2 = MyFile{PROP:Label,I#}
      ....
      New:FieldLast = MyFile{PROP:Label,I#}
   End
End
Add(NewFile)

Leonid Chudakov adds:
And make it shorter:

CurrentField ANY 
  
Clear(MyFile:Record)
Loop I# = 1 To MyFile{PROP:Fields}
    CurrentField &= WHAT(MyFile:Record,I#)
    CurrentField = MyFile{PROP:Label,I#}
End
Add(MyFile)

Patrick O'Brien:
I thought I would send an update with the final version of my code. By
combining elements suggested here and tweaking things I was able to get the
solution that I wanted. The result is the first record of my csv file
contains field names, regardless of how the csv file is defined (numeric
fields, short strings, etc.) and without any hard-coding of field names.

The basic trick is to define the file as an ASCII file (which I did in code,
not in the dictionary), gather up the field names, write them to the ASCII
file, close the file, then open it back up as a BASIC file (or use ABC to do
so if the file is defined in the dictionary) and finish filling it with
data. Pretty simple, actually. The code I've included below is somewhat
cluttered by the fact that I also wanted to get rid of the field label
prefix so you'll see some code that does that. Overall this solution is much
more kludgy that having a switch on the BASIC driver, but it works (C5EEB,
ABC templates at least). It should also probably be a template rather than
hand coded, but I'll leave that to someone else.

Thanks to everyone who helped me with this.
--
Patrick O'Brien
Orbtech
pobrien@orbtech.com
---

ThisWindow.TakeAccepted PROCEDURE

ReturnValue          BYTE,AUTO
Looped BYTE
!These variables were needed to create the field name header record in the
export file.
NewFile       File, Driver('ASCII'), Name(GLO:ExportFileName), PRE(HDR),
Create, Thread
Record          Record, Pre()
Header            String(10000)
                End
              End
FieldCounter  Long(0)
Loc:X         Unsigned, Auto

  CODE
  LOOP
    IF Looped
      RETURN Level:Notify
    ELSE
      Looped = 1
    END
  ReturnValue =PARENT.TakeAccepted()
    CASE ACCEPTED()
    OF ?ExportFileButton
      ThisWindow.Update
      LOC:Filename = FileLookup2.Ask(1)
      DISPLAY
      If LOC:Filename
        GLO:ExportFileName = LOC:Filename
        Relate:Export.Close
        !Create first record containing field names, without prefixes.
Kludgy but it works.
        !Assumes a csv file. Others, like tab separated, would need a
modified version of this code.
        Create(NewFile)
        Open(NewFile)
        If Error() Then Message(Error()) End
        Clear(HDR:Record)
        Loc:X = Instring(':', Export{PROP:Label, 1}, 1, 1)  !Find where the
prefix ends.
        HDR:Header = '"' & Sub(Export{PROP:Label, 1}, Loc:X+1,
Len(Export{PROP:Label, 1})-Loc:X) & '"'
        Loop FieldCounter = 2 To Export{PROP:Fields}
          HDR:Header = Clip(HDR:Header) & ',"' & Sub(Export{PROP:Label,
FieldCounter}, Loc:X+1, Len(Export{PROP:Label, FieldCounter})-Loc:X) & '"'
        End
        Add(NewFile)
        Close(NewFile)
        Relate:Export.Open
        ExportResults
      End
      Self.Reset()
    END
    RETURN ReturnValue
  END
  ReturnValue = Level:Fatal
  RETURN ReturnValue



Printed November 21, 2024, 6:55 am
This article has been viewed/printed 35317 times.