Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!
  1. Monthly (Recurring) Target: $300 :: Achieved: $95
      Become a Supporter    ::   Make a Contribution   

how to do this???

Discussion in 'Information Technology' started by Henrootje, Jul 28, 2006.

  1. Henrootje

    Henrootje
    Expand Collapse
    Guest

    Hello Access-guru's, wizards and helpful people!

    I am trying to import several textfiles into several tables.
    Now I thought the following code up which works swell:


    Code:
    Dim Importspecification, TableName, File2BImported as string
    
    
    Importspecification ="<my Importspecification>"
    TableName = "<my Tablename>"
    File2BImported= "<my File to be imported>"
    
    
    DoCmd.TransferText acImportDelim, Importspecification, TableName,
    File2BImported, False, ""
    

    But I copy this code for every file and table I have to import.
    Is it somehow possible to create for example a table containing the
    tablenames, Importspecifications and so on?


    Importspecification TableName File2BImported
    Transactions tblTransactions
    c:\temp\transactions.txt
    Materials tblUsedMaterial
    c:\temp\materials.txt
    .............. ..............
    ....................


    Code:
    Dim Importspecification, TableName, File2BImported as string
    
    
    Importspecification ="Transactions"
    TableName = "tblTransactions"
    File2BImported= "c:\temp\transactions.txt"
    
    
    DoCmd.TransferText acImportDelim, Importspecification, TableName,
    File2BImported, False, ""
    

    So that it will take the first record out of the table, fill the
    variables using the first record in the table, execute the code and
    then proceed with the second record, execute the code and so on and on
    untill the end of the table? (seciond run would be as below;)


    Code:
    Importspecification ="Materials"
    TableName = "tblUsedMaterial"
    File2BImported= "c:\temp\materials.txt"
    
    
    DoCmd.TransferText acImportDelim, Importspecification, TableName,
    File2BImported, False, ""
    

    I do not mind if it would be a matrix in code, not using a table. These

    values will not change often, seldom even, but I want to reuse code
    where I can, to prevent typingerrors and stuff.


    I think it should be not that hard but to me unfortunately it is :(


    TIA, Henro!
     
  2. Terry Kreft

    Terry Kreft
    Expand Collapse
    Guest

    Sure, something like:-

    Dim db as DAO.Database
    Dim rs as DAO.Recordset

    Set db = Currentdb
    Set rs = db.OpenRecordset("Select * FROM Importspecifications",
    dbOpenSnapshot)

    With rs
    Do Until .EOF
    Call DoCmd.TransferText( _
    acImportDelim, _
    .Fields("Importspecification"), _
    .Fields("TableName"), _
    .Fields("File2BImported"), _
    False, ""
    )

    .MoveNext
    Loop
    .Close
    End With

    Set rs = Nothing
    Setdb= Nothing
    --

    Terry Kreft


    "Henrootje" <HenroV@gmail.com> wrote in message
    news:1150196316.071924.180260@p79g2000cwp.googlegroups.com...
    > Hello Access-guru's, wizards and helpful people!
    >
    > I am trying to import several textfiles into several tables.
    > Now I thought the following code up which works swell:
    >
    >
    >
    Code:
    > Dim Importspecification, TableName, File2BImported as string
    >
    >
    > Importspecification ="<my Importspecification>"
    > TableName = "<my Tablename>"
    > File2BImported= "<my File to be imported>"
    >
    >
    > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > File2BImported, False, ""
    > 
    >
    >
    > But I copy this code for every file and table I have to import.
    > Is it somehow possible to create for example a table containing the
    > tablenames, Importspecifications and so on?
    >
    >
    > Importspecification TableName File2BImported
    > Transactions tblTransactions
    > c:\temp\transactions.txt
    > Materials tblUsedMaterial
    > c:\temp\materials.txt
    > ............. ..............
    > ...................
    >
    >
    >
    Code:
    > Dim Importspecification, TableName, File2BImported as string
    >
    >
    > Importspecification ="Transactions"
    > TableName = "tblTransactions"
    > File2BImported= "c:\temp\transactions.txt"
    >
    >
    > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > File2BImported, False, ""
    > 
    >
    >
    > So that it will take the first record out of the table, fill the
    > variables using the first record in the table, execute the code and
    > then proceed with the second record, execute the code and so on and on
    > untill the end of the table? (seciond run would be as below;)
    >
    >
    >
    Code:
    > Importspecification ="Materials"
    > TableName = "tblUsedMaterial"
    > File2BImported= "c:\temp\materials.txt"
    >
    >
    > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > File2BImported, False, ""
    > 
    >
    >
    > I do not mind if it would be a matrix in code, not using a table. These
    >
    > values will not change often, seldom even, but I want to reuse code
    > where I can, to prevent typingerrors and stuff.
    >
    >
    > I think it should be not that hard but to me unfortunately it is :(
    >
    >
    > TIA, Henro!
    >
     
  3. Henrootje

    Henrootje
    Expand Collapse
    Guest

    Thank you Terry, for this contribution.

    I am working on it but I have a problem implementing this;

    Our company consists of 5 regions. Everything is identical for each
    region but!
    the name of the file to import is preceded by a number.
    So If I want to do region 5 I would like to have a 5 added at the
    beginning of File2BImported.

    For this goal I would like to use something like this:

    i = InputBox("What region?")

    Category = "Materials"
    FileLocation = "c:\files\"

    File2BImported = Filelocation & Category & "\" & i & Category & ".txt"

    --your code as suggested below---

    Category, FileLocation, TableName, Importspec should be drawn from the
    table.

    Your solution works but then I have to enter the same data 5 times in
    the table, one time for each region. Since the only difference between
    them is one number in one field I think that could be done otherwise?
    Although there are 5 regions, only one will be imported at a time, but
    it is the users discretion which one that will be.

    I cannot simply substitute .Fields("File2BImported") with
    File2BImported can I?
    If so, that would solve my problem straight away!


    Terry Kreft schreef:

    > Sure, something like:-
    >
    > Dim db as DAO.Database
    > Dim rs as DAO.Recordset
    >
    > Set db = Currentdb
    > Set rs = db.OpenRecordset("Select * FROM Importspecifications",
    > dbOpenSnapshot)
    >
    > With rs
    > Do Until .EOF
    > Call DoCmd.TransferText( _
    > acImportDelim, _
    > .Fields("Importspecification"), _
    > .Fields("TableName"), _
    > .Fields("File2BImported"), _
    > False, ""
    > )
    >
    > .MoveNext
    > Loop
    > .Close
    > End With
    >
    > Set rs = Nothing
    > Setdb= Nothing
    > --
    >
    > Terry Kreft
    >
    >
    > "Henrootje" <HenroV@gmail.com> wrote in message
    > news:1150196316.071924.180260@p79g2000cwp.googlegroups.com...
    > > Hello Access-guru's, wizards and helpful people!
    > >
    > > I am trying to import several textfiles into several tables.
    > > Now I thought the following code up which works swell:
    > >
    > >
    > >
    Code:
    > > Dim Importspecification, TableName, File2BImported as string
    > >
    > >
    > > Importspecification ="<my Importspecification>"
    > > TableName = "<my Tablename>"
    > > File2BImported= "<my File to be imported>"
    > >
    > >
    > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > File2BImported, False, ""
    > > 
    > >
    > >
    > > But I copy this code for every file and table I have to import.
    > > Is it somehow possible to create for example a table containing the
    > > tablenames, Importspecifications and so on?
    > >
    > >
    > > Importspecification TableName File2BImported
    > > Transactions tblTransactions
    > > c:\temp\transactions.txt
    > > Materials tblUsedMaterial
    > > c:\temp\materials.txt
    > > ............. ..............
    > > ...................
    > >
    > >
    > >
    Code:
    > > Dim Importspecification, TableName, File2BImported as string
    > >
    > >
    > > Importspecification ="Transactions"
    > > TableName = "tblTransactions"
    > > File2BImported= "c:\temp\transactions.txt"
    > >
    > >
    > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > File2BImported, False, ""
    > > 
    > >
    > >
    > > So that it will take the first record out of the table, fill the
    > > variables using the first record in the table, execute the code and
    > > then proceed with the second record, execute the code and so on and on
    > > untill the end of the table? (seciond run would be as below;)
    > >
    > >
    > >
    Code:
    > > Importspecification ="Materials"
    > > TableName = "tblUsedMaterial"
    > > File2BImported= "c:\temp\materials.txt"
    > >
    > >
    > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > File2BImported, False, ""
    > > 
    > >
    > >
    > > I do not mind if it would be a matrix in code, not using a table. These
    > >
    > > values will not change often, seldom even, but I want to reuse code
    > > where I can, to prevent typingerrors and stuff.
    > >
    > >
    > > I think it should be not that hard but to me unfortunately it is :(
    > >
    > >
    > > TIA, Henro!
    > >
     
  4. Terry Kreft

    Terry Kreft
    Expand Collapse
    Guest

    If the numbers are consecutive and you want to import all of the files then
    you could do something like

    Dim db as DAO.Database
    Dim rs as DAO.Recordset
    Dim iRegion as integer

    Set db = Currentdb
    Set rs = db.OpenRecordset("Select * FROM Importspecifications",
    dbOpenSnapshot)

    With rs
    Do Until .EOF
    For iRegion = 1 to 5
    Call DoCmd.TransferText( _
    acImportDelim, _
    .Fields("Importspecification"), _
    .Fields("TableName"), _
    iRegion & .Fields("File2BImported"), _
    False, ""
    )
    Next
    .MoveNext
    Loop
    .Close
    End With

    Set rs = Nothing
    Set db= Nothing


    Or if you want to do 1 set of files you could do as you suggest below,
    something like

    Dim db as DAO.Database
    Dim rs as DAO.Recordset
    Dim iRegion as integer

    iRegion = val(Inputbox("Which Region?"))

    ' Test here that iRegion is valid before proceeding

    Set db = Currentdb
    Set rs = db.OpenRecordset("Select * FROM Importspecifications",
    dbOpenSnapshot)


    With rs
    Do Until .EOF
    Call DoCmd.TransferText( _
    acImportDelim, _
    .Fields("Importspecification"), _
    .Fields("TableName"), _
    iRegion & .Fields("File2BImported"), _
    False, ""
    )
    .MoveNext
    Loop
    .Close
    End With

    Set rs = Nothing
    Set db= Nothing



    --

    Terry Kreft


    "Henrootje" <HenroV@gmail.com> wrote in message
    news:1150201644.781761.16600@f14g2000cwb.googlegroups.com...
    > Thank you Terry, for this contribution.
    >
    > I am working on it but I have a problem implementing this;
    >
    > Our company consists of 5 regions. Everything is identical for each
    > region but!
    > the name of the file to import is preceded by a number.
    > So If I want to do region 5 I would like to have a 5 added at the
    > beginning of File2BImported.
    >
    > For this goal I would like to use something like this:
    >
    > i = InputBox("What region?")
    >
    > Category = "Materials"
    > FileLocation = "c:\files\"
    >
    > File2BImported = Filelocation & Category & "\" & i & Category & ".txt"
    >
    > --your code as suggested below---
    >
    > Category, FileLocation, TableName, Importspec should be drawn from the
    > table.
    >
    > Your solution works but then I have to enter the same data 5 times in
    > the table, one time for each region. Since the only difference between
    > them is one number in one field I think that could be done otherwise?
    > Although there are 5 regions, only one will be imported at a time, but
    > it is the users discretion which one that will be.
    >
    > I cannot simply substitute .Fields("File2BImported") with
    > File2BImported can I?
    > If so, that would solve my problem straight away!
    >
    >
    > Terry Kreft schreef:
    >
    > > Sure, something like:-
    > >
    > > Dim db as DAO.Database
    > > Dim rs as DAO.Recordset
    > >
    > > Set db = Currentdb
    > > Set rs = db.OpenRecordset("Select * FROM Importspecifications",
    > > dbOpenSnapshot)
    > >
    > > With rs
    > > Do Until .EOF
    > > Call DoCmd.TransferText( _
    > > acImportDelim, _
    > > .Fields("Importspecification"), _
    > > .Fields("TableName"), _
    > > .Fields("File2BImported"), _
    > > False, ""
    > > )
    > >
    > > .MoveNext
    > > Loop
    > > .Close
    > > End With
    > >
    > > Set rs = Nothing
    > > Setdb= Nothing
    > > --
    > >
    > > Terry Kreft
    > >
    > >
    > > "Henrootje" <HenroV@gmail.com> wrote in message
    > > news:1150196316.071924.180260@p79g2000cwp.googlegroups.com...
    > > > Hello Access-guru's, wizards and helpful people!
    > > >
    > > > I am trying to import several textfiles into several tables.
    > > > Now I thought the following code up which works swell:
    > > >
    > > >
    > > >
    Code:
    > > > Dim Importspecification, TableName, File2BImported as string
    > > >
    > > >
    > > > Importspecification ="<my Importspecification>"
    > > > TableName = "<my Tablename>"
    > > > File2BImported= "<my File to be imported>"
    > > >
    > > >
    > > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > > File2BImported, False, ""
    > > > 
    > > >
    > > >
    > > > But I copy this code for every file and table I have to import.
    > > > Is it somehow possible to create for example a table containing the
    > > > tablenames, Importspecifications and so on?
    > > >
    > > >
    > > > Importspecification TableName File2BImported
    > > > Transactions tblTransactions
    > > > c:\temp\transactions.txt
    > > > Materials tblUsedMaterial
    > > > c:\temp\materials.txt
    > > > ............. ..............
    > > > ...................
    > > >
    > > >
    > > >
    Code:
    > > > Dim Importspecification, TableName, File2BImported as string
    > > >
    > > >
    > > > Importspecification ="Transactions"
    > > > TableName = "tblTransactions"
    > > > File2BImported= "c:\temp\transactions.txt"
    > > >
    > > >
    > > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > > File2BImported, False, ""
    > > > 
    > > >
    > > >
    > > > So that it will take the first record out of the table, fill the
    > > > variables using the first record in the table, execute the code and
    > > > then proceed with the second record, execute the code and so on and on
    > > > untill the end of the table? (seciond run would be as below;)
    > > >
    > > >
    > > >
    Code:
    > > > Importspecification ="Materials"
    > > > TableName = "tblUsedMaterial"
    > > > File2BImported= "c:\temp\materials.txt"
    > > >
    > > >
    > > > DoCmd.TransferText acImportDelim, Importspecification, TableName,
    > > > File2BImported, False, ""
    > > > 
    > > >
    > > >
    > > > I do not mind if it would be a matrix in code, not using a table.

    These
    > > >
    > > > values will not change often, seldom even, but I want to reuse code
    > > > where I can, to prevent typingerrors and stuff.
    > > >
    > > >
    > > > I think it should be not that hard but to me unfortunately it is :(
    > > >
    > > >
    > > > TIA, Henro!
    > > >

    >
     
Since you're here... we have a small favor to ask...

More people are visiting & reading SPN than ever but far fewer are paying to sustain it. Advertising revenues across the online media have fallen fast. So you can see why we need to ask for your help. Donating to SPN's is vote for free speech, for diversity of opinions, for the right of the people to stand up to religious bigotry. Without any affiliation to any organization, this constant struggle takes a lot of hard work to sustain as we entirely depend on the contributions of our esteemed writers/readers. We do it because we believe our perspective matters – because it might well be your perspective, too... Fund our efforts and together we can keep the world informed about the real Sikh Sikhi Sikhism. If everyone who writes or reads our content, who likes it, helps us to pay for it, our future would be much more secure. Every Contribution Matters, Contribute Generously!

    Become a Supporter      ::     Make a Contribution     



Share This Page