Dec 02

As “Dinosaur”-tech as it might seem these days with most blogs posting about RTC, dataports is still one of the easiest ways to import data into NAVision. Having to search my old databases for a quick way to handle headers in datafiles, i thought i would share this with you. Hopefully Google will answer you with this post next time you have to do it :).

If you appreciate these beginners tips, let me know and i will dig into my archives before everyone leaves on the Dynamics Ark….

This small piece of code will handle writing headers when exporting, and also skip the first datarow when importing.

OnPreDataItem()
IF NOT CurrDataport.IMPORT THEN BEGIN
  Tab := 9; // 9=TAB, if you use another field seperator change it here!
  // Write Header fields
  CurrFile.TEXTMODE(TRUE);
  CurrFile.WRITE(
  'Item No (20)' +
  FORMAT(Tab) +
  'Description (30)');
  CurrFile.TEXTMODE(FALSE);
END ELSE BEGIN
  // Skip header
  CurrFile.TEXTMODE(TRUE);
  CurrFile.READ(Dummy); // read line into text variable
  CurrFile.TEXTMODE(FALSE);
END;

4 Responses to “Beginners Tip: Creating and Handling Headers in Dataports”

  1. Colin says:

    I just use an integer as the first DataItem for exporting.
    See Dataport 5051 in Cronus, it is a good example.

  2. @Colin: Agreed that is a good solution (for export). But the above will work for a dataport that is used for both export and import.

    Dataport 5050/5051 where they use the virtual table Integer, and set the dataport fields to: Cont.FIELDCAPTION(“No.”), only works for export, if you do an import you get an error saying: “The SourceExpr of data item no. 0 field no. 0 should be a variable to import.”.

  3. Colin says:

    Quite agree, just that I always remove the header for any import record or test for it and SKIP it.

    It is all about how much work is involved in creating the dataport and of course, how many ways you can skin a cat.
    (I hope that translates).

    I nearly always use global variables for imports and then validate the data I find before inserting into the table.

    Thanks for the idea however, I will try it out next time I get headers to deal with.

  4. José Monteiro says:

    Hi,

    This post was very helpful…

    I did some improvements concerning the headers because I needed the headers to be the field caption and to make the code more easy to read I add a function to prepare each header field:

    OnPreDataItem()

    IF NOT CurrDataport.IMPORT THEN BEGIN
    CurrFile.TEXTMODE(TRUE);
    CurrFile.WRITE(
    PrepareStringToFile(FIELDCAPTION(“Document No.”)), 0) +
    PrepareStringToFile(FIELDCAPTION(“”Line No.””)), 1)
    );
    CurrFile.TEXTMODE(FALSE);

    END;

    PreparaStringParaFicheiro(string : Text[100];cod_campo : Integer) str : Text[250]
    delim_txt := 34; // ascii ”
    delim_field := 59; // ascii ;

    IF cod_campo = 1 THEN
    str := FORMAT(delim_txt) + string + FORMAT(delim_txt)
    ELSE
    str := FORMAT(delim_txt) + string + FORMAT(delim_txt) + FORMAT(delim_field);

    Best regards,
    ZP.

Leave a Reply

preload preload preload
pornpants.com pornofri.com kilporn.com