Importing a file with a Codeunit.

CDLSACDLSA Member Posts: 4
edited 2013-12-03 in Navision Attain
Is their anyone who has an example of a codeunit for importing a text-file (ASCII) in navision. Just to know how to find the first record of the txt-file and how to find the next record.

Without using a dataport so I would be able to call the CU with NAS to import the file.

Thanks.

Comments

  • wonmowonmo Member Posts: 139
    file.TEXTMODE(TRUE);
    file.WRITEMODE(FALSE);
    file.OPEN(file path and name);

    REPEAT
    file.READ(vstring);

    // do something with vstring
    UNTIL
    file.POS = file.LEN;

    This code reads 1 line of text at a time.

    Remember, vstring can only be a max of 1000 characters so each line cannot be more than 1000 characters.

    A dataport is much better to use because of this. You can also do the same thing with a dataport, some coding and the common dialog ocx.
  • bangswitbangswit Member Posts: 265
    wonmo wrote:
    file.TEXTMODE(TRUE);
    file.WRITEMODE(FALSE);
    file.OPEN(file path and name);

    REPEAT
    file.READ(vstring);

    // do something with vstring
    UNTIL
    file.POS = file.LEN;

    This code reads 1 line of text at a time.

    Remember, vstring can only be a max of 1000 characters so each line cannot be more than 1000 characters.

    A dataport is much better to use because of this. You can also do the same thing with a dataport, some coding and the common dialog ocx.
    but dataport cannot import 1 record that consist of more than 1 line
    isn't it?
  • julkifli33julkifli33 Member Posts: 1,046
    wonmo wrote:
    file.TEXTMODE(TRUE);
    file.WRITEMODE(FALSE);
    file.OPEN(file path and name);

    REPEAT
    file.READ(vstring);

    // do something with vstring
    UNTIL
    file.POS = file.LEN;

    This code reads 1 line of text at a time.

    Remember, vstring can only be a max of 1000 characters so each line cannot be more than 1000 characters.

    A dataport is much better to use because of this. You can also do the same thing with a dataport, some coding and the common dialog ocx.

    for example my data is --> no,name,address
    how to segregate ","
    so no goes to field no, name goes to field name, and address goes to field address
    thanks
  • bekiobekio Member Posts: 204
    After you read the line in .txt file, use STRPOS to determine delimiter of field, and COPYSTR to copy the string.
  • julkifli33julkifli33 Member Posts: 1,046
    bekio wrote:
    After you read the line in .txt file, use STRPOS to determine delimiter of field, and COPYSTR to copy the string.
    i'm confuse how to use STRPOS and COPYSTR
    here is my code and sample data
    i want to segregate no,name and address
    anyone can help?
    thanks
    OnRun()
    SFile.TEXTMODE(TRUE);
    SFile.WRITEMODE(FALSE);
    SFile.OPEN('D:\TestImport\Test.txt');
    REPEAT 
    SFile.READ(vString);
    vStringCode := STRPOS(vstring,';');
    TestImport.No := UPPERCASE(vStringCode);
    TestImport.INSERT;
    UNTIL
    SFile.POS = SFile.LEN;
    MESSAGE('Import Done');
    
    code2;Nama 2;Address 2
    code3;Nama 3;Address 3
    code4;Nama 4;Address 4
    code5;Nama 5;Address 5
  • Luc_VanDyckLuc_VanDyck Kontich (Belgium)Member, Moderator, Administrator Posts: 3,633
    Search the forum on COPYSTR + STRPOS and you'll find some postings with sample code. For example:
    viewtopic.php?f=32&t=44776
    viewtopic.php?f=23&t=15461
    No support using PM or e-mail - Please use this forum.
    NAV TechDays 2022: 17 & 18 November 2022, Antwerp (Belgium)
  • julkifli33julkifli33 Member Posts: 1,046
    Search the forum on COPYSTR + STRPOS and you'll find some postings with sample code. For example:
    viewtopic.php?f=32&t=44776
    viewtopic.php?f=23&t=15461
    ok
    thanks a lot for your help Luc
  • casanovacasanova Member Posts: 194
    anyone how to import txt file without delimeter?
    we use fixed length
  • geordiegeordie Member Posts: 655
    What is your problem exactly, splitting file lines in several variables?
Sign In or Register to comment.